mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-30 05:24:00 +03:00
Merge pull request #1493 from r4vi/fieldValidator-not-applied
add regression tests for field Validators
This commit is contained in:
commit
1c10886eba
|
@ -1,4 +1,5 @@
|
|||
from __future__ import unicode_literals
|
||||
from django.core.validators import MaxValueValidator
|
||||
from django.db import models
|
||||
from django.test import TestCase
|
||||
from rest_framework import generics, serializers, status
|
||||
|
@ -102,3 +103,46 @@ class TestAvoidValidation(TestCase):
|
|||
self.assertFalse(serializer.is_valid())
|
||||
self.assertDictEqual(serializer.errors,
|
||||
{'non_field_errors': ['Invalid data']})
|
||||
|
||||
|
||||
# regression tests for issue: 1493
|
||||
|
||||
class ValidationMaxValueValidatorModel(models.Model):
|
||||
number_value = models.PositiveIntegerField(validators=[MaxValueValidator(100)])
|
||||
|
||||
|
||||
class ValidationMaxValueValidatorModelSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = ValidationMaxValueValidatorModel
|
||||
|
||||
|
||||
class UpdateMaxValueValidationModel(generics.RetrieveUpdateDestroyAPIView):
|
||||
model = ValidationMaxValueValidatorModel
|
||||
serializer_class = ValidationMaxValueValidatorModelSerializer
|
||||
|
||||
|
||||
class TestMaxValueValidatorValidation(TestCase):
|
||||
|
||||
def test_max_value_validation_serializer_success(self):
|
||||
serializer = ValidationMaxValueValidatorModelSerializer(data={'number_value': 99})
|
||||
self.assertTrue(serializer.is_valid())
|
||||
|
||||
def test_max_value_validation_serializer_fails(self):
|
||||
serializer = ValidationMaxValueValidatorModelSerializer(data={'number_value': 101})
|
||||
self.assertFalse(serializer.is_valid())
|
||||
self.assertDictEqual({'number_value': ['Ensure this value is less than or equal to 100.']}, serializer.errors)
|
||||
|
||||
def test_max_value_validation_success(self):
|
||||
obj = ValidationMaxValueValidatorModel.objects.create(number_value=100)
|
||||
request = factory.patch('/{0}'.format(obj.pk), {'number_value': 98}, format='json')
|
||||
view = UpdateMaxValueValidationModel().as_view()
|
||||
response = view(request, pk=obj.pk).render()
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
|
||||
def test_max_value_validation_fail(self):
|
||||
obj = ValidationMaxValueValidatorModel.objects.create(number_value=100)
|
||||
request = factory.patch('/{0}'.format(obj.pk), {'number_value': 101}, format='json')
|
||||
view = UpdateMaxValueValidationModel().as_view()
|
||||
response = view(request, pk=obj.pk).render()
|
||||
self.assertEqual(response.content, '{"number_value": ["Ensure this value is less than or equal to 100."]}')
|
||||
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
|
||||
|
|
Loading…
Reference in New Issue
Block a user