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