Skip to content

Commit 7398af3

Browse files
committed
Moved assertRecordValues() to new HistoricalTestCase
This makes the method available for use in other test cases.
1 parent e5c8a64 commit 7398af3

File tree

2 files changed

+43
-17
lines changed

2 files changed

+43
-17
lines changed

simple_history/tests/tests/test_models.py

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,6 @@
3030
pre_create_historical_m2m_records,
3131
pre_create_historical_record,
3232
)
33-
from simple_history.tests.tests.utils import (
34-
database_router_override_settings,
35-
database_router_override_settings_history_in_diff_db,
36-
middleware_override_settings,
37-
)
3833
from simple_history.utils import get_history_model_for_model, update_change_reason
3934

4035
from ..external.models import (
@@ -126,6 +121,12 @@
126121
UUIDModel,
127122
WaterLevel,
128123
)
124+
from .utils import (
125+
HistoricalTestCase,
126+
database_router_override_settings,
127+
database_router_override_settings_history_in_diff_db,
128+
middleware_override_settings,
129+
)
129130

130131
get_model = apps.get_model
131132
User = get_user_model()
@@ -140,18 +141,10 @@ def get_fake_file(filename):
140141
return fake_file
141142

142143

143-
class HistoricalRecordsTest(TestCase):
144+
class HistoricalRecordsTest(HistoricalTestCase):
144145
def assertDatetimesEqual(self, time1, time2):
145146
self.assertAlmostEqual(time1, time2, delta=timedelta(seconds=2))
146147

147-
def assertRecordValues(self, record, klass, values_dict):
148-
for key, value in values_dict.items():
149-
self.assertEqual(getattr(record, key), value)
150-
self.assertEqual(record.history_object.__class__, klass)
151-
for key, value in values_dict.items():
152-
if key not in ["history_type", "history_change_reason"]:
153-
self.assertEqual(getattr(record.history_object, key), value)
154-
155148
def test_create(self):
156149
p = Poll(question="what's up?", pub_date=today)
157150
p.save()

simple_history/tests/tests/utils.py

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
from enum import Enum
2+
from typing import Type
23

3-
import django
44
from django.conf import settings
5-
6-
from simple_history.tests.models import HistoricalModelWithHistoryInDifferentDb
5+
from django.db.models import Model
6+
from django.test import TestCase
77

88
request_middleware = "simple_history.middleware.HistoryRequestMiddleware"
99

@@ -14,6 +14,27 @@
1414
}
1515

1616

17+
class HistoricalTestCase(TestCase):
18+
def assertRecordValues(self, record, klass: Type[Model], values_dict: dict):
19+
"""
20+
Fail if ``record`` doesn't contain the field values in ``values_dict``.
21+
``record.history_object`` is also checked.
22+
History-tracked fields in ``record`` that are not in ``values_dict``, are not
23+
checked.
24+
25+
:param record: A historical record.
26+
:param klass: The type of the history-tracked class of ``record``.
27+
:param values_dict: Field names of ``record`` mapped to their expected values.
28+
"""
29+
for key, value in values_dict.items():
30+
self.assertEqual(getattr(record, key), value)
31+
32+
self.assertEqual(record.history_object.__class__, klass)
33+
for key, value in values_dict.items():
34+
if key not in ("history_type", "history_change_reason"):
35+
self.assertEqual(getattr(record.history_object, key), value)
36+
37+
1738
class TestDbRouter:
1839
def db_for_read(self, model, **hints):
1940
if model._meta.app_label == "external":
@@ -46,23 +67,35 @@ def allow_migrate(self, db, app_label, model_name=None, **hints):
4667

4768
class TestModelWithHistoryInDifferentDbRouter:
4869
def db_for_read(self, model, **hints):
70+
# Avoids circular importing
71+
from ..models import HistoricalModelWithHistoryInDifferentDb
72+
4973
if model == HistoricalModelWithHistoryInDifferentDb:
5074
return OTHER_DB_NAME
5175
return None
5276

5377
def db_for_write(self, model, **hints):
78+
# Avoids circular importing
79+
from ..models import HistoricalModelWithHistoryInDifferentDb
80+
5481
if model == HistoricalModelWithHistoryInDifferentDb:
5582
return OTHER_DB_NAME
5683
return None
5784

5885
def allow_relation(self, obj1, obj2, **hints):
86+
# Avoids circular importing
87+
from ..models import HistoricalModelWithHistoryInDifferentDb
88+
5989
if isinstance(obj1, HistoricalModelWithHistoryInDifferentDb) or isinstance(
6090
obj2, HistoricalModelWithHistoryInDifferentDb
6191
):
6292
return False
6393
return None
6494

6595
def allow_migrate(self, db, app_label, model_name=None, **hints):
96+
# Avoids circular importing
97+
from ..models import HistoricalModelWithHistoryInDifferentDb
98+
6699
if model_name == HistoricalModelWithHistoryInDifferentDb._meta.model_name:
67100
return db == OTHER_DB_NAME
68101
return None

0 commit comments

Comments
 (0)