mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-01-23 15:54:16 +03:00
add regression tests for field Validators
pep8 and add issue no fix formatting for python 2.6 and strings for python 3.2
This commit is contained in:
parent
73cd4e236f
commit
3560796bbf
|
@ -1,4 +1,5 @@
|
||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
|
from django.core.validators import MaxValueValidator
|
||||||
from django.db import models
|
from django.db import models
|
||||||
from django.test import TestCase
|
from django.test import TestCase
|
||||||
from rest_framework import generics, serializers, status
|
from rest_framework import generics, serializers, status
|
||||||
|
@ -102,3 +103,46 @@ class TestAvoidValidation(TestCase):
|
||||||
self.assertFalse(serializer.is_valid())
|
self.assertFalse(serializer.is_valid())
|
||||||
self.assertDictEqual(serializer.errors,
|
self.assertDictEqual(serializer.errors,
|
||||||
{'non_field_errors': ['Invalid data']})
|
{'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