This project includes utilities for database initialization and seeding when performing End-To-End tests.
E2EViewSet has a set of endpoints for managing the test data from the frontend E2E test client (usually from Cypress):
- POST
/load-data
- load data using Django's default serizlization format. Example payload:
{
"objects": [{
"model": "users.user",
"fields": {
"email": "abc@tsl.io",
// ...
}}, {
"model": "users.user",
"fields": {
"email": "def@tsl.io",
// ...
}
}]
}
- POST
/load-script
- load data through a script module that exists in the backend project repository. Example:
Set the SCRIPTS_PACKAGE
setting to a module path where the scripts are located:
E2E = {
"ENABLED": True,
"SCRIPTS_PACKAGE": "e2e.scripts",
}
Then add a python script on the defined module path, e.g.: <project-root>/e2e/scripts/users.py
. The script is expected to have a def load()
function that will be executed by the endpoint.
import tests.factories as f
def load():
f.UserFactory()
To request the script to be run use request format as:
{
"scripts": ["users"]
}
- POST
/flush-data
- remove the data from database using Django flush command - POST
/set-password
- set a password for an existing user. Example payload:
{
"user": <user id>,
"password": "1234"
}
There is a test project with a complete demo set up.
Add to requirements of yor project (replacing everything inside brackets):
baseapp-e2e @ git+https://github.com/silverlogic/baseapp-backend.git@v0.1#subdirectory=baseapp-e2e
Add the app to your project INSTALLED_APPS:
INSTALLED_APPS = [
...
"baseapp_e2e",
]
Add E2E settings to Django settings file of your application:
E2E = {
"ENABLED": True,
"SCRIPTS_PACKAGE": "e2e.scripts",
}
General development instructions can be found in main README