|
2 | 2 | from django.db.models import ProtectedError |
3 | 3 | from django.http import Http404 |
4 | 4 | from rest_framework import exceptions, status |
| 5 | +from rest_framework.exceptions import ErrorDetail |
5 | 6 |
|
6 | 7 | from exceptions_hog.handler import exception_handler |
7 | 8 | from exceptions_hog.settings import api_settings |
@@ -73,6 +74,92 @@ def test_validation_error() -> None: |
73 | 74 | } |
74 | 75 |
|
75 | 76 |
|
| 77 | +def test_validation_error_serializer_field() -> None: |
| 78 | + response = exception_handler( |
| 79 | + exceptions.ValidationError( |
| 80 | + { |
| 81 | + "phone_number": [ |
| 82 | + ErrorDetail(string="This field is required.", code="required") |
| 83 | + ] |
| 84 | + } |
| 85 | + ) |
| 86 | + ) |
| 87 | + assert response is not None |
| 88 | + assert response.status_code == status.HTTP_400_BAD_REQUEST |
| 89 | + assert response.data == { |
| 90 | + "type": "validation_error", |
| 91 | + "code": "required", |
| 92 | + "detail": "This field is required.", |
| 93 | + "attr": "phone_number", |
| 94 | + } |
| 95 | + |
| 96 | + |
| 97 | +def test_validation_error_with_simple_nested_serializer_field() -> None: |
| 98 | + response = exception_handler( |
| 99 | + exceptions.ValidationError( |
| 100 | + { |
| 101 | + "parent": { |
| 102 | + "children_attr": [ |
| 103 | + ErrorDetail(string="This field is required.", code="required") |
| 104 | + ], |
| 105 | + "second_children_attr": [ |
| 106 | + ErrorDetail( |
| 107 | + string="This field is also invalid.", code="invalid_too" |
| 108 | + ) |
| 109 | + ], |
| 110 | + } |
| 111 | + } |
| 112 | + ) |
| 113 | + ) |
| 114 | + assert response is not None |
| 115 | + assert response.status_code == status.HTTP_400_BAD_REQUEST |
| 116 | + assert response.data == { |
| 117 | + "type": "validation_error", |
| 118 | + "code": "required", |
| 119 | + "detail": "This field is required.", |
| 120 | + "attr": "parent__children_attr", |
| 121 | + } |
| 122 | + |
| 123 | + |
| 124 | +def test_validation_error_with_complex_nested_serializer_field() -> None: |
| 125 | + response = exception_handler( |
| 126 | + exceptions.ValidationError( |
| 127 | + { |
| 128 | + "parent": { |
| 129 | + "l1_attr": { |
| 130 | + "l2_attr": { |
| 131 | + "l3_attr": ErrorDetail( |
| 132 | + string="Focus on this error.", code="focus" |
| 133 | + ), |
| 134 | + }, |
| 135 | + "l2_attr_2": { |
| 136 | + "l3_attr_2": [ |
| 137 | + ErrorDetail( |
| 138 | + string="This field is also invalid.", |
| 139 | + code="invalid_too", |
| 140 | + ) |
| 141 | + ] |
| 142 | + }, |
| 143 | + }, |
| 144 | + "l1_attr_2": [ |
| 145 | + ErrorDetail( |
| 146 | + string="This field is also invalid.", code="invalid_too" |
| 147 | + ) |
| 148 | + ], |
| 149 | + } |
| 150 | + } |
| 151 | + ) |
| 152 | + ) |
| 153 | + assert response is not None |
| 154 | + assert response.status_code == status.HTTP_400_BAD_REQUEST |
| 155 | + assert response.data == { |
| 156 | + "type": "validation_error", |
| 157 | + "code": "focus", |
| 158 | + "detail": "Focus on this error.", |
| 159 | + "attr": "parent__l1_attr__l2_attr__l3_attr", |
| 160 | + } |
| 161 | + |
| 162 | + |
76 | 163 | # Django & DRF exceptions |
77 | 164 |
|
78 | 165 |
|
|
0 commit comments