1
0
mirror of https://github.com/django/django.git synced 2025-10-24 06:06:09 +00:00

Refs #31051 -- Fixed reloading the database with circular related objects and natural keys for tests.

Made deserialize_db_from_string() do not sort dependencies.

deserialize_db_from_string() doesn't use natural keys, so there is no
need to sort dependencies in serialize_db_to_string(). Moreover,
sorting models cause issues for circular dependencies.
This commit is contained in:
Matthijs Kooijman
2019-12-02 01:16:32 +01:00
committed by Mariusz Felisiak
parent 12e6f573ad
commit 289d0ec6fd
3 changed files with 46 additions and 5 deletions

View File

@@ -7,7 +7,9 @@ from django.db.backends.base.creation import (
)
from django.test import SimpleTestCase, TransactionTestCase
from ..models import Object, ObjectReference, ObjectSelfReference
from ..models import (
CircularA, CircularB, Object, ObjectReference, ObjectSelfReference,
)
def get_connection_copy():
@@ -123,3 +125,26 @@ class TestDeserializeDbFromString(TransactionTestCase):
obj_2 = ObjectSelfReference.objects.get(key='Y')
self.assertEqual(obj_1.obj, obj_2)
self.assertEqual(obj_2.obj, obj_1)
def test_circular_reference_with_natural_key(self):
# serialize_db_to_string() and deserialize_db_from_string() handles
# circular references for models with natural keys.
obj_a = CircularA.objects.create(key='A')
obj_b = CircularB.objects.create(key='B', obj=obj_a)
obj_a.obj = obj_b
obj_a.save()
# Serialize objects.
with mock.patch('django.db.migrations.loader.MigrationLoader') as loader:
# serialize_db_to_string() serializes only migrated apps, so mark
# the backends app as migrated.
loader_instance = loader.return_value
loader_instance.migrated_apps = {'backends'}
data = connection.creation.serialize_db_to_string()
CircularA.objects.all().delete()
CircularB.objects.all().delete()
# Deserialize objects.
connection.creation.deserialize_db_from_string(data)
obj_a = CircularA.objects.get()
obj_b = CircularB.objects.get()
self.assertEqual(obj_a.obj, obj_b)
self.assertEqual(obj_b.obj, obj_a)