1
1
import pytest
2
2
from django .core .urlresolvers import reverse
3
3
4
- from example .tests .utils import load_json
5
-
6
4
pytestmark = pytest .mark .django_db
7
5
8
6
@@ -14,9 +12,9 @@ def test_default_included_data_on_list(multiple_entries, client):
14
12
15
13
def test_included_data_on_list (multiple_entries , client , query = '?include=comments&page_size=5' ):
16
14
response = client .get (reverse ("entry-list" ) + query )
17
- included = load_json ( response .content ).get ('included' )
15
+ included = response .json ( ).get ('included' )
18
16
19
- assert len (load_json ( response .content )['data' ]) == len (multiple_entries ), (
17
+ assert len (response .json ( )['data' ]) == len (multiple_entries ), (
20
18
'Incorrect entry count'
21
19
)
22
20
assert [x .get ('type' ) for x in included ] == ['comments' , 'comments' ], (
@@ -34,7 +32,7 @@ def test_default_included_data_on_detail(single_entry, client):
34
32
35
33
def test_included_data_on_detail (single_entry , client , query = '?include=comments' ):
36
34
response = client .get (reverse ("entry-detail" , kwargs = {'pk' : single_entry .pk }) + query )
37
- included = load_json ( response .content ).get ('included' )
35
+ included = response .json ( ).get ('included' )
38
36
39
37
assert [x .get ('type' ) for x in included ] == ['comments' ], 'Detail included types are incorrect'
40
38
@@ -48,7 +46,7 @@ def test_dynamic_related_data_is_included(single_entry, entry_factory, client):
48
46
response = client .get (
49
47
reverse ("entry-detail" , kwargs = {'pk' : single_entry .pk }) + '?include=featured'
50
48
)
51
- included = load_json ( response .content ).get ('included' )
49
+ included = response .json ( ).get ('included' )
52
50
53
51
assert [x .get ('type' ) for x in included ] == ['entries' ], 'Dynamic included types are incorrect'
54
52
assert len (included ) == 1 , 'The dynamically included blog entries are of an incorrect count'
@@ -59,7 +57,7 @@ def test_dynamic_many_related_data_is_included(single_entry, entry_factory, clie
59
57
response = client .get (
60
58
reverse ("entry-detail" , kwargs = {'pk' : single_entry .pk }) + '?include=suggested'
61
59
)
62
- included = load_json ( response .content ).get ('included' )
60
+ included = response .json ( ).get ('included' )
63
61
64
62
assert included
65
63
assert [x .get ('type' ) for x in included ] == ['entries' ], 'Dynamic included types are incorrect'
@@ -69,12 +67,11 @@ def test_missing_field_not_included(author_bio_factory, author_factory, client):
69
67
# First author does not have a bio
70
68
author = author_factory (bio = None )
71
69
response = client .get (reverse ('author-detail' , args = [author .pk ]) + '?include=bio' )
72
- data = load_json (response .content )
73
- assert 'included' not in data
70
+ assert 'included' not in response .json ()
74
71
# Second author does
75
72
author = author_factory ()
76
73
response = client .get (reverse ('author-detail' , args = [author .pk ]) + '?include=bio' )
77
- data = load_json ( response .content )
74
+ data = response .json ( )
78
75
assert 'included' in data
79
76
assert len (data ['included' ]) == 1
80
77
assert data ['included' ][0 ]['attributes' ]['body' ] == author .bio .body
@@ -83,9 +80,9 @@ def test_missing_field_not_included(author_bio_factory, author_factory, client):
83
80
def test_deep_included_data_on_list (multiple_entries , client ):
84
81
response = client .get (reverse ("entry-list" ) + '?include=comments,comments.author,'
85
82
'comments.author.bio,comments.writer&page_size=5' )
86
- included = load_json ( response .content ).get ('included' )
83
+ included = response .json ( ).get ('included' )
87
84
88
- assert len (load_json ( response .content )['data' ]) == len (multiple_entries ), (
85
+ assert len (response .json ( )['data' ]) == len (multiple_entries ), (
89
86
'Incorrect entry count'
90
87
)
91
88
assert [x .get ('type' ) for x in included ] == [
@@ -117,9 +114,9 @@ def test_deep_included_data_on_list(multiple_entries, client):
117
114
# Also include entry authors
118
115
response = client .get (reverse ("entry-list" ) + '?include=authors,comments,comments.author,'
119
116
'comments.author.bio&page_size=5' )
120
- included = load_json ( response .content ).get ('included' )
117
+ included = response .json ( ).get ('included' )
121
118
122
- assert len (load_json ( response .content )['data' ]) == len (multiple_entries ), (
119
+ assert len (response .json ( )['data' ]) == len (multiple_entries ), (
123
120
'Incorrect entry count'
124
121
)
125
122
assert [x .get ('type' ) for x in included ] == [
@@ -138,7 +135,7 @@ def test_deep_included_data_on_detail(single_entry, client):
138
135
# are returned along with the leaf nodes
139
136
response = client .get (reverse ("entry-detail" , kwargs = {'pk' : single_entry .pk }) +
140
137
'?include=comments,comments.author.bio' )
141
- included = load_json ( response .content ).get ('included' )
138
+ included = response .json ( ).get ('included' )
142
139
143
140
assert [x .get ('type' ) for x in included ] == ['authorBios' , 'authors' , 'comments' ], \
144
141
'Detail included types are incorrect'
0 commit comments