def test_three_level_relations(self): one = RelOneDjangoModel.objects.create(name='Level1') two = RelTwoDjangoModel.objects.create(name='Level2', rel_one=one) three = RelThreeDjangoModel.objects.create(name='Level3', rel_two=two) with self.assertNumQueries(0): serializer = RelDjangoModelSerializer(three) with self.assertNumQueries(0): self.assertTrue(serializer.is_valid()) with self.assertNumQueries(0): obj_dump = serializer.dump() with self.assertNumQueries(3): serializer = RelDjangoModelSerializer(RelThreeDjangoModel.objects.first()) with self.assertNumQueries(0): self.assertTrue(serializer.is_valid()) with self.assertNumQueries(0): qs_obj_dump = serializer.dump() test_value = { 'rel_two': { 'rel_one': { 'id': 1, 'name': 'Level1'}, 'id': 1, 'name': 'Level2'}, 'rel_one': None, 'id': 1, 'name': 'Level3' } self.assertDictEqual(obj_dump, test_value) self.assertDictEqual(qs_obj_dump, test_value)
def test_three_level_relations_with_exclude(self): one = RelOneDjangoModel.objects.create(name='Level1') two = RelTwoDjangoModel.objects.create(name='Level2', rel_one=one) RelThreeDjangoModel.objects.create(name='Level3', rel_two=two) with self.assertNumQueries(2): serializer = RelDjangoModelSerializer(RelThreeDjangoModel.objects.first(), exclude=['rel_two.rel_one']) with self.assertNumQueries(0): self.assertTrue(serializer.is_valid()) with self.assertNumQueries(0): model_dump = serializer.dump() test_value = { 'rel_two': { 'id': 1, 'name': 'Level2'}, 'rel_one': None, 'id': 1, 'name': 'Level3' } self.assertDictEqual(model_dump, test_value)