|
6 | 6 | from datetime import datetime, timedelta
|
7 | 7 | from django.apps import apps
|
8 | 8 | from django.contrib.auth import get_user_model
|
| 9 | +from django.core.exceptions import ObjectDoesNotExist |
9 | 10 | from django.core.files.base import ContentFile
|
10 | 11 | from django.db import models
|
11 | 12 | from django.db.models.fields.proxy import OrderWrt
|
|
15 | 16 | from simple_history.models import HistoricalRecords, ModelChange
|
16 | 17 | from simple_history.signals import pre_create_historical_record
|
17 | 18 | from simple_history.tests.tests.utils import middleware_override_settings
|
| 19 | +from simple_history.utils import get_history_model_for_model |
18 | 20 | from simple_history.utils import update_change_reason
|
19 | 21 | from ..external.models import ExternalModel2, ExternalModel4
|
20 | 22 | from ..models import (
|
@@ -1219,3 +1221,91 @@ class Meta:
|
1219 | 1221 | "(AbstractModelWithInheritFalse) without "
|
1220 | 1222 | "inherit=True",
|
1221 | 1223 | )
|
| 1224 | + |
| 1225 | + |
| 1226 | +class MultiDBWithUsingTest(TestCase): |
| 1227 | + |
| 1228 | + """Asserts historical manager respects `using()` and the `using` |
| 1229 | + keyword argument in `save()`. |
| 1230 | + """ |
| 1231 | + |
| 1232 | + multi_db = True |
| 1233 | + db_name = "other" |
| 1234 | + |
| 1235 | + def test_multidb_with_using_not_on_default(self): |
| 1236 | + book = Book.objects.using(self.db_name).create(isbn="1-84356-028-1") |
| 1237 | + self.assertRaises(ObjectDoesNotExist, book.history.get, isbn="1-84356-028-1") |
| 1238 | + |
| 1239 | + def test_multidb_with_using_is_on_dbtwo(self): |
| 1240 | + book = Book.objects.using(self.db_name).create(isbn="1-84356-028-1") |
| 1241 | + try: |
| 1242 | + book.history.using(self.db_name).get(isbn="1-84356-028-1") |
| 1243 | + except ObjectDoesNotExist: |
| 1244 | + self.fail("ObjectDoesNotExist unexpectedly raised.") |
| 1245 | + |
| 1246 | + def test_multidb_with_using_and_fk_not_on_default(self): |
| 1247 | + book = Book.objects.using(self.db_name).create(isbn="1-84356-028-1") |
| 1248 | + library = Library.objects.using(self.db_name).create(book=book) |
| 1249 | + self.assertRaises(ObjectDoesNotExist, library.history.get, book=book) |
| 1250 | + |
| 1251 | + def test_multidb_with_using_and_fk_on_dbtwo(self): |
| 1252 | + book = Book.objects.using(self.db_name).create(isbn="1-84356-028-1") |
| 1253 | + library = Library.objects.using(self.db_name).create(book=book) |
| 1254 | + try: |
| 1255 | + library.history.using(self.db_name).get(book=book) |
| 1256 | + except ObjectDoesNotExist: |
| 1257 | + self.fail("ObjectDoesNotExist unexpectedly raised.") |
| 1258 | + |
| 1259 | + def test_multidb_with_using_keyword_in_save_not_on_default(self): |
| 1260 | + book = Book(isbn="1-84356-028-1") |
| 1261 | + book.save(using=self.db_name) |
| 1262 | + self.assertRaises(ObjectDoesNotExist, book.history.get, isbn="1-84356-028-1") |
| 1263 | + |
| 1264 | + def test_multidb_with_using_keyword_in_save_on_dbtwo(self): |
| 1265 | + book = Book(isbn="1-84356-028-1") |
| 1266 | + book.save(using=self.db_name) |
| 1267 | + try: |
| 1268 | + book.history.using(self.db_name).get(isbn="1-84356-028-1") |
| 1269 | + except ObjectDoesNotExist: |
| 1270 | + self.fail("ObjectDoesNotExist unexpectedly raised.") |
| 1271 | + |
| 1272 | + def test_multidb_with_using_keyword_in_save_with_fk(self): |
| 1273 | + book = Book(isbn="1-84356-028-1") |
| 1274 | + book.save(using=self.db_name) |
| 1275 | + library = Library(book=book) |
| 1276 | + library.save(using=self.db_name) |
| 1277 | + # assert not created on default |
| 1278 | + self.assertRaises(ObjectDoesNotExist, library.history.get, book=book) |
| 1279 | + # assert created on dbtwo |
| 1280 | + try: |
| 1281 | + library.history.using(self.db_name).get(book=book) |
| 1282 | + except ObjectDoesNotExist: |
| 1283 | + self.fail("ObjectDoesNotExist unexpectedly raised.") |
| 1284 | + |
| 1285 | + def test_multidb_with_using_keyword_in_save_and_update(self): |
| 1286 | + book = Book.objects.using(self.db_name).create(isbn="1-84356-028-1") |
| 1287 | + book.save(using=self.db_name) |
| 1288 | + self.assertEqual( |
| 1289 | + ["+", "~"], |
| 1290 | + [ |
| 1291 | + obj.history_type |
| 1292 | + for obj in book.history.using(self.db_name) |
| 1293 | + .all() |
| 1294 | + .order_by("history_date") |
| 1295 | + ], |
| 1296 | + ) |
| 1297 | + |
| 1298 | + def test_multidb_with_using_keyword_in_save_and_delete(self): |
| 1299 | + HistoricalBook = get_history_model_for_model(Book) |
| 1300 | + book = Book.objects.using(self.db_name).create(isbn="1-84356-028-1") |
| 1301 | + book.save(using=self.db_name) |
| 1302 | + book.delete(using=self.db_name) |
| 1303 | + self.assertEqual( |
| 1304 | + ["+", "~", "-"], |
| 1305 | + [ |
| 1306 | + obj.history_type |
| 1307 | + for obj in HistoricalBook.objects.using(self.db_name) |
| 1308 | + .all() |
| 1309 | + .order_by("history_date") |
| 1310 | + ], |
| 1311 | + ) |
0 commit comments