Remove redundant test and move other to more appropriate place

This commit is contained in:
Bruno Alla 2024-10-14 19:24:56 +01:00
parent 25e01d0761
commit d2da2f5e06
No known key found for this signature in database
2 changed files with 24 additions and 45 deletions

View File

@ -542,15 +542,6 @@ 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):
@ -740,17 +731,6 @@ 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)
@ -1420,28 +1400,3 @@ class Issue6751Test(TestCase):
serializer.save() serializer.save()
self.assertEqual(instance.char_field, 'value changed by signal') self.assertEqual(instance.char_field, 'value changed by signal')
class UniqueConstraintNullableModel(models.Model):
title = models.CharField(max_length=100)
age = models.IntegerField(null=True)
tag = models.CharField(max_length=100, null=True)
class Meta:
constraints = [
# Unique constraint on 2 nullable fields
models.UniqueConstraint(name='unique_constraint', fields=('age', 'tag'))
]
class TestUniqueConstraintWithNullableFields(TestCase):
def test_nullable_unique_constraint_fields_are_not_required(self):
class UniqueConstraintNullableSerializer(serializers.ModelSerializer):
class Meta:
model = UniqueConstraintNullableModel
fields = ('title', 'age', 'tag')
serializer = UniqueConstraintNullableSerializer(data={'title': 'Bob'})
self.assertTrue(serializer.is_valid(), serializer.errors)
result = serializer.save()
self.assertIsInstance(result, UniqueConstraintNullableModel)

View File

@ -547,12 +547,30 @@ class UniqueConstraintModel(models.Model):
] ]
class UniqueConstraintNullableModel(models.Model):
title = models.CharField(max_length=100)
age = models.IntegerField(null=True)
tag = models.CharField(max_length=100, null=True)
class Meta:
constraints = [
# Unique constraint on 2 nullable fields
models.UniqueConstraint(name='unique_constraint', fields=('age', 'tag'))
]
class UniqueConstraintSerializer(serializers.ModelSerializer): class UniqueConstraintSerializer(serializers.ModelSerializer):
class Meta: class Meta:
model = UniqueConstraintModel model = UniqueConstraintModel
fields = '__all__' fields = '__all__'
class UniqueConstraintNullableSerializer(serializers.ModelSerializer):
class Meta:
model = UniqueConstraintNullableModel
fields = ('title', 'age', 'tag')
class TestUniqueConstraintValidation(TestCase): class TestUniqueConstraintValidation(TestCase):
def setUp(self): def setUp(self):
self.instance = UniqueConstraintModel.objects.create( self.instance = UniqueConstraintModel.objects.create(
@ -619,6 +637,12 @@ class TestUniqueConstraintValidation(TestCase):
ids_in_qs = {frozenset(v.queryset.values_list(flat=True)) for v in validators if hasattr(v, "queryset")} ids_in_qs = {frozenset(v.queryset.values_list(flat=True)) for v in validators if hasattr(v, "queryset")}
assert ids_in_qs == {frozenset([1]), frozenset([3])} assert ids_in_qs == {frozenset([1]), frozenset([3])}
def test_nullable_unique_constraint_fields_are_not_required(self):
serializer = UniqueConstraintNullableSerializer(data={'title': 'Bob'})
self.assertTrue(serializer.is_valid(), serializer.errors)
result = serializer.save()
self.assertIsInstance(result, UniqueConstraintNullableModel)
# Tests for `UniqueForDateValidator` # Tests for `UniqueForDateValidator`
# ---------------------------------- # ----------------------------------