|
1 | | -import pytest |
2 | | -from django.core.urlresolvers import reverse |
3 | | - |
4 | | -from example.tests.utils import load_json |
5 | | - |
6 | | -try: |
7 | | - from unittest import mock |
8 | | -except ImportError: |
9 | | - import mock |
10 | | - |
11 | | -pytestmark = pytest.mark.django_db |
12 | | - |
13 | | - |
14 | | -@mock.patch('rest_framework_json_api.utils.get_default_included_resources_from_serializer', new=lambda s: ['comments']) |
15 | | -def test_default_included_data_on_list(multiple_entries, client): |
16 | | - return test_included_data_on_list(multiple_entries=multiple_entries, client=client, query='?page_size=5') |
17 | | - |
18 | | - |
19 | | -def test_included_data_on_list(multiple_entries, client, query='?include=comments&page_size=5'): |
20 | | - response = client.get(reverse("entry-list") + query) |
21 | | - included = load_json(response.content).get('included') |
22 | | - |
23 | | - assert len(load_json(response.content)['data']) == len(multiple_entries), 'Incorrect entry count' |
24 | | - assert [x.get('type') for x in included] == ['comments', 'comments'], 'List included types are incorrect' |
25 | | - |
26 | | - comment_count = len([resource for resource in included if resource["type"] == "comments"]) |
27 | | - expected_comment_count = sum([entry.comment_set.count() for entry in multiple_entries]) |
28 | | - assert comment_count == expected_comment_count, 'List comment count is incorrect' |
29 | | - |
30 | | - |
31 | | -@mock.patch('rest_framework_json_api.utils.get_default_included_resources_from_serializer', new=lambda s: ['comments']) |
32 | | -def test_default_included_data_on_detail(single_entry, client): |
33 | | - return test_included_data_on_detail(single_entry=single_entry, client=client, query='') |
34 | | - |
35 | | - |
36 | | -def test_included_data_on_detail(single_entry, client, query='?include=comments'): |
37 | | - response = client.get(reverse("entry-detail", kwargs={'pk': single_entry.pk}) + query) |
38 | | - included = load_json(response.content).get('included') |
39 | | - |
40 | | - assert [x.get('type') for x in included] == ['comments'], 'Detail included types are incorrect' |
41 | | - |
42 | | - comment_count = len([resource for resource in included if resource["type"] == "comments"]) |
43 | | - expected_comment_count = single_entry.comment_set.count() |
44 | | - assert comment_count == expected_comment_count, 'Detail comment count is incorrect' |
45 | | - |
46 | | - |
47 | | -def test_dynamic_related_data_is_included(single_entry, entry_factory, client): |
48 | | - entry_factory() |
49 | | - response = client.get(reverse("entry-detail", kwargs={'pk': single_entry.pk}) + '?include=featured') |
50 | | - included = load_json(response.content).get('included') |
51 | | - |
52 | | - assert [x.get('type') for x in included] == ['entries'], 'Dynamic included types are incorrect' |
53 | | - assert len(included) == 1, 'The dynamically included blog entries are of an incorrect count' |
54 | | - |
55 | | - |
56 | | -def test_dynamic_many_related_data_is_included(single_entry, entry_factory, client): |
57 | | - entry_factory() |
58 | | - response = client.get(reverse("entry-detail", kwargs={'pk': single_entry.pk}) + '?include=suggested') |
59 | | - included = load_json(response.content).get('included') |
60 | | - |
61 | | - assert included |
62 | | - assert [x.get('type') for x in included] == ['entries'], 'Dynamic included types are incorrect' |
63 | | - |
64 | | - |
65 | | -def test_missing_field_not_included(author_bio_factory, author_factory, client): |
66 | | - # First author does not have a bio |
67 | | - author = author_factory(bio=None) |
68 | | - response = client.get(reverse('author-detail', args=[author.pk])+'?include=bio') |
69 | | - data = load_json(response.content) |
70 | | - assert 'included' not in data |
71 | | - # Second author does |
72 | | - author = author_factory() |
73 | | - response = client.get(reverse('author-detail', args=[author.pk])+'?include=bio') |
74 | | - data = load_json(response.content) |
75 | | - assert 'included' in data |
76 | | - assert len(data['included']) == 1 |
77 | | - assert data['included'][0]['attributes']['body'] == author.bio.body |
78 | | - |
79 | | - |
80 | | -def test_deep_included_data_on_list(multiple_entries, client): |
81 | | - response = client.get(reverse("entry-list") + '?include=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', 'comments', 'comments' |
88 | | - ], 'List included types are incorrect' |
89 | | - |
90 | | - comment_count = len([resource for resource in included if resource["type"] == "comments"]) |
91 | | - expected_comment_count = sum([entry.comment_set.count() for entry in multiple_entries]) |
92 | | - assert comment_count == expected_comment_count, 'List comment count is incorrect' |
93 | | - |
94 | | - author_count = len([resource for resource in included if resource["type"] == "authors"]) |
95 | | - expected_author_count = sum( |
96 | | - [entry.comment_set.filter(author__isnull=False).count() for entry in multiple_entries]) |
97 | | - assert author_count == expected_author_count, 'List author count is incorrect' |
98 | | - |
99 | | - author_bio_count = len([resource for resource in included if resource["type"] == "authorBios"]) |
100 | | - expected_author_bio_count = sum([entry.comment_set.filter( |
101 | | - author__bio__isnull=False).count() for entry in multiple_entries]) |
102 | | - assert author_bio_count == expected_author_bio_count, 'List author bio count is incorrect' |
103 | | - |
104 | | - # Also include entry authors |
105 | | - response = client.get(reverse("entry-list") + '?include=authors,comments,comments.author,' |
106 | | - 'comments.author.bio&page_size=5') |
107 | | - included = load_json(response.content).get('included') |
108 | | - |
109 | | - assert len(load_json(response.content)['data']) == len(multiple_entries), 'Incorrect entry count' |
110 | | - assert [x.get('type') for x in included] == [ |
111 | | - 'authorBios', 'authorBios', 'authors', 'authors', 'authors', 'authors', |
112 | | - 'comments', 'comments'], 'List included types are incorrect' |
113 | | - |
114 | | - author_count = len([resource for resource in included if resource["type"] == "authors"]) |
115 | | - expected_author_count = sum( |
116 | | - [entry.authors.count() for entry in multiple_entries] + |
117 | | - [entry.comment_set.filter(author__isnull=False).count() for entry in multiple_entries]) |
118 | | - assert author_count == expected_author_count, 'List author count is incorrect' |
119 | | - |
120 | | - |
121 | | -def test_deep_included_data_on_detail(single_entry, client): |
122 | | - # Same test as in list but also ensures that intermediate resources (here comments' authors) |
123 | | - # are returned along with the leaf nodes |
124 | | - response = client.get(reverse("entry-detail", kwargs={'pk': single_entry.pk}) + |
125 | | - '?include=comments,comments.author.bio') |
126 | | - included = load_json(response.content).get('included') |
127 | | - |
128 | | - assert [x.get('type') for x in included] == ['authorBios', 'authors', 'comments'], \ |
129 | | - 'Detail included types are incorrect' |
130 | | - |
131 | | - comment_count = len([resource for resource in included if resource["type"] == "comments"]) |
132 | | - expected_comment_count = single_entry.comment_set.count() |
133 | | - assert comment_count == expected_comment_count, 'Detail comment count is incorrect' |
134 | | - |
135 | | - author_bio_count = len([resource for resource in included if resource["type"] == "authorBios"]) |
136 | | - expected_author_bio_count = single_entry.comment_set.filter(author__bio__isnull=False).count() |
137 | | - assert author_bio_count == expected_author_bio_count, 'Detail author bio count is incorrect' |
| 1 | +# import pytest |
| 2 | +# from django.core.urlresolvers import reverse |
| 3 | +# |
| 4 | +# from example.tests.utils import load_json |
| 5 | +# |
| 6 | +# try: |
| 7 | +# from unittest import mock |
| 8 | +# except ImportError: |
| 9 | +# import mock |
| 10 | +# |
| 11 | +# pytestmark = pytest.mark.django_db |
| 12 | +# |
| 13 | +# |
| 14 | +# @mock.patch('rest_framework_json_api.utils.get_default_included_resources_from_serializer', new=lambda s: ['comments']) |
| 15 | +# def test_default_included_data_on_list(multiple_entries, client): |
| 16 | +# return test_included_data_on_list(multiple_entries=multiple_entries, client=client, query='?page_size=5') |
| 17 | +# |
| 18 | +# |
| 19 | +# def test_included_data_on_list(multiple_entries, client, query='?include=comments&page_size=5'): |
| 20 | +# response = client.get(reverse("entry-list") + query) |
| 21 | +# included = load_json(response.content).get('included') |
| 22 | +# |
| 23 | +# assert len(load_json(response.content)['data']) == len(multiple_entries), 'Incorrect entry count' |
| 24 | +# assert [x.get('type') for x in included] == ['comments', 'comments'], 'List included types are incorrect' |
| 25 | +# |
| 26 | +# comment_count = len([resource for resource in included if resource["type"] == "comments"]) |
| 27 | +# expected_comment_count = sum([entry.comment_set.count() for entry in multiple_entries]) |
| 28 | +# assert comment_count == expected_comment_count, 'List comment count is incorrect' |
| 29 | +# |
| 30 | +# |
| 31 | +# @mock.patch('rest_framework_json_api.utils.get_default_included_resources_from_serializer', new=lambda s: ['comments']) |
| 32 | +# def test_default_included_data_on_detail(single_entry, client): |
| 33 | +# return test_included_data_on_detail(single_entry=single_entry, client=client, query='') |
| 34 | +# |
| 35 | +# |
| 36 | +# def test_included_data_on_detail(single_entry, client, query='?include=comments'): |
| 37 | +# response = client.get(reverse("entry-detail", kwargs={'pk': single_entry.pk}) + query) |
| 38 | +# included = load_json(response.content).get('included') |
| 39 | +# |
| 40 | +# assert [x.get('type') for x in included] == ['comments'], 'Detail included types are incorrect' |
| 41 | +# |
| 42 | +# comment_count = len([resource for resource in included if resource["type"] == "comments"]) |
| 43 | +# expected_comment_count = single_entry.comment_set.count() |
| 44 | +# assert comment_count == expected_comment_count, 'Detail comment count is incorrect' |
| 45 | +# |
| 46 | +# |
| 47 | +# def test_dynamic_related_data_is_included(single_entry, entry_factory, client): |
| 48 | +# entry_factory() |
| 49 | +# response = client.get(reverse("entry-detail", kwargs={'pk': single_entry.pk}) + '?include=featured') |
| 50 | +# included = load_json(response.content).get('included') |
| 51 | +# |
| 52 | +# assert [x.get('type') for x in included] == ['entries'], 'Dynamic included types are incorrect' |
| 53 | +# assert len(included) == 1, 'The dynamically included blog entries are of an incorrect count' |
| 54 | +# |
| 55 | +# |
| 56 | +# def test_dynamic_many_related_data_is_included(single_entry, entry_factory, client): |
| 57 | +# entry_factory() |
| 58 | +# response = client.get(reverse("entry-detail", kwargs={'pk': single_entry.pk}) + '?include=suggested') |
| 59 | +# included = load_json(response.content).get('included') |
| 60 | +# |
| 61 | +# assert included |
| 62 | +# assert [x.get('type') for x in included] == ['entries'], 'Dynamic included types are incorrect' |
| 63 | +# |
| 64 | +# |
| 65 | +# def test_missing_field_not_included(author_bio_factory, author_factory, client): |
| 66 | +# # First author does not have a bio |
| 67 | +# author = author_factory(bio=None) |
| 68 | +# response = client.get(reverse('author-detail', args=[author.pk])+'?include=bio') |
| 69 | +# data = load_json(response.content) |
| 70 | +# assert 'included' not in data |
| 71 | +# # Second author does |
| 72 | +# author = author_factory() |
| 73 | +# response = client.get(reverse('author-detail', args=[author.pk])+'?include=bio') |
| 74 | +# data = load_json(response.content) |
| 75 | +# assert 'included' in data |
| 76 | +# assert len(data['included']) == 1 |
| 77 | +# assert data['included'][0]['attributes']['body'] == author.bio.body |
| 78 | +# |
| 79 | +# |
| 80 | +# def test_deep_included_data_on_list(multiple_entries, client): |
| 81 | +# response = client.get(reverse("entry-list") + '?include=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', 'comments', 'comments' |
| 88 | +# ], 'List included types are incorrect' |
| 89 | +# |
| 90 | +# comment_count = len([resource for resource in included if resource["type"] == "comments"]) |
| 91 | +# expected_comment_count = sum([entry.comment_set.count() for entry in multiple_entries]) |
| 92 | +# assert comment_count == expected_comment_count, 'List comment count is incorrect' |
| 93 | +# |
| 94 | +# author_count = len([resource for resource in included if resource["type"] == "authors"]) |
| 95 | +# expected_author_count = sum( |
| 96 | +# [entry.comment_set.filter(author__isnull=False).count() for entry in multiple_entries]) |
| 97 | +# assert author_count == expected_author_count, 'List author count is incorrect' |
| 98 | +# |
| 99 | +# author_bio_count = len([resource for resource in included if resource["type"] == "authorBios"]) |
| 100 | +# expected_author_bio_count = sum([entry.comment_set.filter( |
| 101 | +# author__bio__isnull=False).count() for entry in multiple_entries]) |
| 102 | +# assert author_bio_count == expected_author_bio_count, 'List author bio count is incorrect' |
| 103 | +# |
| 104 | +# # Also include entry authors |
| 105 | +# response = client.get(reverse("entry-list") + '?include=authors,comments,comments.author,' |
| 106 | +# 'comments.author.bio&page_size=5') |
| 107 | +# included = load_json(response.content).get('included') |
| 108 | +# |
| 109 | +# assert len(load_json(response.content)['data']) == len(multiple_entries), 'Incorrect entry count' |
| 110 | +# assert [x.get('type') for x in included] == [ |
| 111 | +# 'authorBios', 'authorBios', 'authors', 'authors', 'authors', 'authors', |
| 112 | +# 'comments', 'comments'], 'List included types are incorrect' |
| 113 | +# |
| 114 | +# author_count = len([resource for resource in included if resource["type"] == "authors"]) |
| 115 | +# expected_author_count = sum( |
| 116 | +# [entry.authors.count() for entry in multiple_entries] + |
| 117 | +# [entry.comment_set.filter(author__isnull=False).count() for entry in multiple_entries]) |
| 118 | +# assert author_count == expected_author_count, 'List author count is incorrect' |
| 119 | +# |
| 120 | +# |
| 121 | +# def test_deep_included_data_on_detail(single_entry, client): |
| 122 | +# # Same test as in list but also ensures that intermediate resources (here comments' authors) |
| 123 | +# # are returned along with the leaf nodes |
| 124 | +# response = client.get(reverse("entry-detail", kwargs={'pk': single_entry.pk}) + |
| 125 | +# '?include=comments,comments.author.bio') |
| 126 | +# included = load_json(response.content).get('included') |
| 127 | +# |
| 128 | +# assert [x.get('type') for x in included] == ['authorBios', 'authors', 'comments'], \ |
| 129 | +# 'Detail included types are incorrect' |
| 130 | +# |
| 131 | +# comment_count = len([resource for resource in included if resource["type"] == "comments"]) |
| 132 | +# expected_comment_count = single_entry.comment_set.count() |
| 133 | +# assert comment_count == expected_comment_count, 'Detail comment count is incorrect' |
| 134 | +# |
| 135 | +# author_bio_count = len([resource for resource in included if resource["type"] == "authorBios"]) |
| 136 | +# expected_author_bio_count = single_entry.comment_set.filter(author__bio__isnull=False).count() |
| 137 | +# assert author_bio_count == expected_author_bio_count, 'Detail author bio count is incorrect' |
0 commit comments