|
1 | 1 | from enum import Enum
|
| 2 | +from typing import Type |
2 | 3 |
|
3 |
| -import django |
4 | 4 | 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 |
7 | 7 |
|
8 | 8 | request_middleware = "simple_history.middleware.HistoryRequestMiddleware"
|
9 | 9 |
|
|
14 | 14 | }
|
15 | 15 |
|
16 | 16 |
|
| 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 | + |
17 | 38 | class TestDbRouter:
|
18 | 39 | def db_for_read(self, model, **hints):
|
19 | 40 | if model._meta.app_label == "external":
|
@@ -46,23 +67,35 @@ def allow_migrate(self, db, app_label, model_name=None, **hints):
|
46 | 67 |
|
47 | 68 | class TestModelWithHistoryInDifferentDbRouter:
|
48 | 69 | def db_for_read(self, model, **hints):
|
| 70 | + # Avoids circular importing |
| 71 | + from ..models import HistoricalModelWithHistoryInDifferentDb |
| 72 | + |
49 | 73 | if model == HistoricalModelWithHistoryInDifferentDb:
|
50 | 74 | return OTHER_DB_NAME
|
51 | 75 | return None
|
52 | 76 |
|
53 | 77 | def db_for_write(self, model, **hints):
|
| 78 | + # Avoids circular importing |
| 79 | + from ..models import HistoricalModelWithHistoryInDifferentDb |
| 80 | + |
54 | 81 | if model == HistoricalModelWithHistoryInDifferentDb:
|
55 | 82 | return OTHER_DB_NAME
|
56 | 83 | return None
|
57 | 84 |
|
58 | 85 | def allow_relation(self, obj1, obj2, **hints):
|
| 86 | + # Avoids circular importing |
| 87 | + from ..models import HistoricalModelWithHistoryInDifferentDb |
| 88 | + |
59 | 89 | if isinstance(obj1, HistoricalModelWithHistoryInDifferentDb) or isinstance(
|
60 | 90 | obj2, HistoricalModelWithHistoryInDifferentDb
|
61 | 91 | ):
|
62 | 92 | return False
|
63 | 93 | return None
|
64 | 94 |
|
65 | 95 | def allow_migrate(self, db, app_label, model_name=None, **hints):
|
| 96 | + # Avoids circular importing |
| 97 | + from ..models import HistoricalModelWithHistoryInDifferentDb |
| 98 | + |
66 | 99 | if model_name == HistoricalModelWithHistoryInDifferentDb._meta.model_name:
|
67 | 100 | return db == OTHER_DB_NAME
|
68 | 101 | return None
|
|
0 commit comments