diff --git a/tests/fixtures/fixtures/circular_reference.json b/tests/fixtures/fixtures/circular_reference.json new file mode 100644 index 0000000000..0656c30c93 --- /dev/null +++ b/tests/fixtures/fixtures/circular_reference.json @@ -0,0 +1,18 @@ +[ + { + "model": "fixtures.circulara", + "pk": 1, + "fields": { + "key": "x", + "obj": 1 + } + }, + { + "model": "fixtures.circularb", + "pk": 1, + "fields": { + "key": "y", + "obj": 1 + } + } +] diff --git a/tests/fixtures/models.py b/tests/fixtures/models.py index 2ecb56537f..db7d7fbd7a 100644 --- a/tests/fixtures/models.py +++ b/tests/fixtures/models.py @@ -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) diff --git a/tests/fixtures/tests.py b/tests/fixtures/tests.py index 0027c62c20..cf05717277 100644 --- a/tests/fixtures/tests.py +++ b/tests/fixtures/tests.py @@ -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'), ['', ''] ) + + +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}}]', + )