@@ -28,6 +28,7 @@ def test_included_data_on_detail(single_entry, client):
2828 expected_comment_count = single_entry .comment_set .count ()
2929 assert comment_count == expected_comment_count , 'Detail comment count is incorrect'
3030
31+
3132def test_dynamic_related_data_is_included (single_entry , entry_factory , client ):
3233 entry_factory ()
3334 response = client .get (reverse ("entry-detail" , kwargs = {'pk' : single_entry .pk }) + '?include=suggested' )
@@ -39,14 +40,74 @@ def test_dynamic_related_data_is_included(single_entry, entry_factory, client):
3940
4041def test_missing_field_not_included (author_bio_factory , author_factory , client ):
4142 # First author does not have a bio
42- author = author_factory ()
43+ author = author_factory (bio = None )
4344 response = client .get (reverse ('author-detail' , args = [author .pk ])+ '?include=bio' )
4445 data = load_json (response .content )
4546 assert 'included' not in data
4647 # Second author does
47- bio = author_bio_factory ()
48- response = client .get (reverse ('author-detail' , args = [bio . author .pk ])+ '?include=bio' )
48+ author = author_factory ()
49+ response = client .get (reverse ('author-detail' , args = [author .pk ])+ '?include=bio' )
4950 data = load_json (response .content )
5051 assert 'included' in data
5152 assert len (data ['included' ]) == 1
52- assert data ['included' ][0 ]['attributes' ]['body' ] == bio .body
53+ assert data ['included' ][0 ]['attributes' ]['body' ] == author .bio .body
54+
55+
56+ def test_deep_included_data_on_list (multiple_entries , client ):
57+ response = client .get (reverse ("entry-list" ) + '?include=comments,comments.author,'
58+ 'comments.author.bio&page_size=5' )
59+ included = load_json (response .content ).get ('included' )
60+
61+ assert len (load_json (response .content )['data' ]) == len (multiple_entries ), 'Incorrect entry count'
62+ assert [x .get ('type' ) for x in included ] == [
63+ 'authorBios' , 'authorBios' , 'authors' , 'authors' , 'comments' , 'comments'
64+ ], 'List included types are incorrect'
65+
66+ comment_count = len ([resource for resource in included if resource ["type" ] == "comments" ])
67+ expected_comment_count = sum ([entry .comment_set .count () for entry in multiple_entries ])
68+ assert comment_count == expected_comment_count , 'List comment count is incorrect'
69+
70+ author_count = len ([resource for resource in included if resource ["type" ] == "authors" ])
71+ expected_author_count = sum (
72+ [entry .comment_set .filter (author__isnull = False ).count () for entry in multiple_entries ])
73+ assert author_count == expected_author_count , 'List author count is incorrect'
74+
75+ author_bio_count = len ([resource for resource in included if resource ["type" ] == "authorBios" ])
76+ expected_author_bio_count = sum ([entry .comment_set .filter (
77+ author__bio__isnull = False ).count () for entry in multiple_entries ])
78+ assert author_bio_count == expected_author_bio_count , 'List author bio count is incorrect'
79+
80+ # Also include entry authors
81+ response = client .get (reverse ("entry-list" ) + '?include=authors,comments,comments.author,'
82+ 'comments.author.bio&page_size=5' )
83+ included = load_json (response .content ).get ('included' )
84+
85+ assert len (load_json (response .content )['data' ]) == len (multiple_entries ), 'Incorrect entry count'
86+ assert [x .get ('type' ) for x in included ] == [
87+ 'authorBios' , 'authorBios' , 'authors' , 'authors' , 'authors' , 'authors' ,
88+ 'comments' , 'comments' ], 'List included types are incorrect'
89+
90+ author_count = len ([resource for resource in included if resource ["type" ] == "authors" ])
91+ expected_author_count = sum (
92+ [entry .authors .count () for entry in multiple_entries ] +
93+ [entry .comment_set .filter (author__isnull = False ).count () for entry in multiple_entries ])
94+ assert author_count == expected_author_count , 'List author count is incorrect'
95+
96+
97+ def test_deep_included_data_on_detail (single_entry , client ):
98+ # Same test as in list but also ensures that intermediate resources (here comments' authors)
99+ # are returned along with the leaf nodes
100+ response = client .get (reverse ("entry-detail" , kwargs = {'pk' : single_entry .pk }) +
101+ '?include=comments,comments.author.bio' )
102+ included = load_json (response .content ).get ('included' )
103+
104+ assert [x .get ('type' ) for x in included ] == ['authorBios' , 'authors' , 'comments' ], \
105+ 'Detail included types are incorrect'
106+
107+ comment_count = len ([resource for resource in included if resource ["type" ] == "comments" ])
108+ expected_comment_count = single_entry .comment_set .count ()
109+ assert comment_count == expected_comment_count , 'Detail comment count is incorrect'
110+
111+ author_bio_count = len ([resource for resource in included if resource ["type" ] == "authorBios" ])
112+ expected_author_bio_count = single_entry .comment_set .filter (author__bio__isnull = False ).count ()
113+ assert author_bio_count == expected_author_bio_count , 'Detail author bio count is incorrect'
0 commit comments