Skip to content

Commit 0131747

Browse files
committed
Improve database configuration in tests
1 parent 3fef273 commit 0131747

File tree

7 files changed

+55
-189
lines changed

7 files changed

+55
-189
lines changed

docker/docker-compose.local.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ services:
1919
- 5432:5432
2020
environment:
2121
POSTGRES_HOST_AUTH_METHOD: trust
22+
TZ: UTC
2223
healthcheck:
2324
test: pg_isready -Upostgres
2425
interval: 1s
@@ -33,6 +34,7 @@ services:
3334
- 5433:5432
3435
environment:
3536
POSTGRES_HOST_AUTH_METHOD: trust
37+
TZ: UTC
3638
healthcheck:
3739
test: pg_isready -Upostgres
3840
interval: 1s

settings/dev.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
),
3939
),
4040
}
41-
DATABASE_ROUTERS = ["task_processor.routers.TaskProcessorRouter"]
4241
TASK_PROCESSOR_DATABASES = ["default"]
4342
INSTALLED_APPS = [
4443
"django.contrib.auth",

src/task_processor/routers.py

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from django.conf import settings
21
from django.db.models import Model
32

43

@@ -9,17 +8,10 @@ class TaskProcessorRouter:
98

109
route_app_labels = ["task_processor"]
1110

12-
@property
13-
def is_enabled(self) -> bool:
14-
return "task_processor" in settings.TASK_PROCESSOR_DATABASES
15-
1611
def db_for_read(self, model: type[Model], **hints: None) -> str | None:
1712
"""
1813
If enabled, route read operations to the task processor database
1914
"""
20-
if not self.is_enabled:
21-
return None
22-
2315
if model._meta.app_label in self.route_app_labels:
2416
return "task_processor"
2517

@@ -29,9 +21,6 @@ def db_for_write(self, model: type[Model], **hints: None) -> str | None:
2921
"""
3022
If enabled, route write operations to the task processor database
3123
"""
32-
if not self.is_enabled:
33-
return None
34-
3524
if model._meta.app_label in self.route_app_labels:
3625
return "task_processor"
3726

@@ -41,9 +30,6 @@ def allow_relation(self, obj1: Model, obj2: Model, **hints: None) -> bool | None
4130
"""
4231
If enabled, allow relations between task processor models
4332
"""
44-
if not self.is_enabled:
45-
return None
46-
4733
both_objects_from_task_processor = (
4834
obj1._meta.app_label in self.route_app_labels
4935
and obj2._meta.app_label in self.route_app_labels
@@ -72,9 +58,6 @@ def allow_migrate(
7258
- We want to make it easier to migrate to the new database, _or back_
7359
to a single database setup if needed. Running DDL consistently helps.
7460
"""
75-
if not self.is_enabled:
76-
return None
77-
7861
if app_label in self.route_app_labels:
7962
return db in ["default", "task_processor"]
8063

tests/unit/task_processor/conftest.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,34 @@
77
from task_processor.task_registry import RegisteredTask
88

99

10+
def pytest_generate_tests(metafunc: pytest.Metafunc) -> None:
11+
# Parametrize marked tests to run against both databases
12+
if marker := metafunc.definition.get_closest_marker("multi_database"):
13+
metafunc.parametrize(
14+
"database",
15+
[
16+
pytest.param(
17+
database,
18+
marks=pytest.mark.django_db(databases=[database], **marker.kwargs),
19+
)
20+
for database in ["default", "task_processor"]
21+
],
22+
indirect=True,
23+
)
24+
25+
26+
@pytest.fixture
27+
def database(
28+
request: pytest.FixtureRequest,
29+
settings: SettingsWrapper,
30+
) -> str:
31+
database: str = request.param
32+
settings.TASK_PROCESSOR_DATABASES = [database]
33+
if database == "task_processor":
34+
settings.DATABASE_ROUTERS = ["task_processor.routers.TaskProcessorRouter"]
35+
return database
36+
37+
1038
@pytest.fixture()
1139
def task_processor_mode(settings: SettingsWrapper) -> None:
1240
settings.TASK_PROCESSOR_MODE = True

0 commit comments

Comments
 (0)