Add tests for nested FKs

This commit is contained in:
Tom Christie 2012-12-29 13:10:43 +00:00
parent 1f6af163fe
commit 250a7231d3

View File

@ -15,12 +15,18 @@ class ForeignKeySource(models.Model):
class ForeignKeySourceSerializer(serializers.ModelSerializer): class ForeignKeySourceSerializer(serializers.ModelSerializer):
class Meta:
depth = 1
model = ForeignKeySource
class FlatForeignKeySourceSerializer(serializers.ModelSerializer):
class Meta: class Meta:
model = ForeignKeySource model = ForeignKeySource
class ForeignKeyTargetSerializer(serializers.ModelSerializer): class ForeignKeyTargetSerializer(serializers.ModelSerializer):
sources = ForeignKeySourceSerializer() sources = FlatForeignKeySourceSerializer()
class Meta: class Meta:
model = ForeignKeyTarget model = ForeignKeyTarget
@ -36,6 +42,16 @@ class ReverseForeignKeyTests(TestCase):
source = ForeignKeySource(name='source-%d' % idx, target=target) source = ForeignKeySource(name='source-%d' % idx, target=target)
source.save() source.save()
def test_foreign_key_retrieve(self):
queryset = ForeignKeySource.objects.all()
serializer = ForeignKeySourceSerializer(queryset)
expected = [
{'id': 1, 'name': u'source-1', 'target': {'id': 1, 'name': u'target-1'}},
{'id': 2, 'name': u'source-2', 'target': {'id': 1, 'name': u'target-1'}},
{'id': 3, 'name': u'source-3', 'target': {'id': 1, 'name': u'target-1'}},
]
self.assertEquals(serializer.data, expected)
def test_reverse_foreign_key_retrieve(self): def test_reverse_foreign_key_retrieve(self):
queryset = ForeignKeyTarget.objects.all() queryset = ForeignKeyTarget.objects.all()
serializer = ForeignKeyTargetSerializer(queryset) serializer = ForeignKeyTargetSerializer(queryset)