Skip to content

Commit 2115a41

Browse files
committed
Add test for relationship pointer in errors
Signed-off-by: Mehdy Khoshnoody <[email protected]>
1 parent 60e3d02 commit 2115a41

File tree

2 files changed

+56
-5
lines changed

2 files changed

+56
-5
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

+45-5
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,33 @@
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

88
from example.models import Blog
99

1010

11+
class AuthorModelMock:
12+
__name__ = "author"
13+
14+
@property
15+
def model(self):
16+
return self
17+
18+
@property
19+
def queryset(self):
20+
return self
21+
22+
def create(self, *args, **kwargs):
23+
pass
24+
25+
def get(self, pk):
26+
if isinstance(pk, int):
27+
return self
28+
raise TypeError("pk must be integer")
29+
30+
1131
# serializers
1232
class CommentAttachmentSerializer(serializers.Serializer):
1333
data = serializers.CharField(allow_null=False, required=True)
@@ -30,6 +50,9 @@ class EntrySerializer(serializers.Serializer):
3050
comment = CommentSerializer(required=False)
3151
headline = serializers.CharField(allow_null=True, required=True)
3252
body_text = serializers.CharField()
53+
author = serializers.ResourceRelatedField(
54+
queryset=AuthorModelMock().queryset, required=False
55+
)
3356

3457
def validate(self, attrs):
3558
body_text = attrs["body_text"]
@@ -40,13 +63,12 @@ def validate(self, attrs):
4063

4164

4265
# view
43-
class DummyTestView(views.APIView):
66+
class DummyTestView(generics.CreateAPIView):
4467
serializer_class = EntrySerializer
4568
resource_name = "entries"
4669

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

5173

5274
urlpatterns = [
@@ -191,3 +213,21 @@ def test_many_third_level_dict_errors(client, some_blog, snapshot):
191213
}
192214

193215
snapshot.assert_match(perform_error_test(client, data))
216+
217+
218+
def test_relationship_errors_has_correct_pointers(client, some_blog, snapshot):
219+
data = {
220+
"data": {
221+
"type": "entries",
222+
"attributes": {
223+
"blog": some_blog.pk,
224+
"bodyText": "body_text",
225+
"headline": "headline",
226+
},
227+
"relationships": {
228+
"author": {"data": {"id": "INVALID_ID", "type": "authors"}}
229+
},
230+
}
231+
}
232+
233+
snapshot.assert_match(perform_error_test(client, data))

0 commit comments

Comments
 (0)