diff options
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/test_fields.py | 23 | ||||
| -rw-r--r-- | tests/test_relations.py | 20 | ||||
| -rw-r--r-- | tests/test_validation.py | 11 |
3 files changed, 26 insertions, 28 deletions
diff --git a/tests/test_fields.py b/tests/test_fields.py index eaa0a3c8..5e8c67c5 100644 --- a/tests/test_fields.py +++ b/tests/test_fields.py @@ -1,7 +1,6 @@ from decimal import Decimal -from django.core.exceptions import ValidationError from django.utils import timezone -from rest_framework import fields, serializers +from rest_framework import exceptions, fields, serializers import datetime import django import pytest @@ -19,9 +18,9 @@ class TestEmpty: By default a field must be included in the input. """ field = fields.IntegerField() - with pytest.raises(fields.ValidationError) as exc_info: + with pytest.raises(exceptions.ValidationFailed) as exc_info: field.run_validation() - assert exc_info.value.messages == ['This field is required.'] + assert exc_info.value.detail == ['This field is required.'] def test_not_required(self): """ @@ -36,9 +35,9 @@ class TestEmpty: By default `None` is not a valid input. """ field = fields.IntegerField() - with pytest.raises(fields.ValidationError) as exc_info: + with pytest.raises(exceptions.ValidationFailed) as exc_info: field.run_validation(None) - assert exc_info.value.messages == ['This field may not be null.'] + assert exc_info.value.detail == ['This field may not be null.'] def test_allow_null(self): """ @@ -53,9 +52,9 @@ class TestEmpty: By default '' is not a valid input. """ field = fields.CharField() - with pytest.raises(fields.ValidationError) as exc_info: + with pytest.raises(exceptions.ValidationFailed) as exc_info: field.run_validation('') - assert exc_info.value.messages == ['This field may not be blank.'] + assert exc_info.value.detail == ['This field may not be blank.'] def test_allow_blank(self): """ @@ -190,7 +189,7 @@ class TestInvalidErrorKey: with pytest.raises(AssertionError) as exc_info: self.field.to_native(123) expected = ( - 'ValidationError raised by `ExampleField`, but error key ' + 'ValidationFailed raised by `ExampleField`, but error key ' '`incorrect` does not exist in the `error_messages` dictionary.' ) assert str(exc_info.value) == expected @@ -244,9 +243,9 @@ class FieldValues: Ensure that invalid values raise the expected validation error. """ for input_value, expected_failure in get_items(self.invalid_inputs): - with pytest.raises(fields.ValidationError) as exc_info: + with pytest.raises(exceptions.ValidationFailed) as exc_info: self.field.run_validation(input_value) - assert exc_info.value.messages == expected_failure + assert exc_info.value.detail == expected_failure def test_outputs(self): for output_value, expected_output in get_items(self.outputs): @@ -901,7 +900,7 @@ class TestFieldFieldWithName(FieldValues): # call into it's regular validation, or require PIL for testing. class FailImageValidation(object): def to_python(self, value): - raise ValidationError(self.error_messages['invalid_image']) + raise exceptions.ValidationFailed(self.error_messages['invalid_image']) class PassImageValidation(object): diff --git a/tests/test_relations.py b/tests/test_relations.py index 66784195..53c1b25c 100644 --- a/tests/test_relations.py +++ b/tests/test_relations.py @@ -1,6 +1,6 @@ from .utils import mock_reverse, fail_reverse, BadType, MockObject, MockQueryset -from django.core.exceptions import ImproperlyConfigured, ValidationError -from rest_framework import serializers +from django.core.exceptions import ImproperlyConfigured +from rest_framework import exceptions, serializers from rest_framework.test import APISimpleTestCase import pytest @@ -30,15 +30,15 @@ class TestPrimaryKeyRelatedField(APISimpleTestCase): assert instance is self.instance def test_pk_related_lookup_does_not_exist(self): - with pytest.raises(ValidationError) as excinfo: + with pytest.raises(exceptions.ValidationFailed) as excinfo: self.field.to_internal_value(4) - msg = excinfo.value.messages[0] + msg = excinfo.value.detail[0] assert msg == "Invalid pk '4' - object does not exist." def test_pk_related_lookup_invalid_type(self): - with pytest.raises(ValidationError) as excinfo: + with pytest.raises(exceptions.ValidationFailed) as excinfo: self.field.to_internal_value(BadType()) - msg = excinfo.value.messages[0] + msg = excinfo.value.detail[0] assert msg == 'Incorrect type. Expected pk value, received BadType.' def test_pk_representation(self): @@ -120,15 +120,15 @@ class TestSlugRelatedField(APISimpleTestCase): assert instance is self.instance def test_slug_related_lookup_does_not_exist(self): - with pytest.raises(ValidationError) as excinfo: + with pytest.raises(exceptions.ValidationFailed) as excinfo: self.field.to_internal_value('doesnotexist') - msg = excinfo.value.messages[0] + msg = excinfo.value.detail[0] assert msg == 'Object with name=doesnotexist does not exist.' def test_slug_related_lookup_invalid_type(self): - with pytest.raises(ValidationError) as excinfo: + with pytest.raises(exceptions.ValidationFailed) as excinfo: self.field.to_internal_value(BadType()) - msg = excinfo.value.messages[0] + msg = excinfo.value.detail[0] assert msg == 'Invalid value.' def test_representation(self): diff --git a/tests/test_validation.py b/tests/test_validation.py index ce39714d..849c7e9d 100644 --- a/tests/test_validation.py +++ b/tests/test_validation.py @@ -1,9 +1,8 @@ from __future__ import unicode_literals from django.core.validators import MaxValueValidator -from django.core.exceptions import ValidationError from django.db import models from django.test import TestCase -from rest_framework import generics, serializers, status +from rest_framework import exceptions, generics, serializers, status from rest_framework.test import APIRequestFactory factory = APIRequestFactory() @@ -38,7 +37,7 @@ class ShouldValidateModelSerializer(serializers.ModelSerializer): def validate_renamed(self, value): if len(value) < 3: - raise serializers.ValidationError('Minimum 3 characters.') + raise exceptions.ValidationFailed('Minimum 3 characters.') return value class Meta: @@ -74,10 +73,10 @@ class ValidationSerializer(serializers.Serializer): foo = serializers.CharField() def validate_foo(self, attrs, source): - raise serializers.ValidationError("foo invalid") + raise exceptions.ValidationFailed("foo invalid") def validate(self, attrs): - raise serializers.ValidationError("serializer invalid") + raise exceptions.ValidationFailed("serializer invalid") class TestAvoidValidation(TestCase): @@ -159,7 +158,7 @@ class TestChoiceFieldChoicesValidate(TestCase): value = self.CHOICES[0][0] try: f.to_internal_value(value) - except ValidationError: + except exceptions.ValidationFailed: self.fail("Value %s does not validate" % str(value)) # def test_nested_choices(self): |
