diff --git a/tests/test_model_serializer.py b/tests/test_model_serializer.py index cfa671125..3633781fd 100644 --- a/tests/test_model_serializer.py +++ b/tests/test_model_serializer.py @@ -99,6 +99,15 @@ class Issue3674ChildModel(models.Model): value = models.CharField(primary_key=True, max_length=64) +class UniqueChoiceModel(models.Model): + CHOICES = ( + ('choice1', 'choice 1'), + ('choice2', 'choice 1'), + ) + + name = models.CharField(max_length=254, unique=True, choices=CHOICES) + + class TestModelSerializer(TestCase): def test_create_method(self): class TestSerializer(serializers.ModelSerializer): @@ -1080,3 +1089,22 @@ class Issue4897TestCase(TestCase): with pytest.raises(AssertionError) as cm: TestSerializer(obj).fields cm.match(r'readonly_fields') + + +class Test5004UniqueChoiceField(TestCase): + def test_unique_choice_field(self): + class TestUniqueChoiceSerializer(serializers.ModelSerializer): + class Meta: + model = UniqueChoiceModel + fields = '__all__' + + UniqueChoiceModel.objects.create(name='choice1') + serializer = TestUniqueChoiceSerializer(data={'name': 'choice1'}) + expected = dedent(""" + TestUniqueChoiceSerializer(): + id = IntegerField(label='ID', read_only=True) + name = ChoiceField(choices=(('choice1', 'choice 1'), ('choice2', 'choice 1')), validators=[]) + """) + self.assertEqual(unicode_repr(TestUniqueChoiceSerializer()), expected) + assert not serializer.is_valid() + assert serializer.errors == {'name': ['unique choice model with this name already exists.']} diff --git a/tests/test_serializer.py b/tests/test_serializer.py index 37d22ed21..f76cec9c3 100644 --- a/tests/test_serializer.py +++ b/tests/test_serializer.py @@ -519,29 +519,3 @@ class TestDeclaredFieldInheritance: assert len(Parent().get_fields()) == 2 assert len(Child().get_fields()) == 2 assert len(Grandchild().get_fields()) == 2 - - -class Poll(models.Model): - CHOICES = ( - ('choice1', 'choice 1'), - ('choice2', 'choice 1'), - ) - - name = models.CharField( - 'name', max_length=254, unique=True, choices=CHOICES - ) - - -@pytest.mark.django_db -class Test5004UniqueChoiceField: - def test_unique_choice_field(self): - Poll.objects.create(name='choice1') - - class PollSerializer(serializers.ModelSerializer): - class Meta: - model = Poll - fields = '__all__' - - serializer = PollSerializer(data={'name': 'choice1'}) - assert not serializer.is_valid() - assert serializer.errors == {'name': ['poll with this name already exists.']}