Refs #26291 -- Added tests for dumpdata/loaddata with forward references without natural keys.

This commit is contained in:
Matthijs Kooijman 2020-04-07 12:14:45 +02:00 committed by Mariusz Felisiak
parent fca36f3c98
commit 26799c6503
3 changed files with 74 additions and 0 deletions

View File

@ -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
}
}
]

View File

@ -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"
}
}
]

View File

@ -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'),
['<NaturalKeyThing: t2>', '<NaturalKeyThing: t3>']
)
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',