Skip to content

Commit d840037

Browse files
committed
Use separate settings for django/tests and django_mongodb_backend/tests
This paves the way for separate settings for queryable encryption.
1 parent 84dc005 commit d840037

File tree

8 files changed

+56
-47
lines changed

8 files changed

+56
-47
lines changed

.evergreen/run-tests.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ pip install -e ..
1515
pip install -r requirements/py3.txt
1616
popd
1717

18-
# Copy the test settings file
19-
cp ./.github/workflows/mongodb_settings.py django_repo/tests/
18+
# Copy the test settings files
19+
cp ./.github/workflows/*_settings.py django_repo/tests/
2020

2121
# Copy the test runner file
2222
cp ./.github/workflows/runtests.py django_repo/tests/runtests_.py
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Settings for django/tests.
2+
import os
3+
4+
from pymongo.uri_parser import parse_uri
5+
6+
if mongodb_uri := os.getenv("MONGODB_URI"):
7+
db_settings = {
8+
"ENGINE": "django_mongodb_backend",
9+
"HOST": mongodb_uri,
10+
}
11+
# Workaround for https://github.com/mongodb-labs/mongo-orchestration/issues/268
12+
uri = parse_uri(mongodb_uri)
13+
if uri.get("username") and uri.get("password"):
14+
db_settings["OPTIONS"] = {"tls": True, "tlsAllowInvalidCertificates": True}
15+
DATABASES = {
16+
"default": {**db_settings, "NAME": "djangotests"},
17+
"other": {**db_settings, "NAME": "djangotests-other"},
18+
}
19+
else:
20+
DATABASES = {
21+
"default": {
22+
"ENGINE": "django_mongodb_backend",
23+
"NAME": "djangotests",
24+
# Required when connecting to the Atlas image in Docker.
25+
"OPTIONS": {"directConnection": True},
26+
},
27+
"other": {
28+
"ENGINE": "django_mongodb_backend",
29+
"NAME": "djangotests-other",
30+
"OPTIONS": {"directConnection": True},
31+
},
32+
}
33+
34+
DEFAULT_AUTO_FIELD = "django_mongodb_backend.fields.ObjectIdAutoField"
35+
PASSWORD_HASHERS = ("django.contrib.auth.hashers.MD5PasswordHasher",)
36+
SECRET_KEY = "django_tests_secret_key" # noqa: S105
37+
USE_TZ = False
Lines changed: 3 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,4 @@
1-
import os
1+
# Settings for django_mongodb_backend/tests.
2+
from django_settings import * # noqa: F403
23

3-
from pymongo.uri_parser import parse_uri
4-
5-
if mongodb_uri := os.getenv("MONGODB_URI"):
6-
db_settings = {
7-
"ENGINE": "django_mongodb_backend",
8-
"HOST": mongodb_uri,
9-
}
10-
# Workaround for https://github.com/mongodb-labs/mongo-orchestration/issues/268
11-
uri = parse_uri(mongodb_uri)
12-
if uri.get("username") and uri.get("password"):
13-
db_settings["OPTIONS"] = {"tls": True, "tlsAllowInvalidCertificates": True}
14-
DATABASES = {
15-
"default": {**db_settings, "NAME": "djangotests"},
16-
"other": {**db_settings, "NAME": "djangotests-other"},
17-
}
18-
else:
19-
DATABASES = {
20-
"default": {
21-
"ENGINE": "django_mongodb_backend",
22-
"NAME": "djangotests",
23-
# Required when connecting to the Atlas image in Docker.
24-
"OPTIONS": {"directConnection": True},
25-
},
26-
"other": {
27-
"ENGINE": "django_mongodb_backend",
28-
"NAME": "djangotests-other",
29-
"OPTIONS": {"directConnection": True},
30-
},
31-
}
32-
33-
DEFAULT_AUTO_FIELD = "django_mongodb_backend.fields.ObjectIdAutoField"
34-
PASSWORD_HASHERS = ("django.contrib.auth.hashers.MD5PasswordHasher",)
35-
SECRET_KEY = "django_tests_secret_key"
36-
USE_TZ = False
4+
DATABASE_ROUTERS = ["django_mongodb_backend.routers.MongoRouter"]

.github/workflows/runtests.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,10 +172,18 @@
172172

173173
runtests = pathlib.Path(__file__).parent.resolve() / "runtests.py"
174174
run_tests_cmd = f"python3 {runtests} %s --settings mongodb_settings -v 2"
175+
run_tests_cmd = f"python3 {runtests} %s --settings %s -v 2"
175176

176177
shouldFail = False
177178
for app_name in test_apps:
178-
res = os.system(run_tests_cmd % app_name) # noqa: S605
179+
# Use the custom settings module only for django_mongodb_backend's tests
180+
# (which always end with an underscore).
181+
settings_module = (
182+
os.environ.get("DJANGO_SETTINGS_MODULE", "mongodb_settings")
183+
if app_name.endswith("_")
184+
else "django_settings"
185+
)
186+
res = os.system(run_tests_cmd % (app_name, settings_module)) # noqa: S605
179187
if res != 0:
180188
shouldFail = True
181189
sys.exit(1 if shouldFail else 0)

.github/workflows/test-python-geo.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ jobs:
4646
cd django_repo/tests/
4747
pip3 install -e ..
4848
pip3 install -r requirements/py3.txt
49-
- name: Copy the test settings file
50-
run: cp .github/workflows/mongodb_settings.py django_repo/tests/
49+
- name: Copy the test settings files
50+
run: cp .github/workflows/*_settings.py django_repo/tests/
5151
- name: Copy the test runner file
5252
run: cp .github/workflows/runtests.py django_repo/tests/runtests_.py
5353
- name: Start MongoDB

.github/workflows/test-python.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ jobs:
4545
cd django_repo/tests/
4646
pip3 install -e ..
4747
pip3 install -r requirements/py3.txt
48-
- name: Copy the test settings file
49-
run: cp .github/workflows/mongodb_settings.py django_repo/tests/
48+
- name: Copy the test settings files
49+
run: cp .github/workflows/*_settings.py django_repo/tests/
5050
- name: Copy the test runner file
5151
run: cp .github/workflows/runtests.py django_repo/tests/runtests_.py
5252
- name: Start MongoDB

docs/internals/contributing/unit-tests.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ Then, create a test settings file, ``django-repo/tests/test_mongo.py``::
6666
},
6767
}
6868

69+
DATABASE_ROUTERS = ["django_mongodb_backend.routers.MongoRouter"]
6970
DEFAULT_AUTO_FIELD = "django_mongodb_backend.fields.ObjectIdAutoField"
7071

7172
Finally, you can run the test script in the Django repository:

pyproject.toml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,6 @@ unfixable = []
103103
exclude = []
104104
dummy-variable-rgx = "^(_+|(_+[a-zA-Z0-9_]*[a-zA-Z0-9]+?)|dummy.*)$"
105105

106-
[tool.ruff.lint.per-file-ignores]
107-
".github/workflows/mongodb_settings.py" = [
108-
"S105", # Possible hardcoded password assigned to: "SECRET_KEY"
109-
]
110-
111106
[tool.coverage.report]
112107
exclude_lines = [
113108
"if (.*and +)*_use_c( and.*)*:",

0 commit comments

Comments
 (0)