forked from django-json-api/django-rest-framework-json-api
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_includes.py
206 lines (174 loc) · 6.93 KB
/
test_includes.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import pytest
from django.urls import reverse
pytestmark = pytest.mark.django_db
def test_dynamic_related_data_is_included(single_entry, entry_factory, client):
entry_factory()
response = client.get(
reverse("entry-detail", kwargs={"pk": single_entry.pk}) + "?include=featured"
)
included = response.json().get("included")
assert [x.get("type") for x in included] == [
"entries"
], "Dynamic included types are incorrect"
assert (
len(included) == 1
), "The dynamically included blog entries are of an incorrect count"
def test_dynamic_many_related_data_is_included(single_entry, entry_factory, client):
entry_factory()
response = client.get(
reverse("entry-detail", kwargs={"pk": single_entry.pk}) + "?include=suggested"
)
included = response.json().get("included")
assert included
assert [x.get("type") for x in included] == [
"entries"
], "Dynamic included types are incorrect"
def test_missing_field_not_included(author_bio_factory, author_factory, client):
# First author does not have a bio
author = author_factory(bio=None)
response = client.get(reverse("author-detail", args=[author.pk]) + "?include=bio")
assert "included" not in response.json()
# Second author does
author = author_factory()
response = client.get(reverse("author-detail", args=[author.pk]) + "?include=bio")
data = response.json()
assert "included" in data
assert len(data["included"]) == 1
assert data["included"][0]["attributes"]["body"] == author.bio.body
def test_deep_included_data_on_list(multiple_entries, client):
response = client.get(
reverse("entry-list") + "?include=comments,comments.author,"
"comments.author.bio,comments.writer&page[size]=5"
)
included = response.json().get("included")
assert len(response.json()["data"]) == len(
multiple_entries
), "Incorrect entry count"
assert [x.get("type") for x in included] == [
"authorBios",
"authorBios",
"authors",
"authors",
"comments",
"comments",
"writers",
"writers",
], "List included types are incorrect"
comment_count = len(
[resource for resource in included if resource["type"] == "comments"]
)
expected_comment_count = sum(entry.comments.count() for entry in multiple_entries)
assert comment_count == expected_comment_count, "List comment count is incorrect"
author_count = len(
[resource for resource in included if resource["type"] == "authors"]
)
expected_author_count = sum(
entry.comments.filter(author__isnull=False).count()
for entry in multiple_entries
)
assert author_count == expected_author_count, "List author count is incorrect"
author_bio_count = len(
[resource for resource in included if resource["type"] == "authorBios"]
)
expected_author_bio_count = sum(
entry.comments.filter(author__bio__isnull=False).count()
for entry in multiple_entries
)
assert (
author_bio_count == expected_author_bio_count
), "List author bio count is incorrect"
writer_count = len(
[resource for resource in included if resource["type"] == "writers"]
)
expected_writer_count = sum(
entry.comments.filter(author__isnull=False).count()
for entry in multiple_entries
)
assert writer_count == expected_writer_count, "List writer count is incorrect"
# Also include entry authors
response = client.get(
reverse("entry-list") + "?include=authors,comments,comments.author,"
"comments.author.bio&page[size]=5"
)
included = response.json().get("included")
assert len(response.json()["data"]) == len(
multiple_entries
), "Incorrect entry count"
assert [x.get("type") for x in included] == [
"authorBios",
"authorBios",
"authors",
"authors",
"authors",
"authors",
"comments",
"comments",
], "List included types are incorrect"
author_count = len(
[resource for resource in included if resource["type"] == "authors"]
)
expected_author_count = sum(
[entry.authors.count() for entry in multiple_entries]
+ [
entry.comments.filter(author__isnull=False).count()
for entry in multiple_entries
]
)
assert author_count == expected_author_count, "List author count is incorrect"
def test_deep_included_data_on_detail(single_entry, client):
# Same test as in list but also ensures that intermediate resources (here comments' authors)
# are returned along with the leaf nodes
response = client.get(
reverse("entry-detail", kwargs={"pk": single_entry.pk})
+ "?include=comments,comments.author.bio"
)
included = response.json().get("included")
assert [x.get("type") for x in included] == [
"authorBios",
"authors",
"comments",
], "Detail included types are incorrect"
comment_count = len(
[resource for resource in included if resource["type"] == "comments"]
)
expected_comment_count = single_entry.comments.count()
assert comment_count == expected_comment_count, "Detail comment count is incorrect"
author_bio_count = len(
[resource for resource in included if resource["type"] == "authorBios"]
)
expected_author_bio_count = single_entry.comments.filter(
author__bio__isnull=False
).count()
assert (
author_bio_count == expected_author_bio_count
), "Detail author bio count is incorrect"
def test_data_resource_not_included_again(single_comment, client):
# This test makes sure that the resource which is in the data field is excluded
# from the included field.
response = client.get(
reverse("comment-detail", kwargs={"pk": single_comment.pk})
+ "?include=entry.comments"
)
included = response.json().get("included")
included_comments = [
resource for resource in included if resource["type"] == "comments"
]
assert single_comment.pk not in [
int(x.get("id")) for x in included_comments
], "Resource of the data field duplicated in included"
comment_count = len(included_comments)
expected_comment_count = single_comment.entry.comments.count()
# The comment in the data attribute must not be included again.
expected_comment_count -= 1
assert comment_count == expected_comment_count, "Comment count incorrect"
def test_meta_object_added_to_included_resources(single_entry, client):
response = client.get(
reverse("entry-detail", kwargs={"pk": single_entry.pk}) + "?include=comments"
)
assert response.json()["included"][0].get("meta")
response = client.get(
reverse("entry-detail", kwargs={"pk": single_entry.pk})
+ "?include=comments.author"
)
assert response.json()["included"][0].get("meta")
assert response.json()["included"][1].get("meta")