Tests for reverse relationships

This commit is contained in:
Tom Christie 2014-09-15 13:48:03 +01:00
parent c1e2a9cba6
commit 0c15b97b33

View File

@ -161,23 +161,44 @@ class TestRelationalFieldMappings(TestCase):
""")
self.assertEqual(repr(TestSerializer()), expected)
# def test_flat_reverse_foreign_key(self):
# class TestSerializer(serializers.ModelSerializer):
# class Meta:
# model = ForeignKeyTargetModel
# fields = ('id', 'name', 'reverse_foreign_key')
# print repr(TestSerializer())
def test_flat_reverse_foreign_key(self):
class TestSerializer(serializers.ModelSerializer):
class Meta:
model = ForeignKeyTargetModel
fields = ('id', 'name', 'reverse_foreign_key')
# def test_flat_reverse_one_to_one(self):
# class TestSerializer(serializers.ModelSerializer):
# class Meta:
# model = OneToOneTargetModel
# fields = ('id', 'name', 'reverse_one_to_one')
# print repr(TestSerializer())
expected = dedent("""
TestSerializer():
id = IntegerField(label='ID', read_only=True)
name = CharField(max_length=100)
reverse_foreign_key = PrimaryKeyRelatedField(many=True, queryset=RelationalModel.objects.all())
""")
self.assertEqual(repr(TestSerializer()), expected)
# def test_flat_reverse_many_to_many(self):
# class TestSerializer(serializers.ModelSerializer):
# class Meta:
# model = ManyToManyTargetModel
# fields = ('id', 'name', 'reverse_many_to_many')
# print repr(TestSerializer())
def test_flat_reverse_one_to_one(self):
class TestSerializer(serializers.ModelSerializer):
class Meta:
model = OneToOneTargetModel
fields = ('id', 'name', 'reverse_one_to_one')
expected = dedent("""
TestSerializer():
id = IntegerField(label='ID', read_only=True)
name = CharField(max_length=100)
reverse_one_to_one = PrimaryKeyRelatedField(queryset=RelationalModel.objects.all())
""")
self.assertEqual(repr(TestSerializer()), expected)
def test_flat_reverse_many_to_many(self):
class TestSerializer(serializers.ModelSerializer):
class Meta:
model = ManyToManyTargetModel
fields = ('id', 'name', 'reverse_many_to_many')
expected = dedent("""
TestSerializer():
id = IntegerField(label='ID', read_only=True)
name = CharField(max_length=100)
reverse_many_to_many = PrimaryKeyRelatedField(many=True, queryset=RelationalModel.objects.all())
""")
self.assertEqual(repr(TestSerializer()), expected)