From d2da2f5e06c3d551f62526fe9651cb758468a8eb Mon Sep 17 00:00:00 2001 From: Bruno Alla Date: Mon, 14 Oct 2024 19:24:56 +0100 Subject: [PATCH] Remove redundant test and move other to more appropriate place --- tests/test_model_serializer.py | 45 ---------------------------------- tests/test_validators.py | 24 ++++++++++++++++++ 2 files changed, 24 insertions(+), 45 deletions(-) diff --git a/tests/test_model_serializer.py b/tests/test_model_serializer.py index 281720517..ae1a2b0fa 100644 --- a/tests/test_model_serializer.py +++ b/tests/test_model_serializer.py @@ -542,15 +542,6 @@ 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): @@ -740,17 +731,6 @@ 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) @@ -1420,28 +1400,3 @@ class Issue6751Test(TestCase): serializer.save() 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) diff --git a/tests/test_validators.py b/tests/test_validators.py index f09ecc09e..9c1a0eac3 100644 --- a/tests/test_validators.py +++ b/tests/test_validators.py @@ -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 Meta: model = UniqueConstraintModel fields = '__all__' +class UniqueConstraintNullableSerializer(serializers.ModelSerializer): + class Meta: + model = UniqueConstraintNullableModel + fields = ('title', 'age', 'tag') + + class TestUniqueConstraintValidation(TestCase): def setUp(self): 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")} 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` # ----------------------------------