Skip to content

Commit 29a4b3a

Browse files
committed
Add test for relationship pointer in errors
Signed-off-by: Mehdy Khoshnoody <[email protected]>
1 parent 2bb9bae commit 29a4b3a

File tree

2 files changed

+37
-6
lines changed

2 files changed

+37
-6
lines changed

example/tests/snapshots/snap_test_errors.py

+11
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,17 @@
4444
]
4545
}
4646

47+
snapshots["test_relationship_errors_has_correct_pointers 1"] = {
48+
"errors": [
49+
{
50+
"code": "incorrect_type",
51+
"detail": "Incorrect type. Expected resource identifier object, received str.",
52+
"source": {"pointer": "/data/relationships/author"},
53+
"status": "400",
54+
}
55+
]
56+
}
57+
4758
snapshots["test_second_level_array_error 1"] = {
4859
"errors": [
4960
{

example/tests/test_errors.py

+26-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import pytest
22
from django.test import override_settings
33
from django.urls import path, reverse
4-
from rest_framework import views
4+
from rest_framework import generics
55

66
from rest_framework_json_api import serializers
77

8-
from example.models import Blog
8+
from example.models import Author, Blog
99

1010

1111
# serializers
@@ -30,6 +30,9 @@ class EntrySerializer(serializers.Serializer):
3030
comment = CommentSerializer(required=False)
3131
headline = serializers.CharField(allow_null=True, required=True)
3232
body_text = serializers.CharField()
33+
author = serializers.ResourceRelatedField(
34+
queryset=Author.objects.all(), required=False
35+
)
3336

3437
def validate(self, attrs):
3538
body_text = attrs["body_text"]
@@ -40,13 +43,12 @@ def validate(self, attrs):
4043

4144

4245
# view
43-
class DummyTestView(views.APIView):
46+
class DummyTestView(generics.CreateAPIView):
4447
serializer_class = EntrySerializer
4548
resource_name = "entries"
4649

47-
def post(self, request, *args, **kwargs):
48-
serializer = self.serializer_class(data=request.data)
49-
serializer.is_valid(raise_exception=True)
50+
def get_serializer_context(self):
51+
return {}
5052

5153

5254
urlpatterns = [
@@ -191,3 +193,21 @@ def test_many_third_level_dict_errors(client, some_blog, snapshot):
191193
}
192194

193195
snapshot.assert_match(perform_error_test(client, data))
196+
197+
198+
def test_relationship_errors_has_correct_pointers(client, some_blog, snapshot):
199+
data = {
200+
"data": {
201+
"type": "entries",
202+
"attributes": {
203+
"blog": some_blog.pk,
204+
"bodyText": "body_text",
205+
"headline": "headline",
206+
},
207+
"relationships": {
208+
"author": {"data": {"id": "INVALID_ID", "type": "authors"}}
209+
},
210+
}
211+
}
212+
213+
snapshot.assert_match(perform_error_test(client, data))

0 commit comments

Comments
 (0)