mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-02-18 04:20:53 +03:00
update test case
This commit is contained in:
parent
56fe0e4b3f
commit
ef8092ba2f
|
@ -99,6 +99,15 @@ class Issue3674ChildModel(models.Model):
|
||||||
value = models.CharField(primary_key=True, max_length=64)
|
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):
|
class TestModelSerializer(TestCase):
|
||||||
def test_create_method(self):
|
def test_create_method(self):
|
||||||
class TestSerializer(serializers.ModelSerializer):
|
class TestSerializer(serializers.ModelSerializer):
|
||||||
|
@ -1080,3 +1089,22 @@ class Issue4897TestCase(TestCase):
|
||||||
with pytest.raises(AssertionError) as cm:
|
with pytest.raises(AssertionError) as cm:
|
||||||
TestSerializer(obj).fields
|
TestSerializer(obj).fields
|
||||||
cm.match(r'readonly_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=[<UniqueValidator(queryset=UniqueChoiceModel.objects.all())>])
|
||||||
|
""")
|
||||||
|
self.assertEqual(unicode_repr(TestUniqueChoiceSerializer()), expected)
|
||||||
|
assert not serializer.is_valid()
|
||||||
|
assert serializer.errors == {'name': ['unique choice model with this name already exists.']}
|
||||||
|
|
|
@ -519,29 +519,3 @@ class TestDeclaredFieldInheritance:
|
||||||
assert len(Parent().get_fields()) == 2
|
assert len(Parent().get_fields()) == 2
|
||||||
assert len(Child().get_fields()) == 2
|
assert len(Child().get_fields()) == 2
|
||||||
assert len(Grandchild().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.']}
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user