From 89c704901e6bb93ec20cf2db90a7a7afc52b6953 Mon Sep 17 00:00:00 2001 From: Tim Graham Date: Sat, 25 Oct 2025 19:38:24 -0400 Subject: [PATCH] Use separate settings for django/tests and django_mongodb_backend/tests This paves the way for separate settings for queryable encryption. --- .evergreen/run-tests.sh | 4 +-- .github/workflows/django_settings.py | 37 +++++++++++++++++++++ .github/workflows/mongodb_settings.py | 38 ++-------------------- .github/workflows/runtests.py | 12 +++++-- .github/workflows/test-python-atlas.yml | 4 +-- .github/workflows/test-python-geo.yml | 4 +-- .github/workflows/test-python.yml | 4 +-- docs/internals/contributing/unit-tests.rst | 1 + pyproject.toml | 5 --- 9 files changed, 59 insertions(+), 50 deletions(-) create mode 100644 .github/workflows/django_settings.py diff --git a/.evergreen/run-tests.sh b/.evergreen/run-tests.sh index f49a0e9a9..700f9bf98 100644 --- a/.evergreen/run-tests.sh +++ b/.evergreen/run-tests.sh @@ -15,8 +15,8 @@ pip install -e .. pip install -r requirements/py3.txt popd -# Copy the test settings file -cp ./.github/workflows/mongodb_settings.py django_repo/tests/ +# Copy the test settings files +cp ./.github/workflows/*_settings.py django_repo/tests/ # Copy the test runner file cp ./.github/workflows/runtests.py django_repo/tests/runtests_.py diff --git a/.github/workflows/django_settings.py b/.github/workflows/django_settings.py new file mode 100644 index 000000000..67cfa6d34 --- /dev/null +++ b/.github/workflows/django_settings.py @@ -0,0 +1,37 @@ +# Settings for django/tests. +import os + +from pymongo.uri_parser import parse_uri + +if mongodb_uri := os.getenv("MONGODB_URI"): + db_settings = { + "ENGINE": "django_mongodb_backend", + "HOST": mongodb_uri, + } + # Workaround for https://github.com/mongodb-labs/mongo-orchestration/issues/268 + uri = parse_uri(mongodb_uri) + if uri.get("username") and uri.get("password"): + db_settings["OPTIONS"] = {"tls": True, "tlsAllowInvalidCertificates": True} + DATABASES = { + "default": {**db_settings, "NAME": "djangotests"}, + "other": {**db_settings, "NAME": "djangotests-other"}, + } +else: + DATABASES = { + "default": { + "ENGINE": "django_mongodb_backend", + "NAME": "djangotests", + # Required when connecting to the Atlas image in Docker. + "OPTIONS": {"directConnection": True}, + }, + "other": { + "ENGINE": "django_mongodb_backend", + "NAME": "djangotests-other", + "OPTIONS": {"directConnection": True}, + }, + } + +DEFAULT_AUTO_FIELD = "django_mongodb_backend.fields.ObjectIdAutoField" +PASSWORD_HASHERS = ("django.contrib.auth.hashers.MD5PasswordHasher",) +SECRET_KEY = "django_tests_secret_key" # noqa: S105 +USE_TZ = False diff --git a/.github/workflows/mongodb_settings.py b/.github/workflows/mongodb_settings.py index 20cd41cbc..4dce3c0d5 100644 --- a/.github/workflows/mongodb_settings.py +++ b/.github/workflows/mongodb_settings.py @@ -1,36 +1,4 @@ -import os +# Settings for django_mongodb_backend/tests. +from django_settings import * # noqa: F403 -from pymongo.uri_parser import parse_uri - -if mongodb_uri := os.getenv("MONGODB_URI"): - db_settings = { - "ENGINE": "django_mongodb_backend", - "HOST": mongodb_uri, - } - # Workaround for https://github.com/mongodb-labs/mongo-orchestration/issues/268 - uri = parse_uri(mongodb_uri) - if uri.get("username") and uri.get("password"): - db_settings["OPTIONS"] = {"tls": True, "tlsAllowInvalidCertificates": True} - DATABASES = { - "default": {**db_settings, "NAME": "djangotests"}, - "other": {**db_settings, "NAME": "djangotests-other"}, - } -else: - DATABASES = { - "default": { - "ENGINE": "django_mongodb_backend", - "NAME": "djangotests", - # Required when connecting to the Atlas image in Docker. - "OPTIONS": {"directConnection": True}, - }, - "other": { - "ENGINE": "django_mongodb_backend", - "NAME": "djangotests-other", - "OPTIONS": {"directConnection": True}, - }, - } - -DEFAULT_AUTO_FIELD = "django_mongodb_backend.fields.ObjectIdAutoField" -PASSWORD_HASHERS = ("django.contrib.auth.hashers.MD5PasswordHasher",) -SECRET_KEY = "django_tests_secret_key" -USE_TZ = False +DATABASE_ROUTERS = ["django_mongodb_backend.routers.MongoRouter"] diff --git a/.github/workflows/runtests.py b/.github/workflows/runtests.py index 9e4096b55..cc258f363 100755 --- a/.github/workflows/runtests.py +++ b/.github/workflows/runtests.py @@ -171,11 +171,19 @@ test_apps.extend(["gis_tests", "gis_tests_"]) runtests = pathlib.Path(__file__).parent.resolve() / "runtests.py" -run_tests_cmd = f"python3 {runtests} %s --settings mongodb_settings -v 2" +run_tests_cmd = f"python3 {runtests} %s --settings %s -v 2" shouldFail = False for app_name in test_apps: - res = os.system(run_tests_cmd % app_name) # noqa: S605 + # Use the custom settings module only for django_mongodb_backend's tests + # (which always end with an underscore). Some of Django's tests aren't + # compatible with extra DATABASE_ROUTERS or other DATABASES aliases. + settings_module = ( + os.environ.get("DJANGO_SETTINGS_MODULE", "mongodb_settings") + if app_name.endswith("_") + else "django_settings" + ) + res = os.system(run_tests_cmd % (app_name, settings_module)) # noqa: S605 if res != 0: shouldFail = True sys.exit(1 if shouldFail else 0) diff --git a/.github/workflows/test-python-atlas.yml b/.github/workflows/test-python-atlas.yml index 6f9553422..e98d2512d 100644 --- a/.github/workflows/test-python-atlas.yml +++ b/.github/workflows/test-python-atlas.yml @@ -45,8 +45,8 @@ jobs: cd django_repo/tests/ pip3 install -e .. pip3 install -r requirements/py3.txt - - name: Copy the test settings file - run: cp .github/workflows/mongodb_settings.py django_repo/tests/ + - name: Copy the test settings files + run: cp .github/workflows/*_settings.py django_repo/tests/ - name: Copy the test runner file run: cp .github/workflows/runtests.py django_repo/tests/runtests_.py - name: Start local Atlas diff --git a/.github/workflows/test-python-geo.yml b/.github/workflows/test-python-geo.yml index 0976bb064..309f3506a 100644 --- a/.github/workflows/test-python-geo.yml +++ b/.github/workflows/test-python-geo.yml @@ -46,8 +46,8 @@ jobs: cd django_repo/tests/ pip3 install -e .. pip3 install -r requirements/py3.txt - - name: Copy the test settings file - run: cp .github/workflows/mongodb_settings.py django_repo/tests/ + - name: Copy the test settings files + run: cp .github/workflows/*_settings.py django_repo/tests/ - name: Copy the test runner file run: cp .github/workflows/runtests.py django_repo/tests/runtests_.py - name: Start MongoDB diff --git a/.github/workflows/test-python.yml b/.github/workflows/test-python.yml index ff36c8949..7f74b3376 100644 --- a/.github/workflows/test-python.yml +++ b/.github/workflows/test-python.yml @@ -45,8 +45,8 @@ jobs: cd django_repo/tests/ pip3 install -e .. pip3 install -r requirements/py3.txt - - name: Copy the test settings file - run: cp .github/workflows/mongodb_settings.py django_repo/tests/ + - name: Copy the test settings files + run: cp .github/workflows/*_settings.py django_repo/tests/ - name: Copy the test runner file run: cp .github/workflows/runtests.py django_repo/tests/runtests_.py - name: Start MongoDB diff --git a/docs/internals/contributing/unit-tests.rst b/docs/internals/contributing/unit-tests.rst index 2144e705b..1804fe273 100644 --- a/docs/internals/contributing/unit-tests.rst +++ b/docs/internals/contributing/unit-tests.rst @@ -66,6 +66,7 @@ Then, create a test settings file, ``django-repo/tests/test_mongo.py``:: }, } + DATABASE_ROUTERS = ["django_mongodb_backend.routers.MongoRouter"] DEFAULT_AUTO_FIELD = "django_mongodb_backend.fields.ObjectIdAutoField" Finally, you can run the test script in the Django repository: diff --git a/pyproject.toml b/pyproject.toml index 30252e913..0549f02ef 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -103,11 +103,6 @@ unfixable = [] exclude = [] dummy-variable-rgx = "^(_+|(_+[a-zA-Z0-9_]*[a-zA-Z0-9]+?)|dummy.*)$" -[tool.ruff.lint.per-file-ignores] -".github/workflows/mongodb_settings.py" = [ - "S105", # Possible hardcoded password assigned to: "SECRET_KEY" -] - [tool.coverage.report] exclude_lines = [ "if (.*and +)*_use_c( and.*)*:",