| 1 |
from django.core.management import call_command |
| 2 |
from django.test import TestCase, TransactionTestCase |
| 3 |
|
| 4 |
from .models import Book |
| 5 |
|
| 6 |
|
| 7 |
class MigrationDataPersistenceTestCase(TransactionTestCase): |
| 8 |
""" |
| 9 |
Data loaded in migrations is available if |
| 10 |
TransactionTestCase.serialized_rollback = True. |
| 11 |
""" |
| 12 |
|
| 13 |
available_apps = ["migration_test_data_persistence"] |
| 14 |
serialized_rollback = True |
| 15 |
|
| 16 |
def test_persistence(self): |
| 17 |
self.assertEqual( |
| 18 |
Book.objects.count(), |
| 19 |
1, |
| 20 |
) |
| 21 |
|
| 22 |
|
| 23 |
class MigrationDataPersistenceClassSetup(TransactionTestCase): |
| 24 |
""" |
| 25 |
Data loaded in migrations is available during class setup if |
| 26 |
TransactionTestCase.serialized_rollback = True. |
| 27 |
""" |
| 28 |
|
| 29 |
available_apps = ["migration_test_data_persistence"] |
| 30 |
serialized_rollback = True |
| 31 |
|
| 32 |
@classmethod |
| 33 |
def setUpClass(cls): |
| 34 |
# Simulate another TransactionTestCase having just torn down. |
| 35 |
call_command("flush", verbosity=0, interactive=False, allow_cascade=True) |
| 36 |
super().setUpClass() |
| 37 |
cls.book = Book.objects.first() |
| 38 |
|
| 39 |
def test_data_available_in_class_setup(self): |
| 40 |
self.assertIsInstance(self.book, Book) |
| 41 |
|
| 42 |
|
| 43 |
class MigrationDataNormalPersistenceTestCase(TestCase): |
| 44 |
""" |
| 45 |
Data loaded in migrations is available on TestCase |
| 46 |
""" |
| 47 |
|
| 48 |
def test_persistence(self): |
| 49 |
self.assertEqual( |
| 50 |
Book.objects.count(), |
| 51 |
1, |
| 52 |
) |