|
| 1 | +import json |
| 2 | + |
| 3 | +import pytest |
| 4 | +from django.conf.urls import url |
| 5 | +from django.test import override_settings |
| 6 | +from django.urls import reverse |
| 7 | + |
| 8 | +# from example import urls_test |
| 9 | +from example.models import Blog |
| 10 | +from example.tests import TestBase |
| 11 | +from rest_framework_json_api import serializers |
| 12 | +from rest_framework import views |
| 13 | + |
| 14 | + |
| 15 | +# serializers |
| 16 | +class CommentAttachmentSerializer(serializers.Serializer): |
| 17 | + data = serializers.CharField(allow_null=False, required=True) |
| 18 | + |
| 19 | + def validate_data(self, value): |
| 20 | + if value and len(value) < 10: |
| 21 | + raise serializers.ValidationError('Too short data') |
| 22 | + |
| 23 | + |
| 24 | +class CommentSerializer(serializers.Serializer): |
| 25 | + attachments = CommentAttachmentSerializer(many=True, required=False) |
| 26 | + attachment = CommentAttachmentSerializer(required=False) |
| 27 | + body = serializers.CharField(allow_null=False, required=True) |
| 28 | + |
| 29 | + |
| 30 | +class EntrySerializer(serializers.Serializer): |
| 31 | + blog = serializers.IntegerField() |
| 32 | + comments = CommentSerializer(many=True, required=False) |
| 33 | + comment = CommentSerializer(required=False) |
| 34 | + headline = serializers.CharField(allow_null=True, required=True) |
| 35 | + body_text = serializers.CharField() |
| 36 | + |
| 37 | + def validate(self, attrs): |
| 38 | + body_text = attrs['body_text'] |
| 39 | + if len(body_text) < 5: |
| 40 | + raise serializers.ValidationError({'body_text': 'Too short'}) |
| 41 | + |
| 42 | + |
| 43 | +# view |
| 44 | +class DummyTestView(views.APIView): |
| 45 | + serializer_class = EntrySerializer |
| 46 | + resource_name = 'entries' |
| 47 | + |
| 48 | + def post(self, request, *args, **kwargs): |
| 49 | + serializer = self.serializer_class(data=request.data) |
| 50 | + serializer.is_valid(raise_exception=True) |
| 51 | + |
| 52 | + |
| 53 | +urlpatterns = [ |
| 54 | + url(r'^entries-nested/$', DummyTestView.as_view(), |
| 55 | + name='entries-nested-list') |
| 56 | +] |
| 57 | + |
| 58 | + |
| 59 | +@override_settings(ROOT_URLCONF=__name__) |
| 60 | +@pytest.mark.filterwarnings('ignore:Rendering nested') |
| 61 | +class TestNestedErrors(TestBase): |
| 62 | + |
| 63 | + def setUp(self): |
| 64 | + super(TestNestedErrors, self).setUp() |
| 65 | + self.url = reverse('entries-nested-list') |
| 66 | + self.blog = Blog.objects.create(name='Some Blog', tagline="It's a blog") |
| 67 | + |
| 68 | + def perform_error_test(self, data, expected_pointer): |
| 69 | + with override_settings( |
| 70 | + JSON_API_SERIALIZE_NESTED_SERIALIZERS_AS_ATTRIBUTE=True): |
| 71 | + response = self.client.post(self.url, data=data) |
| 72 | + |
| 73 | + errors = response.data |
| 74 | + |
| 75 | + assert len(errors) == 1 |
| 76 | + assert errors[0]['source']['pointer'] == expected_pointer |
| 77 | + |
| 78 | + def test_first_level_attribute_error(self): |
| 79 | + data = { |
| 80 | + 'data': { |
| 81 | + 'type': 'entries', |
| 82 | + 'attributes': { |
| 83 | + 'blog': self.blog.pk, |
| 84 | + 'body_text': 'body_text', |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + self.perform_error_test(data, '/data/attributes/headline') |
| 89 | + |
| 90 | + def test_first_level_custom_attribute_error(self): |
| 91 | + data = { |
| 92 | + 'data': { |
| 93 | + 'type': 'entries', |
| 94 | + 'attributes': { |
| 95 | + 'blog': self.blog.pk, |
| 96 | + 'body_text': 'body', |
| 97 | + 'headline': 'headline' |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + with override_settings(JSON_API_FORMAT_FIELD_NAMES='underscore'): |
| 102 | + self.perform_error_test(data, '/data/attributes/body_text') |
| 103 | + |
| 104 | + def test_second_level_array_error(self): |
| 105 | + data = { |
| 106 | + 'data': { |
| 107 | + 'type': 'entries', |
| 108 | + 'attributes': { |
| 109 | + 'blog': self.blog.pk, |
| 110 | + 'body_text': 'body_text', |
| 111 | + 'headline': 'headline', |
| 112 | + 'comments': [ |
| 113 | + { |
| 114 | + } |
| 115 | + ] |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + self.perform_error_test(data, '/data/attributes/comments/0/body') |
| 121 | + |
| 122 | + def test_second_level_dict_error(self): |
| 123 | + data = { |
| 124 | + 'data': { |
| 125 | + 'type': 'entries', |
| 126 | + 'attributes': { |
| 127 | + 'blog': self.blog.pk, |
| 128 | + 'body_text': 'body_text', |
| 129 | + 'headline': 'headline', |
| 130 | + 'comment': {} |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + self.perform_error_test(data, '/data/attributes/comment/body') |
| 136 | + |
| 137 | + def test_third_level_array_error(self): |
| 138 | + data = { |
| 139 | + 'data': { |
| 140 | + 'type': 'entries', |
| 141 | + 'attributes': { |
| 142 | + 'blog': self.blog.pk, |
| 143 | + 'body_text': 'body_text', |
| 144 | + 'headline': 'headline', |
| 145 | + 'comments': [ |
| 146 | + { |
| 147 | + 'body': 'test comment', |
| 148 | + 'attachments': [ |
| 149 | + { |
| 150 | + } |
| 151 | + ] |
| 152 | + } |
| 153 | + ] |
| 154 | + } |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + self.perform_error_test(data, '/data/attributes/comments/0/attachments/0/data') |
| 159 | + |
| 160 | + def test_third_level_custom_array_error(self): |
| 161 | + data = { |
| 162 | + 'data': { |
| 163 | + 'type': 'entries', |
| 164 | + 'attributes': { |
| 165 | + 'blog': self.blog.pk, |
| 166 | + 'body_text': 'body_text', |
| 167 | + 'headline': 'headline', |
| 168 | + 'comments': [ |
| 169 | + { |
| 170 | + 'body': 'test comment', |
| 171 | + 'attachments': [ |
| 172 | + { |
| 173 | + 'data': 'text' |
| 174 | + } |
| 175 | + ] |
| 176 | + } |
| 177 | + ] |
| 178 | + } |
| 179 | + } |
| 180 | + } |
| 181 | + |
| 182 | + self.perform_error_test(data, '/data/attributes/comments/0/attachments/0/data') |
| 183 | + |
| 184 | + def test_third_level_dict_error(self): |
| 185 | + data = { |
| 186 | + 'data': { |
| 187 | + 'type': 'entries', |
| 188 | + 'attributes': { |
| 189 | + 'blog': self.blog.pk, |
| 190 | + 'body_text': 'body_text', |
| 191 | + 'headline': 'headline', |
| 192 | + 'comments': [ |
| 193 | + { |
| 194 | + 'body': 'test comment', |
| 195 | + 'attachment': {} |
| 196 | + } |
| 197 | + ] |
| 198 | + } |
| 199 | + } |
| 200 | + } |
| 201 | + |
| 202 | + self.perform_error_test(data, '/data/attributes/comments/0/attachment/data') |
0 commit comments