|
6 | 6 | from decimal import ROUND_DOWN, ROUND_UP, Decimal
|
7 | 7 |
|
8 | 8 | import pytest
|
| 9 | +from django.core.exceptions import ValidationError as DjangoValidationError |
9 | 10 | from django.http import QueryDict
|
10 | 11 | from django.test import TestCase, override_settings
|
11 | 12 | from django.utils import six
|
12 | 13 | from django.utils.timezone import activate, deactivate, override, utc
|
13 | 14 |
|
14 | 15 | import rest_framework
|
15 |
| -from rest_framework import compat, serializers |
| 16 | +from rest_framework import compat, exceptions, serializers |
16 | 17 | from rest_framework.fields import DjangoImageField, is_simple_callable
|
17 | 18 |
|
18 | 19 | try:
|
@@ -2183,3 +2184,91 @@ class ExampleSerializer(serializers.Serializer):
|
2183 | 2184 | "'ExampleSerializer', because it is the same as the default "
|
2184 | 2185 | "method name. Remove the `method_name` argument."
|
2185 | 2186 | )
|
| 2187 | + |
| 2188 | + |
| 2189 | +class TestValidationErrorCode: |
| 2190 | + @pytest.mark.parametrize('use_list', (False, True)) |
| 2191 | + def test_validationerror_code_with_msg(self, use_list): |
| 2192 | + |
| 2193 | + class ExampleSerializer(serializers.Serializer): |
| 2194 | + password = serializers.CharField() |
| 2195 | + |
| 2196 | + def validate_password(self, obj): |
| 2197 | + err = DjangoValidationError('exc_msg', code='exc_code') |
| 2198 | + if use_list: |
| 2199 | + err = DjangoValidationError([err]) |
| 2200 | + raise err |
| 2201 | + |
| 2202 | + serializer = ExampleSerializer(data={'password': 123}) |
| 2203 | + serializer.is_valid() |
| 2204 | + assert serializer.errors == {'password': ['exc_msg']} |
| 2205 | + assert serializer.errors['password'][0].code == 'exc_code' |
| 2206 | + |
| 2207 | + @pytest.mark.parametrize('code', (None, 'exc_code',)) |
| 2208 | + @pytest.mark.parametrize('use_list', (False, True)) |
| 2209 | + def test_validationerror_code_with_dict(self, use_list, code): |
| 2210 | + |
| 2211 | + class ExampleSerializer(serializers.Serializer): |
| 2212 | + |
| 2213 | + def validate(self, obj): |
| 2214 | + if code is None: |
| 2215 | + err = DjangoValidationError({ |
| 2216 | + 'email': 'email error', |
| 2217 | + }) |
| 2218 | + else: |
| 2219 | + err = DjangoValidationError({ |
| 2220 | + 'email': DjangoValidationError( |
| 2221 | + 'email error', |
| 2222 | + code=code), |
| 2223 | + }) |
| 2224 | + if use_list: |
| 2225 | + err = DjangoValidationError([err]) |
| 2226 | + raise err |
| 2227 | + |
| 2228 | + serializer = ExampleSerializer(data={}) |
| 2229 | + serializer.is_valid() |
| 2230 | + expected_code = code if code else 'invalid' |
| 2231 | + if use_list: |
| 2232 | + assert serializer.errors == { |
| 2233 | + 'non_field_errors': [ |
| 2234 | + exceptions.ErrorDetail( |
| 2235 | + string='email error', |
| 2236 | + code=expected_code |
| 2237 | + ) |
| 2238 | + ] |
| 2239 | + } |
| 2240 | + else: |
| 2241 | + assert serializer.errors == { |
| 2242 | + 'email': ['email error'], |
| 2243 | + } |
| 2244 | + assert serializer.errors['email'][0].code == expected_code |
| 2245 | + |
| 2246 | + @pytest.mark.parametrize('code', (None, 'exc_code',)) |
| 2247 | + def test_validationerror_code_with_dict_list_same_code(self, code): |
| 2248 | + |
| 2249 | + class ExampleSerializer(serializers.Serializer): |
| 2250 | + |
| 2251 | + def validate(self, obj): |
| 2252 | + if code is None: |
| 2253 | + raise DjangoValidationError({'email': ['email error 1', |
| 2254 | + 'email error 2']}) |
| 2255 | + raise DjangoValidationError({'email': [ |
| 2256 | + DjangoValidationError('email error 1', code=code), |
| 2257 | + DjangoValidationError('email error 2', code=code), |
| 2258 | + ]}) |
| 2259 | + |
| 2260 | + serializer = ExampleSerializer(data={}) |
| 2261 | + serializer.is_valid() |
| 2262 | + expected_code = code if code else 'invalid' |
| 2263 | + assert serializer.errors == { |
| 2264 | + 'email': [ |
| 2265 | + exceptions.ErrorDetail( |
| 2266 | + string='email error 1', |
| 2267 | + code=expected_code |
| 2268 | + ), |
| 2269 | + exceptions.ErrorDetail( |
| 2270 | + string='email error 2', |
| 2271 | + code=expected_code |
| 2272 | + ), |
| 2273 | + ] |
| 2274 | + } |
0 commit comments