From 26799c650389bec255df581eb400730916919aa1 Mon Sep 17 00:00:00 2001 From: Matthijs Kooijman Date: Tue, 7 Apr 2020 12:14:45 +0200 Subject: [PATCH] Refs #26291 -- Added tests for dumpdata/loaddata with forward references without natural keys. --- .../fixtures/forward_reference_fk.json | 18 +++++++++++ .../fixtures/forward_reference_m2m.json | 24 ++++++++++++++ tests/fixtures/tests.py | 32 +++++++++++++++++++ 3 files changed, 74 insertions(+) create mode 100644 tests/fixtures/fixtures/forward_reference_fk.json create mode 100644 tests/fixtures/fixtures/forward_reference_m2m.json diff --git a/tests/fixtures/fixtures/forward_reference_fk.json b/tests/fixtures/fixtures/forward_reference_fk.json new file mode 100644 index 0000000000..c553d2b487 --- /dev/null +++ b/tests/fixtures/fixtures/forward_reference_fk.json @@ -0,0 +1,18 @@ +[ + { + "model": "fixtures.naturalkeything", + "pk": 1, + "fields": { + "key": "t1", + "other_thing": 2 + } + }, + { + "model": "fixtures.naturalkeything", + "pk": 2, + "fields": { + "key": "t2", + "other_thing": 1 + } + } +] diff --git a/tests/fixtures/fixtures/forward_reference_m2m.json b/tests/fixtures/fixtures/forward_reference_m2m.json new file mode 100644 index 0000000000..927bac62b6 --- /dev/null +++ b/tests/fixtures/fixtures/forward_reference_m2m.json @@ -0,0 +1,24 @@ +[ + { + "model": "fixtures.naturalkeything", + "pk": 1, + "fields": { + "key": "t1", + "other_things": [2, 3] + } + }, + { + "model": "fixtures.naturalkeything", + "pk": 2, + "fields": { + "key": "t2" + } + }, + { + "model": "fixtures.naturalkeything", + "pk": 3, + "fields": { + "key": "t3" + } + } +] diff --git a/tests/fixtures/tests.py b/tests/fixtures/tests.py index a1e9b8ccaf..2ed17d57be 100644 --- a/tests/fixtures/tests.py +++ b/tests/fixtures/tests.py @@ -789,6 +789,20 @@ class FixtureTransactionTests(DumpDataAssertMixin, TransactionTestCase): class ForwardReferenceTests(DumpDataAssertMixin, TestCase): + def test_forward_reference_fk(self): + management.call_command('loaddata', 'forward_reference_fk.json', verbosity=0) + self.assertEqual(NaturalKeyThing.objects.count(), 2) + t1, t2 = NaturalKeyThing.objects.all() + self.assertEqual(t1.other_thing, t2) + self.assertEqual(t2.other_thing, t1) + self._dumpdata_assert( + ['fixtures'], + '[{"model": "fixtures.naturalkeything", "pk": 1, ' + '"fields": {"key": "t1", "other_thing": 2, "other_things": []}}, ' + '{"model": "fixtures.naturalkeything", "pk": 2, ' + '"fields": {"key": "t2", "other_thing": 1, "other_things": []}}]', + ) + def test_forward_reference_fk_natural_key(self): management.call_command( 'loaddata', @@ -809,6 +823,24 @@ class ForwardReferenceTests(DumpDataAssertMixin, TestCase): natural_foreign_keys=True, ) + def test_forward_reference_m2m(self): + management.call_command('loaddata', 'forward_reference_m2m.json', verbosity=0) + self.assertEqual(NaturalKeyThing.objects.count(), 3) + t1 = NaturalKeyThing.objects.get_by_natural_key('t1') + self.assertQuerysetEqual( + t1.other_things.order_by('key'), + ['', ''] + ) + self._dumpdata_assert( + ['fixtures'], + '[{"model": "fixtures.naturalkeything", "pk": 1, ' + '"fields": {"key": "t1", "other_thing": null, "other_things": [2, 3]}}, ' + '{"model": "fixtures.naturalkeything", "pk": 2, ' + '"fields": {"key": "t2", "other_thing": null, "other_things": []}}, ' + '{"model": "fixtures.naturalkeything", "pk": 3, ' + '"fields": {"key": "t3", "other_thing": null, "other_things": []}}]', + ) + def test_forward_reference_m2m_natural_key(self): management.call_command( 'loaddata',