Refs #31051 -- Added test for loaddata/dumpdata with circular references without natural keys.

This commit is contained in:
Matthijs Kooijman 2020-04-07 11:28:25 +02:00 committed by Mariusz Felisiak
parent cf21fc9bf0
commit 481d8fc324
3 changed files with 46 additions and 2 deletions

View File

@ -0,0 +1,18 @@
[
{
"model": "fixtures.circulara",
"pk": 1,
"fields": {
"key": "x",
"obj": 1
}
},
{
"model": "fixtures.circularb",
"pk": 1,
"fields": {
"key": "y",
"obj": 1
}
}
]

View File

@ -134,3 +134,13 @@ class NaturalKeyThing(models.Model):
def __str__(self):
return self.key
class CircularA(models.Model):
key = models.CharField(max_length=3, unique=True)
obj = models.ForeignKey('CircularB', models.SET_NULL, null=True)
class CircularB(models.Model):
key = models.CharField(max_length=3, unique=True)
obj = models.ForeignKey('CircularA', models.SET_NULL, null=True)

View File

@ -17,8 +17,8 @@ from django.db import IntegrityError, connection
from django.test import TestCase, TransactionTestCase, skipUnlessDBFeature
from .models import (
Article, Category, NaturalKeyThing, PrimaryKeyUUIDModel, ProxySpy, Spy,
Tag, Visa,
Article, Category, CircularA, CircularB, NaturalKeyThing,
PrimaryKeyUUIDModel, ProxySpy, Spy, Tag, Visa,
)
@ -804,3 +804,19 @@ class ForwardReferenceTests(TestCase):
t1.other_things.order_by('key'),
['<NaturalKeyThing: t2>', '<NaturalKeyThing: t3>']
)
class CircularReferenceTests(DumpDataAssertMixin, TestCase):
def test_circular_reference(self):
management.call_command('loaddata', 'circular_reference.json', verbosity=0)
obj_a = CircularA.objects.get()
obj_b = CircularB.objects.get()
self.assertEqual(obj_a.obj, obj_b)
self.assertEqual(obj_b.obj, obj_a)
self._dumpdata_assert(
['fixtures'],
'[{"model": "fixtures.circulara", "pk": 1, '
'"fields": {"key": "x", "obj": 1}}, '
'{"model": "fixtures.circularb", "pk": 1, '
'"fields": {"key": "y", "obj": 1}}]',
)