Skip to content

Commit 310b1d2

Browse files
author
Andrea Velosa
committed
Update tests for include support
1 parent 064d627 commit 310b1d2

File tree

3 files changed

+271
-7
lines changed

3 files changed

+271
-7
lines changed

tests/serializers.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,6 @@
33
from tests.models import BasicModel, RelatedModel, RelatedModelTwo
44

55

6-
class BasicModelSerializer(ModelSerializer):
7-
class Meta:
8-
fields = "__all__"
9-
model = BasicModel
10-
11-
126
class RelatedModelSerializer(ModelSerializer):
137
class Meta:
148
fields = "__all__"
@@ -19,3 +13,13 @@ class RelatedModelTwoSerializer(ModelSerializer):
1913
class Meta:
2014
fields = "__all__"
2115
model = RelatedModelTwo
16+
17+
18+
class BasicModelSerializer(ModelSerializer):
19+
class Meta:
20+
fields = "__all__"
21+
model = BasicModel
22+
23+
included_serializers = {
24+
"to_one": RelatedModelSerializer,
25+
}

tests/test_parsers.py

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,19 @@ def test_parse(self):
6666
{"type": "tags", "id": "2"},
6767
{"type": "tags", "id": "3"}
6868
]
69+
}, {
70+
"op": "update",
71+
"data": {
72+
"id": "13",
73+
"type": "articles",
74+
"attributes": {
75+
"title": "New Title"
76+
}
77+
},
78+
"meta": {
79+
"include": ["author"]
80+
}
81+
6982
}
7083
]
7184
}
@@ -104,7 +117,14 @@ def test_parse(self):
104117
"type": "articles",
105118
"tags": [{'type': 'tags', 'id': '2'}, {'type': 'tags', 'id': '3'}]
106119
}
107-
}
120+
}, {
121+
"update": {
122+
"id": "13",
123+
"type": "articles",
124+
"title": "New Title",
125+
"meta": {"include": ["author"]}
126+
}
127+
},
108128
]
109129
self.assertEqual(expected_result, result)
110130

@@ -457,3 +477,32 @@ def test_primary_data_with_id_and_lid(self):
457477
"parser_context": self.parser_context
458478
}
459479
)
480+
481+
def test_invalid_include_value_in_operation_meta(self):
482+
data = {
483+
ATOMIC_OPERATIONS: [
484+
{
485+
"op": "update",
486+
"data": {
487+
"id": "1",
488+
"type": "articles",
489+
"attributes": {
490+
"title": "Title Change"
491+
},
492+
},
493+
"meta": {
494+
"include": 123
495+
}
496+
}
497+
]
498+
}
499+
stream = BytesIO(json.dumps(data).encode("utf-8"))
500+
self.assertRaisesRegex(
501+
JsonApiParseError,
502+
"Received operation include value is not a list",
503+
self.parser.parse,
504+
**{
505+
"stream": stream,
506+
"parser_context": self.parser_context
507+
}
508+
)

tests/test_views.py

Lines changed: 211 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -947,3 +947,214 @@ def test_adding_resource_with_lid_relationship(self):
947947

948948
self.assertDictEqual(expected_result,
949949
json.loads(response.content))
950+
951+
def test_view_processing_with_include_request(self):
952+
operations = [
953+
{
954+
"op": "add",
955+
"data": {
956+
"type": "BasicModel",
957+
"attributes": {
958+
"text": "JSON API paints my bikeshed!"
959+
}
960+
}
961+
}, {
962+
"op": "add",
963+
"data": {
964+
"type": "RelatedModel",
965+
"attributes": {
966+
"text": "JSON API paints my bikeshed!"
967+
}
968+
}
969+
}, {
970+
"op": "add",
971+
"data": {
972+
"type": "RelatedModelTwo",
973+
"attributes": {
974+
"text": "JSON API paints my bikeshed!"
975+
}
976+
}
977+
}, {
978+
"op": "update",
979+
"ref": {
980+
"id": "1",
981+
"type": "BasicModel",
982+
"relationship": "to_one"
983+
},
984+
"data": {"type": "RelatedModel", "id": "1"},
985+
}, {
986+
"op": "update",
987+
"data": {
988+
"id": "1",
989+
"type": "BasicModel",
990+
"relationships": {
991+
"to_many": {
992+
"data": [
993+
{
994+
"type": "RelatedModelTwo",
995+
"id": "1"
996+
}
997+
]
998+
}
999+
}
1000+
},
1001+
"meta": {
1002+
"include": ["to_one"]
1003+
}
1004+
}
1005+
]
1006+
1007+
data = {
1008+
ATOMIC_OPERATIONS: operations
1009+
}
1010+
1011+
response = self.client.post(
1012+
path="/",
1013+
data=data,
1014+
content_type=ATOMIC_CONTENT_TYPE,
1015+
1016+
**{"HTTP_ACCEPT": ATOMIC_CONTENT_TYPE}
1017+
)
1018+
1019+
# check response
1020+
self.assertEqual(200, response.status_code)
1021+
1022+
expected_result = {
1023+
ATOMIC_RESULTS: [
1024+
{
1025+
"data": {
1026+
"id": "1",
1027+
1028+
"type": "BasicModel",
1029+
"attributes": {
1030+
"text": "JSON API paints my bikeshed!"
1031+
},
1032+
"relationships": {
1033+
"to_one": {"data": None},
1034+
"to_many": {"data": [], "meta": {"count": 0}},
1035+
}
1036+
}
1037+
},
1038+
{
1039+
"data": {
1040+
"id": "1",
1041+
"type": "RelatedModel",
1042+
"attributes": {
1043+
"text": "JSON API paints my bikeshed!"
1044+
}
1045+
}
1046+
},
1047+
{
1048+
"data": {
1049+
"id": "1",
1050+
"type": "RelatedModelTwo",
1051+
"attributes": {
1052+
"text": "JSON API paints my bikeshed!"
1053+
}
1054+
}
1055+
},
1056+
{
1057+
"data": {
1058+
"id": "1",
1059+
"type": "BasicModel",
1060+
"attributes": {
1061+
"text": "JSON API paints my bikeshed!"
1062+
},
1063+
"relationships": {
1064+
"to_many": {
1065+
"data": [
1066+
{
1067+
"id": "1",
1068+
"type": "RelatedModelTwo"
1069+
}
1070+
],
1071+
"meta": {
1072+
"count": 1
1073+
}
1074+
},
1075+
"to_one": {
1076+
"data": {
1077+
"id":"1",
1078+
"type":"RelatedModel"
1079+
}
1080+
},
1081+
}
1082+
1083+
},
1084+
"included": [
1085+
{"id": "1","type":"RelatedModel","attributes":{"text":"JSON API paints my bikeshed!"}},
1086+
]
1087+
},
1088+
]
1089+
}
1090+
1091+
self.assertDictEqual(expected_result,
1092+
json.loads(response.content))
1093+
1094+
def test_view_processing_with_invalid_include_request(self):
1095+
operations = [
1096+
{
1097+
"op": "add",
1098+
"data": {
1099+
"type": "BasicModel",
1100+
"attributes": {
1101+
"text": "JSON API paints my bikeshed!"
1102+
}
1103+
}
1104+
}, {
1105+
"op": "add",
1106+
"data": {
1107+
"id": "1",
1108+
"type": "RelatedModelTwo",
1109+
"attributes": {
1110+
"text": "JSON API paints my bikeshed!"
1111+
}
1112+
}
1113+
}, {
1114+
"op": "update",
1115+
"data": {
1116+
"id": "1",
1117+
"type": "BasicModel",
1118+
"relationships": {
1119+
"to_many": {
1120+
"data": [
1121+
{
1122+
"type": "RelatedModelTwo",
1123+
"id": "1"
1124+
}
1125+
]
1126+
}
1127+
}
1128+
},
1129+
"meta": {
1130+
"include": ["to_many"]
1131+
}
1132+
}
1133+
]
1134+
data = {
1135+
ATOMIC_OPERATIONS: operations
1136+
}
1137+
1138+
response = self.client.post(
1139+
path="/",
1140+
data=data,
1141+
content_type=ATOMIC_CONTENT_TYPE,
1142+
1143+
**{"HTTP_ACCEPT": ATOMIC_CONTENT_TYPE}
1144+
)
1145+
1146+
# check response
1147+
self.assertEqual(400, response.status_code)
1148+
1149+
expected_result = {
1150+
"errors": [
1151+
{
1152+
"code": "parse_error",
1153+
"detail":"This endpoint does not support the include parameter for path to_many",
1154+
"source": { "pointer":"/data" },
1155+
"status":"400"
1156+
}
1157+
]
1158+
}
1159+
self.assertDictEqual(expected_result,
1160+
json.loads(response.content))

0 commit comments

Comments
 (0)