Skip to content

Commit 17f2311

Browse files
committed
Move DATABASES config to utils
1 parent 9fbe39c commit 17f2311

File tree

7 files changed

+32
-86
lines changed

7 files changed

+32
-86
lines changed

config/allauth/allauth_settings.py

+3-8
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
import django_mongodb_backend
2-
import os
3-
41
from bson import ObjectId
52
from pathlib import Path
63

74
from django.contrib.auth.hashers import PBKDF2PasswordHasher
85

9-
DATABASE_URL = os.environ.get("MONGODB_URI", "mongodb://localhost:27017/djangotests")
6+
from django_mongodb_cli.utils import get_databases
7+
108

9+
DATABASES = get_databases("allauth")
1110

1211
SECRET_KEY = "psst"
1312
SITE_ID = ObjectId()
@@ -18,10 +17,6 @@
1817
USE_I18N = False
1918
USE_TZ = True
2019

21-
DATABASES = {
22-
"default": django_mongodb_backend.parse_uri(DATABASE_URL),
23-
}
24-
2520
ROOT_URLCONF = "tests.regular.urls"
2621
LOGIN_URL = "/accounts/login/"
2722

config/debug_toolbar/debug_toolbar_settings.py

+5-31
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
"""Django settings for tests."""
22

33
import os
4+
from django_mongodb_cli.utils import get_databases
5+
6+
7+
DATABASES = get_databases("debug_toolbar")
48

59
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
610

@@ -91,37 +95,7 @@
9195
"second": {"BACKEND": "django.core.cache.backends.locmem.LocMemCache"},
9296
}
9397

94-
DATABASES = {
95-
"default": {
96-
"ENGINE": "django.{}db.backends.{}".format(
97-
"contrib.gis." if USE_GIS else "", os.getenv("DB_BACKEND", "sqlite3")
98-
),
99-
"NAME": os.getenv("DB_NAME", ":memory:"),
100-
"USER": os.getenv("DB_USER"),
101-
"PASSWORD": os.getenv("DB_PASSWORD"),
102-
"HOST": os.getenv("DB_HOST", ""),
103-
"PORT": os.getenv("DB_PORT", ""),
104-
"TEST": {
105-
"USER": "default_test",
106-
},
107-
},
108-
"replica": {
109-
"ENGINE": "django.{}db.backends.{}".format(
110-
"contrib.gis." if USE_GIS else "", os.getenv("DB_BACKEND", "sqlite3")
111-
),
112-
"NAME": os.getenv("DB_NAME", ":memory:"),
113-
"USER": os.getenv("DB_USER"),
114-
"PASSWORD": os.getenv("DB_PASSWORD"),
115-
"HOST": os.getenv("DB_HOST", ""),
116-
"PORT": os.getenv("DB_PORT", ""),
117-
"TEST": {
118-
"USER": "default_test",
119-
"MIRROR": "default",
120-
},
121-
},
122-
}
123-
124-
DEFAULT_AUTO_FIELD = "django.db.models.AutoField"
98+
DEFAULT_AUTO_FIELD = "django_mongodb_backend.fields.ObjectIdAutoField"
12599

126100
# Debug Toolbar configuration
127101

config/django/django_settings.py

+3-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
import django_mongodb_backend
2-
import os
1+
from django_mongodb_cli.utils import get_databases
32

43

5-
MONGODB_URI = os.environ.get("MONGODB_URI", "mongodb://localhost:27017/djangotests")
6-
DATABASES = {
7-
"default": django_mongodb_backend.parse_uri(MONGODB_URI),
8-
}
4+
DATABASES = get_databases("django")
5+
96
DEFAULT_AUTO_FIELD = "django_mongodb_backend.fields.ObjectIdAutoField"
107
PASSWORD_HASHERS = ("django.contrib.auth.hashers.MD5PasswordHasher",)
118
SECRET_KEY = "django_tests_secret_key"

config/filter/filter_settings.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
# ensure package/conf is importable
22
from django_filters.conf import DEFAULTS
33

4-
import django_mongodb_backend
5-
import os
64

7-
DATABASES = {}
8-
DATABASE_URL = os.environ.get("MONGODB_URI", "mongodb://localhost:27017/djangotests")
9-
DATABASES["default"] = django_mongodb_backend.parse_uri(DATABASE_URL)
5+
from django_mongodb_cli.utils import get_databases
6+
7+
8+
DATABASES = get_databases("django_filter")
9+
1010

1111
INSTALLED_APPS = (
1212
"tests.mongo_apps.MongoContentTypesConfig",

config/rest_framework/rest_framework_settings.py

+3-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import os
22

33
import django
4-
import django_mongodb_backend
54
from bson import ObjectId
65
from django.core import management
76

8-
DATABASE_URL = os.environ.get("MONGODB_URI", "mongodb://localhost:27017/djangotests")
7+
from django_mongodb_cli.utils import get_databases
98

109

1110
def pytest_addoption(parser):
@@ -23,10 +22,8 @@ def pytest_configure(config):
2322

2423
settings.configure(
2524
DEBUG_PROPAGATE_EXCEPTIONS=True,
26-
DATABASES={
27-
"default": django_mongodb_backend.parse_uri(DATABASE_URL),
28-
},
29-
SITE_ID=ObjectId(),
25+
DATABASES=get_databases("rest_framework"),
26+
SITE_ID=ObjectId("000000000000000000000001"),
3027
SECRET_KEY="not very secret in tests",
3128
USE_I18N=True,
3229
STATIC_URL="/static/",

config/wagtail/wagtail_settings.py

+2-30
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from django.utils.translation import gettext_lazy as _
77

88
from wagtail.test.numberformat import patch_number_formats
9+
from django_mongodb_cli.utils import get_databases
910

1011
WAGTAIL_CHECK_TEMPLATE_NUMBER_FORMAT = (
1112
os.environ.get("WAGTAIL_CHECK_TEMPLATE_NUMBER_FORMAT", "0") == "1"
@@ -24,36 +25,7 @@
2425

2526
TIME_ZONE = "Asia/Tokyo"
2627

27-
DATABASES = {
28-
"default": {
29-
"ENGINE": os.environ.get("DATABASE_ENGINE", "django.db.backends.sqlite3"),
30-
"NAME": os.environ.get("DATABASE_NAME", ":memory:"),
31-
"USER": os.environ.get("DATABASE_USER", ""),
32-
"PASSWORD": os.environ.get("DATABASE_PASSWORD", ""),
33-
"HOST": os.environ.get("DATABASE_HOST", ""),
34-
"PORT": os.environ.get("DATABASE_PORT", ""),
35-
"TEST": {"NAME": os.environ.get("DATABASE_NAME", "")},
36-
}
37-
}
38-
39-
# Set regular database name when a non-SQLite db is used
40-
if DATABASES["default"]["ENGINE"] != "django.db.backends.sqlite3":
41-
DATABASES["default"]["NAME"] = os.environ.get("DATABASE_NAME", "wagtail")
42-
43-
# Add extra options when mssql is used (on for example appveyor)
44-
if DATABASES["default"]["ENGINE"] == "sql_server.pyodbc":
45-
DATABASES["default"]["OPTIONS"] = {
46-
"driver": os.environ.get("DATABASE_DRIVER", "SQL Server Native Client 11.0"),
47-
"MARS_Connection": "True",
48-
"host_is_server": True, # Applies to FreeTDS driver only
49-
}
50-
51-
52-
# explicitly set charset / collation to utf8 on mysql
53-
if DATABASES["default"]["ENGINE"] == "django.db.backends.mysql":
54-
DATABASES["default"]["TEST"]["CHARSET"] = "utf8"
55-
DATABASES["default"]["TEST"]["COLLATION"] = "utf8_general_ci"
56-
28+
DATABASES = get_databases("wagtail")
5729

5830
SECRET_KEY = "not needed"
5931

django_mongodb_cli/utils.py

+11
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import click
2+
import django_mongodb_backend
23
import git
34
import os
45
import shutil
@@ -108,6 +109,16 @@ def get_management_command(command=None):
108109
return base_command
109110

110111

112+
def get_databases(app):
113+
"""Get the databases configuration for the specified app."""
114+
115+
DATABASE_URL = os.environ.get(
116+
"MONGODB_URI", f"mongodb://localhost:27017/{app}_tests"
117+
)
118+
DATABASES = {"default": django_mongodb_backend.parse_uri(DATABASE_URL)}
119+
return DATABASES
120+
121+
111122
def get_repos(pyproject_path):
112123
with open(pyproject_path, "r") as f:
113124
pyproject_data = toml.load(f)

0 commit comments

Comments
 (0)