What is the best way to spin up databases/other se...
# general
r
What is the best way to spin up databases/other services for testing purposes via
pants
? In conventional CI tool I'd just
docker-compose up
stuff and run my
pytest
p
this should work with pants too. The issue might be that pants will run multiple tests in parallel and if they hit the same DB (as in DB schema, not DB server) it will likely cause issues. There are a few ways around it, one is using execution slots mechanism. https://www.pantsbuild.org/docs/troubleshooting#controlling-test-parallelism the other way is to have logic in conftest.py that will create a unique DB schema each time it runs. so with Django settings.DATABASES it will look something a similar to :
Copy code
def get_test_db_config(test_db: db_name: str) -> dict:
    return {
        "ENGINE": "django.db.backends.postgresql",
        "NAME": "postgres",
        "USER": "postgres",
        "HOST": DB_HOST,
        "PORT": DB_PORT,
        "TEST": {
            "DEPENDENCIES": [],
            # NB: We use a random UUID to ensure that tests run concurrently (i.e. through the V2 test
            # runner) do not share the same database name.
            "NAME": f"testdb_{db_name}_{shortuuid.uuid()}",
        },
    }
DATABASES = {"users_db": get_test_db_config("users_db")}
@hundreds-father-404 since the examle-django repo uses sqlite... this technique is not used there. I think it is useful to either move the
examle-django
repo to use a real wold DB or at least document that case.... this is a very common scenario that should be addressed in docs.
r
So how it would look like, in outline, if I need to start a container with DB, migrate some data before tests in it, and finally run the tests (in my case it's in a container, too, so I presume that case isn't a subject for that parallel issue)?