Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0892e3a

Browse files
authored
Convert parsers test to pytest style (#898)
1 parent 0cc7750 commit 0892e3a

File tree

6 files changed

+234
-182
lines changed

6 files changed

+234
-182
lines changed

‎example/tests/test_parsers.py‎

Lines changed: 0 additions & 145 deletions
This file was deleted.

‎tests/conftest.py‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import pytest
2+
from rest_framework.test import APIClient
23

34
from tests.models import (
45
BasicModel,
@@ -51,3 +52,8 @@ def many_to_many_targets(db):
5152
ManyToManyTarget.objects.create(name="Target1"),
5253
ManyToManyTarget.objects.create(name="Target2"),
5354
]
55+
56+
57+
@pytest.fixture
58+
def client():
59+
return APIClient()

‎tests/test_parsers.py‎

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
import json
2+
from io import BytesIO
3+
4+
import pytest
5+
from rest_framework.exceptions import ParseError
6+
7+
from rest_framework_json_api.parsers import JSONParser
8+
from rest_framework_json_api.utils import format_value
9+
from tests.views import BasicModelViewSet
10+
11+
12+
class TestJSONParser:
13+
@pytest.fixture
14+
def parser(self):
15+
return JSONParser()
16+
17+
@pytest.fixture
18+
def parse(self, parser, parser_context):
19+
def parse_wrapper(data):
20+
stream = BytesIO(json.dumps(data).encode("utf-8"))
21+
return parser.parse(stream, None, parser_context)
22+
23+
return parse_wrapper
24+
25+
@pytest.fixture
26+
def parser_context(self, rf):
27+
return {"request": rf.post("/"), "kwargs": {}, "view": BasicModelViewSet()}
28+
29+
@pytest.mark.parametrize(
30+
"format_field_names",
31+
[
32+
None,
33+
"dasherize",
34+
"camelize",
35+
"capitalize",
36+
"underscore",
37+
],
38+
)
39+
def test_parse_formats_field_names(
40+
self,
41+
settings,
42+
format_field_names,
43+
parse,
44+
):
45+
settings.JSON_API_FORMAT_FIELD_NAMES = format_field_names
46+
47+
data = {
48+
"data": {
49+
"id": "123",
50+
"type": "BasicModel",
51+
"attributes": {
52+
format_value("test_attribute", format_field_names): "test-value"
53+
},
54+
"relationships": {
55+
format_value("test_relationship", format_field_names): {
56+
"data": {"type": "TestRelationship", "id": "123"}
57+
}
58+
},
59+
}
60+
}
61+
62+
result = parse(data)
63+
assert result == {
64+
"id": "123",
65+
"test_attribute": "test-value",
66+
"test_relationship": {"id": "123", "type": "TestRelationship"},
67+
}
68+
69+
def test_parse_extracts_meta(self, parse):
70+
data = {
71+
"data": {
72+
"type": "BasicModel",
73+
},
74+
"meta": {"random_key": "random_value"},
75+
}
76+
77+
result = parse(data)
78+
assert result["_meta"] == data["meta"]
79+
80+
def test_parse_preserves_json_value_field_names(self, settings, parse):
81+
settings.JSON_API_FORMAT_FIELD_NAMES = "dasherize"
82+
83+
data = {
84+
"data": {
85+
"type": "BasicModel",
86+
"attributes": {"json-value": {"JsonKey": "JsonValue"}},
87+
},
88+
}
89+
90+
result = parse(data)
91+
assert result["json_value"] == {"JsonKey": "JsonValue"}
92+
93+
def test_parse_raises_error_on_empty_data(self, parse):
94+
data = []
95+
96+
with pytest.raises(ParseError) as excinfo:
97+
parse(data)
98+
assert "Received document does not contain primary data" == str(excinfo.value)
99+
100+
def test_parse_fails_on_list_of_objects(self, parse):
101+
data = {
102+
"data": [
103+
{
104+
"type": "BasicModel",
105+
"attributes": {"json-value": {"JsonKey": "JsonValue"}},
106+
}
107+
],
108+
}
109+
110+
with pytest.raises(ParseError) as excinfo:
111+
parse(data)
112+
113+
assert "Received data is not a valid JSONAPI Resource Identifier Object" == str(
114+
excinfo.value
115+
)
116+
117+
def test_parse_fails_when_id_is_missing_on_patch(self, rf, parse, parser_context):
118+
parser_context["request"] = rf.patch("/")
119+
data = {
120+
"data": {
121+
"type": "BasicModel",
122+
},
123+
}
124+
125+
with pytest.raises(ParseError) as excinfo:
126+
parse(data)
127+
128+
assert "The resource identifier object must contain an 'id' member" == str(
129+
excinfo.value
130+
)

‎tests/test_relations.py‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,14 @@
1111
SerializerMethodHyperlinkedRelatedField,
1212
)
1313
from rest_framework_json_api.utils import format_link_segment
14-
from rest_framework_json_api.views import ModelViewSet, RelationshipView
14+
from rest_framework_json_api.views import RelationshipView
1515
from tests.models import BasicModel
1616
from tests.serializers import (
1717
ForeignKeySourceSerializer,
1818
ManyToManySourceReadOnlySerializer,
1919
ManyToManySourceSerializer,
2020
)
21+
from tests.views import BasicModelViewSet
2122

2223

2324
@pytest.mark.django_db
@@ -266,11 +267,6 @@ def test_get_links(
266267
# Routing setup
267268

268269

269-
class BasicModelViewSet(ModelViewSet):
270-
class Meta:
271-
model = BasicModel
272-
273-
274270
class BasicModelRelationshipView(RelationshipView):
275271
queryset = BasicModel.objects
276272

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /