Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .evergreen/run-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
37 changes: 37 additions & 0 deletions .github/workflows/django_settings.py
Original file line number Diff line number Diff line change
@@ -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
38 changes: 3 additions & 35 deletions .github/workflows/mongodb_settings.py
Original file line number Diff line number Diff line change
@@ -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"]
12 changes: 10 additions & 2 deletions .github/workflows/runtests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
4 changes: 2 additions & 2 deletions .github/workflows/test-python-atlas.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/test-python-geo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/test-python.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions docs/internals/contributing/unit-tests.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
5 changes: 0 additions & 5 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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.*)*:",
Expand Down
Loading