Refs #26552 -- Added test for reloading the database with self-referential objects.
This commit is contained in:
parent
1fe3e24264
commit
12e6f573ad
|
@ -7,7 +7,7 @@ from django.db.backends.base.creation import (
|
||||||
)
|
)
|
||||||
from django.test import SimpleTestCase, TransactionTestCase
|
from django.test import SimpleTestCase, TransactionTestCase
|
||||||
|
|
||||||
from ..models import Object, ObjectReference
|
from ..models import Object, ObjectReference, ObjectSelfReference
|
||||||
|
|
||||||
|
|
||||||
def get_connection_copy():
|
def get_connection_copy():
|
||||||
|
@ -101,3 +101,25 @@ class TestDeserializeDbFromString(TransactionTestCase):
|
||||||
obj_ref = ObjectReference.objects.get()
|
obj_ref = ObjectReference.objects.get()
|
||||||
self.assertEqual(obj.obj_ref, obj_ref)
|
self.assertEqual(obj.obj_ref, obj_ref)
|
||||||
self.assertEqual(obj_ref.obj, obj)
|
self.assertEqual(obj_ref.obj, obj)
|
||||||
|
|
||||||
|
def test_self_reference(self):
|
||||||
|
# serialize_db_to_string() and deserialize_db_from_string() handles
|
||||||
|
# self references.
|
||||||
|
obj_1 = ObjectSelfReference.objects.create(key='X')
|
||||||
|
obj_2 = ObjectSelfReference.objects.create(key='Y', obj=obj_1)
|
||||||
|
obj_1.obj = obj_2
|
||||||
|
obj_1.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()
|
||||||
|
ObjectSelfReference.objects.all().delete()
|
||||||
|
# Deserialize objects.
|
||||||
|
connection.creation.deserialize_db_from_string(data)
|
||||||
|
obj_1 = ObjectSelfReference.objects.get(key='X')
|
||||||
|
obj_2 = ObjectSelfReference.objects.get(key='Y')
|
||||||
|
self.assertEqual(obj_1.obj, obj_2)
|
||||||
|
self.assertEqual(obj_2.obj, obj_1)
|
||||||
|
|
|
@ -102,6 +102,11 @@ class ObjectReference(models.Model):
|
||||||
return str(self.obj_id)
|
return str(self.obj_id)
|
||||||
|
|
||||||
|
|
||||||
|
class ObjectSelfReference(models.Model):
|
||||||
|
key = models.CharField(max_length=3, unique=True)
|
||||||
|
obj = models.ForeignKey('ObjectSelfReference', models.SET_NULL, null=True)
|
||||||
|
|
||||||
|
|
||||||
class RawData(models.Model):
|
class RawData(models.Model):
|
||||||
raw_data = models.BinaryField()
|
raw_data = models.BinaryField()
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue