|
1 | 1 | from __future__ import unicode_literals
|
2 | 2 |
|
3 | 3 | import unittest
|
4 |
| - |
5 |
| -import django |
6 | 4 | import uuid
|
7 | 5 | import warnings
|
8 | 6 | from datetime import datetime, timedelta
|
| 7 | + |
| 8 | +import django |
9 | 9 | from django.apps import apps
|
10 | 10 | from django.contrib.auth import get_user_model
|
11 | 11 | from django.core.exceptions import ObjectDoesNotExist
|
|
18 | 18 | from simple_history import register
|
19 | 19 | from simple_history.exceptions import RelatedNameConflictError
|
20 | 20 | from simple_history.models import HistoricalRecords, ModelChange
|
21 |
| -from simple_history.signals import ( |
22 |
| - pre_create_historical_record, |
23 |
| - post_create_historical_record, |
24 |
| -) |
| 21 | +from simple_history.signals import pre_create_historical_record |
25 | 22 | from simple_history.tests.custom_user.models import CustomUser
|
26 | 23 | from simple_history.tests.tests.utils import (
|
27 | 24 | database_router_override_settings,
|
28 |
| - middleware_override_settings, |
29 | 25 | database_router_override_settings_history_in_diff_db,
|
| 26 | + middleware_override_settings, |
30 | 27 | )
|
31 | 28 | from simple_history.utils import get_history_model_for_model
|
32 | 29 | from simple_history.utils import update_change_reason
|
33 | 30 | from ..external.models import (
|
34 | 31 | ExternalModel,
|
35 |
| - ExternalModelWithCustomUserIdField, |
36 | 32 | ExternalModelRegistered,
|
| 33 | + ExternalModelWithCustomUserIdField, |
37 | 34 | )
|
38 | 35 | from ..models import (
|
39 | 36 | AbstractBase,
|
|
66 | 63 | HistoricalPollWithHistoricalIPAddress,
|
67 | 64 | HistoricalState,
|
68 | 65 | Library,
|
| 66 | + ModelWithFkToModelWithHistoryUsingBaseModelDb, |
69 | 67 | ModelWithHistoryInDifferentDb,
|
| 68 | + ModelWithHistoryUsingBaseModelDb, |
70 | 69 | MultiOneToOne,
|
71 | 70 | Person,
|
72 | 71 | Place,
|
@@ -1262,78 +1261,98 @@ class MultiDBWithUsingTest(TestCase):
|
1262 | 1261 | db_name = "other"
|
1263 | 1262 |
|
1264 | 1263 | def test_multidb_with_using_not_on_default(self):
|
1265 |
| - book = Book.objects.using(self.db_name).create(isbn="1-84356-028-1") |
1266 |
| - self.assertRaises(ObjectDoesNotExist, book.history.get, isbn="1-84356-028-1") |
| 1264 | + model = ModelWithHistoryUsingBaseModelDb.objects.using(self.db_name).create( |
| 1265 | + name="1-84356-028-1" |
| 1266 | + ) |
| 1267 | + self.assertRaises(ObjectDoesNotExist, model.history.get, name="1-84356-028-1") |
1267 | 1268 |
|
1268 | 1269 | def test_multidb_with_using_is_on_dbtwo(self):
|
1269 |
| - book = Book.objects.using(self.db_name).create(isbn="1-84356-028-1") |
| 1270 | + model = ModelWithHistoryUsingBaseModelDb.objects.using(self.db_name).create( |
| 1271 | + name="1-84356-028-1" |
| 1272 | + ) |
1270 | 1273 | try:
|
1271 |
| - book.history.using(self.db_name).get(isbn="1-84356-028-1") |
| 1274 | + model.history.using(self.db_name).get(name="1-84356-028-1") |
1272 | 1275 | except ObjectDoesNotExist:
|
1273 | 1276 | self.fail("ObjectDoesNotExist unexpectedly raised.")
|
1274 | 1277 |
|
1275 | 1278 | def test_multidb_with_using_and_fk_not_on_default(self):
|
1276 |
| - book = Book.objects.using(self.db_name).create(isbn="1-84356-028-1") |
1277 |
| - library = Library.objects.using(self.db_name).create(book=book) |
1278 |
| - self.assertRaises(ObjectDoesNotExist, library.history.get, book=book) |
| 1279 | + model = ModelWithHistoryUsingBaseModelDb.objects.using(self.db_name).create( |
| 1280 | + name="1-84356-028-1" |
| 1281 | + ) |
| 1282 | + parent_model = ModelWithFkToModelWithHistoryUsingBaseModelDb.objects.using( |
| 1283 | + self.db_name |
| 1284 | + ).create(fk=model) |
| 1285 | + self.assertRaises(ObjectDoesNotExist, parent_model.history.get, fk=model) |
1279 | 1286 |
|
1280 | 1287 | def test_multidb_with_using_and_fk_on_dbtwo(self):
|
1281 |
| - book = Book.objects.using(self.db_name).create(isbn="1-84356-028-1") |
1282 |
| - library = Library.objects.using(self.db_name).create(book=book) |
| 1288 | + model = ModelWithHistoryUsingBaseModelDb.objects.using(self.db_name).create( |
| 1289 | + name="1-84356-028-1" |
| 1290 | + ) |
| 1291 | + parent_model = ModelWithFkToModelWithHistoryUsingBaseModelDb.objects.using( |
| 1292 | + self.db_name |
| 1293 | + ).create(fk=model) |
1283 | 1294 | try:
|
1284 |
| - library.history.using(self.db_name).get(book=book) |
| 1295 | + parent_model.history.using(self.db_name).get(fk=model) |
1285 | 1296 | except ObjectDoesNotExist:
|
1286 | 1297 | self.fail("ObjectDoesNotExist unexpectedly raised.")
|
1287 | 1298 |
|
1288 | 1299 | def test_multidb_with_using_keyword_in_save_not_on_default(self):
|
1289 |
| - book = Book(isbn="1-84356-028-1") |
1290 |
| - book.save(using=self.db_name) |
1291 |
| - self.assertRaises(ObjectDoesNotExist, book.history.get, isbn="1-84356-028-1") |
| 1300 | + model = ModelWithHistoryUsingBaseModelDb(name="1-84356-028-1") |
| 1301 | + model.save(using=self.db_name) |
| 1302 | + self.assertRaises(ObjectDoesNotExist, model.history.get, name="1-84356-028-1") |
1292 | 1303 |
|
1293 | 1304 | def test_multidb_with_using_keyword_in_save_on_dbtwo(self):
|
1294 |
| - book = Book(isbn="1-84356-028-1") |
1295 |
| - book.save(using=self.db_name) |
| 1305 | + model = ModelWithHistoryUsingBaseModelDb(name="1-84356-028-1") |
| 1306 | + model.save(using=self.db_name) |
1296 | 1307 | try:
|
1297 |
| - book.history.using(self.db_name).get(isbn="1-84356-028-1") |
| 1308 | + model.history.using(self.db_name).get(name="1-84356-028-1") |
1298 | 1309 | except ObjectDoesNotExist:
|
1299 | 1310 | self.fail("ObjectDoesNotExist unexpectedly raised.")
|
1300 | 1311 |
|
1301 | 1312 | def test_multidb_with_using_keyword_in_save_with_fk(self):
|
1302 |
| - book = Book(isbn="1-84356-028-1") |
1303 |
| - book.save(using=self.db_name) |
1304 |
| - library = Library(book=book) |
1305 |
| - library.save(using=self.db_name) |
| 1313 | + model = ModelWithHistoryUsingBaseModelDb(name="1-84356-028-1") |
| 1314 | + model.save(using=self.db_name) |
| 1315 | + parent_model = ModelWithFkToModelWithHistoryUsingBaseModelDb(fk=model) |
| 1316 | + parent_model.save(using=self.db_name) |
1306 | 1317 | # assert not created on default
|
1307 |
| - self.assertRaises(ObjectDoesNotExist, library.history.get, book=book) |
| 1318 | + self.assertRaises(ObjectDoesNotExist, parent_model.history.get, fk=model) |
1308 | 1319 | # assert created on dbtwo
|
1309 | 1320 | try:
|
1310 |
| - library.history.using(self.db_name).get(book=book) |
| 1321 | + parent_model.history.using(self.db_name).get(fk=model) |
1311 | 1322 | except ObjectDoesNotExist:
|
1312 | 1323 | self.fail("ObjectDoesNotExist unexpectedly raised.")
|
1313 | 1324 |
|
1314 | 1325 | def test_multidb_with_using_keyword_in_save_and_update(self):
|
1315 |
| - book = Book.objects.using(self.db_name).create(isbn="1-84356-028-1") |
1316 |
| - book.save(using=self.db_name) |
| 1326 | + model = ModelWithHistoryUsingBaseModelDb.objects.using(self.db_name).create( |
| 1327 | + name="1-84356-028-1" |
| 1328 | + ) |
| 1329 | + model.save(using=self.db_name) |
1317 | 1330 | self.assertEqual(
|
1318 | 1331 | ["+", "~"],
|
1319 | 1332 | [
|
1320 | 1333 | obj.history_type
|
1321 |
| - for obj in book.history.using(self.db_name) |
| 1334 | + for obj in model.history.using(self.db_name) |
1322 | 1335 | .all()
|
1323 | 1336 | .order_by("history_date")
|
1324 | 1337 | ],
|
1325 | 1338 | )
|
1326 | 1339 |
|
1327 | 1340 | def test_multidb_with_using_keyword_in_save_and_delete(self):
|
1328 |
| - HistoricalBook = get_history_model_for_model(Book) |
1329 |
| - book = Book.objects.using(self.db_name).create(isbn="1-84356-028-1") |
1330 |
| - book.save(using=self.db_name) |
1331 |
| - book.delete(using=self.db_name) |
| 1341 | + HistoricalModelWithHistoryUseBaseModelDb = get_history_model_for_model( |
| 1342 | + ModelWithHistoryUsingBaseModelDb |
| 1343 | + ) |
| 1344 | + model = ModelWithHistoryUsingBaseModelDb.objects.using(self.db_name).create( |
| 1345 | + name="1-84356-028-1" |
| 1346 | + ) |
| 1347 | + model.save(using=self.db_name) |
| 1348 | + model.delete(using=self.db_name) |
1332 | 1349 | self.assertEqual(
|
1333 | 1350 | ["+", "~", "-"],
|
1334 | 1351 | [
|
1335 | 1352 | obj.history_type
|
1336 |
| - for obj in HistoricalBook.objects.using(self.db_name) |
| 1353 | + for obj in HistoricalModelWithHistoryUseBaseModelDb.objects.using( |
| 1354 | + self.db_name |
| 1355 | + ) |
1337 | 1356 | .all()
|
1338 | 1357 | .order_by("history_date")
|
1339 | 1358 | ],
|
|
0 commit comments