From f92c5b28ade6d01f0b52fe59bbc8cdbf44080e92 Mon Sep 17 00:00:00 2001 From: Trey Hunner Date: Wed, 14 Nov 2012 13:39:54 -0800 Subject: [PATCH] Add test for creating a reverse fk relation --- rest_framework/tests/nested_relations.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/rest_framework/tests/nested_relations.py b/rest_framework/tests/nested_relations.py index 35e75bb03..b5daea891 100644 --- a/rest_framework/tests/nested_relations.py +++ b/rest_framework/tests/nested_relations.py @@ -49,6 +49,22 @@ class ReverseForeignKeyTests(TestCase): serializer = ForeignKeyTargetSerializer(queryset) self.assertEquals(serializer.data, self.data) + def test_reverse_foreign_key_create(self): + data = deepcopy(self.new_target_data) + data['sources'].append({'name': u'source-4', 'target': 2}) + instance = ForeignKeyTarget.objects.get(pk=2) + serializer = ForeignKeyTargetSerializer(instance, data=data) + self.assertTrue(serializer.is_valid()) + self.assertEquals(serializer.data, data) + serializer.save() + + # Ensure target 2 has new source and everything else is as expected + queryset = ForeignKeyTarget.objects.all() + serializer = ForeignKeyTargetSerializer(queryset) + expected = deepcopy(self.data) + expected[1]['sources'].append({'id': 4, 'name': 'source-4', 'target': 2}) + self.assertEquals(serializer.data, expected) + def test_reverse_foreign_key_update(self): data = deepcopy(self.target_data) data['sources'][0]['name'] = 'source-1-changed'