mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-30 21:44:04 +03:00
Simplify test case and add similar case for unique_together
This commit is contained in:
parent
335a27f0fd
commit
81c74d903d
|
@ -542,6 +542,15 @@ class UniqueTogetherModel(models.Model):
|
||||||
unique_together = ("foreign_key", "one_to_one")
|
unique_together = ("foreign_key", "one_to_one")
|
||||||
|
|
||||||
|
|
||||||
|
class NullableUniqueTogetherModel(models.Model):
|
||||||
|
name = models.CharField(max_length=100)
|
||||||
|
field_1 = models.IntegerField(null=True)
|
||||||
|
field_2 = models.TextField(null=True)
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
unique_together = ("field_1", "field_2")
|
||||||
|
|
||||||
|
|
||||||
class TestRelationalFieldMappings(TestCase):
|
class TestRelationalFieldMappings(TestCase):
|
||||||
def test_pk_relations(self):
|
def test_pk_relations(self):
|
||||||
class TestSerializer(serializers.ModelSerializer):
|
class TestSerializer(serializers.ModelSerializer):
|
||||||
|
@ -731,6 +740,17 @@ class TestRelationalFieldMappings(TestCase):
|
||||||
""")
|
""")
|
||||||
self.assertEqual(repr(TestSerializer()), expected)
|
self.assertEqual(repr(TestSerializer()), expected)
|
||||||
|
|
||||||
|
def test_nullable_unique_together(self):
|
||||||
|
class TestSerializer(serializers.ModelSerializer):
|
||||||
|
class Meta:
|
||||||
|
model = NullableUniqueTogetherModel
|
||||||
|
fields = ('name', 'field_1', 'field_2')
|
||||||
|
|
||||||
|
serializer = TestSerializer(data={"name": "Test"})
|
||||||
|
self.assertTrue(serializer.is_valid(), serializer.errors)
|
||||||
|
instance = serializer.save()
|
||||||
|
assert isinstance(instance, NullableUniqueTogetherModel)
|
||||||
|
|
||||||
|
|
||||||
class DisplayValueTargetModel(models.Model):
|
class DisplayValueTargetModel(models.Model):
|
||||||
name = models.CharField(max_length=100)
|
name = models.CharField(max_length=100)
|
||||||
|
@ -1402,14 +1422,10 @@ class Issue6751Test(TestCase):
|
||||||
self.assertEqual(instance.char_field, 'value changed by signal')
|
self.assertEqual(instance.char_field, 'value changed by signal')
|
||||||
|
|
||||||
|
|
||||||
class Tag(models.Model):
|
class UniqueConstraintNullableModel(models.Model):
|
||||||
name = models.CharField(max_length=100)
|
|
||||||
|
|
||||||
|
|
||||||
class UniqueConstraintModel(models.Model):
|
|
||||||
title = models.CharField(max_length=100)
|
title = models.CharField(max_length=100)
|
||||||
age = models.IntegerField(null=True)
|
age = models.IntegerField(null=True)
|
||||||
tag = models.ForeignKey(Tag, on_delete=models.CASCADE, related_name='instances', null=True)
|
tag = models.CharField(max_length=100, null=True)
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
constraints = [
|
constraints = [
|
||||||
|
@ -1420,12 +1436,12 @@ class UniqueConstraintModel(models.Model):
|
||||||
|
|
||||||
class TestUniqueConstraintWithNullableFields(TestCase):
|
class TestUniqueConstraintWithNullableFields(TestCase):
|
||||||
def test_nullable_unique_constraint_fields_are_not_required(self):
|
def test_nullable_unique_constraint_fields_are_not_required(self):
|
||||||
class UniqueConstraintSerializer(serializers.ModelSerializer):
|
class UniqueConstraintNullableSerializer(serializers.ModelSerializer):
|
||||||
class Meta:
|
class Meta:
|
||||||
model = UniqueConstraintModel
|
model = UniqueConstraintNullableModel
|
||||||
fields = '__all__'
|
fields = ('title', 'age', 'tag')
|
||||||
|
|
||||||
serializer = UniqueConstraintSerializer(data={'title': 'Bob'})
|
serializer = UniqueConstraintNullableSerializer(data={'title': 'Bob'})
|
||||||
self.assertTrue(serializer.is_valid(), serializer.errors)
|
self.assertTrue(serializer.is_valid(), serializer.errors)
|
||||||
result = serializer.save()
|
result = serializer.save()
|
||||||
self.assertIsInstance(result, UniqueConstraintModel)
|
self.assertIsInstance(result, UniqueConstraintNullableModel)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user