mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-28 04:24:00 +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")
|
||||
|
||||
|
||||
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):
|
||||
def test_pk_relations(self):
|
||||
class TestSerializer(serializers.ModelSerializer):
|
||||
|
@ -731,6 +740,17 @@ class TestRelationalFieldMappings(TestCase):
|
|||
""")
|
||||
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):
|
||||
name = models.CharField(max_length=100)
|
||||
|
@ -1402,14 +1422,10 @@ class Issue6751Test(TestCase):
|
|||
self.assertEqual(instance.char_field, 'value changed by signal')
|
||||
|
||||
|
||||
class Tag(models.Model):
|
||||
name = models.CharField(max_length=100)
|
||||
|
||||
|
||||
class UniqueConstraintModel(models.Model):
|
||||
class UniqueConstraintNullableModel(models.Model):
|
||||
title = models.CharField(max_length=100)
|
||||
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:
|
||||
constraints = [
|
||||
|
@ -1420,12 +1436,12 @@ class UniqueConstraintModel(models.Model):
|
|||
|
||||
class TestUniqueConstraintWithNullableFields(TestCase):
|
||||
def test_nullable_unique_constraint_fields_are_not_required(self):
|
||||
class UniqueConstraintSerializer(serializers.ModelSerializer):
|
||||
class UniqueConstraintNullableSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = UniqueConstraintModel
|
||||
fields = '__all__'
|
||||
model = UniqueConstraintNullableModel
|
||||
fields = ('title', 'age', 'tag')
|
||||
|
||||
serializer = UniqueConstraintSerializer(data={'title': 'Bob'})
|
||||
serializer = UniqueConstraintNullableSerializer(data={'title': 'Bob'})
|
||||
self.assertTrue(serializer.is_valid(), serializer.errors)
|
||||
result = serializer.save()
|
||||
self.assertIsInstance(result, UniqueConstraintModel)
|
||||
self.assertIsInstance(result, UniqueConstraintNullableModel)
|
||||
|
|
Loading…
Reference in New Issue
Block a user