|
4 | 4 | from django.utils import timezone
|
5 | 5 | from rest_framework.reverse import reverse
|
6 | 6 |
|
7 |
| -from rest_framework_json_api.settings import JSONAPISettings |
| 7 | +from rest_framework_json_api.settings import JSONAPISettings, \ |
| 8 | + ATTRIBUTE_RENDERING_STRATEGY, RELATIONS_RENDERING_STRATEGY |
8 | 9 | from . import TestBase
|
9 | 10 | from example.models import Author, Blog, Comment, Entry
|
10 | 11 | from django.test import override_settings
|
@@ -39,7 +40,7 @@ def setUp(self):
|
39 | 40 | )
|
40 | 41 |
|
41 | 42 | def test_attribute_rendering_strategy(self):
|
42 |
| - with override_settings(JSON_API_NESTED_SERIALIZERS_RENDERING_STRATEGY='ATTRIBUTE'): |
| 43 | + with override_settings(JSON_API_NESTED_SERIALIZERS_RENDERING_STRATEGY=ATTRIBUTE_RENDERING_STRATEGY): |
43 | 44 | response = self.client.get(self.list_url)
|
44 | 45 |
|
45 | 46 | expected = {
|
@@ -79,6 +80,118 @@ def test_attribute_rendering_strategy(self):
|
79 | 80 | }
|
80 | 81 | assert expected == response.json()
|
81 | 82 |
|
| 83 | + def test_relations_rendering_strategy(self): |
| 84 | + with override_settings(JSON_API_NESTED_SERIALIZERS_RENDERING_STRATEGY=RELATIONS_RENDERING_STRATEGY): |
| 85 | + response = self.client.get(self.list_url) |
| 86 | + |
| 87 | + expected = { |
| 88 | + "links": { |
| 89 | + "first": "http://testserver/authors-nested?page%5Bnumber%5D=1", |
| 90 | + "last": "http://testserver/authors-nested?page%5Bnumber%5D=5", |
| 91 | + "next": "http://testserver/authors-nested?page%5Bnumber%5D=2", |
| 92 | + "prev": None |
| 93 | + }, |
| 94 | + "data": [ |
| 95 | + { |
| 96 | + "type": "authors", |
| 97 | + "id": "1", |
| 98 | + "attributes": { |
| 99 | + "name": "some_author1", |
| 100 | + |
| 101 | + }, |
| 102 | + "relationships": { |
| 103 | + "comments": { |
| 104 | + "data": [ |
| 105 | + { |
| 106 | + "type": "comments", |
| 107 | + "id": "1" |
| 108 | + } |
| 109 | + ] |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + ], |
| 114 | + "meta": { |
| 115 | + "pagination": { |
| 116 | + "page": 1, |
| 117 | + "pages": 5, |
| 118 | + "count": 5 |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | + assert expected == response.json() |
| 123 | + |
| 124 | + def test_relations_rendering_strategy_included(self): |
| 125 | + with override_settings(JSON_API_NESTED_SERIALIZERS_RENDERING_STRATEGY=RELATIONS_RENDERING_STRATEGY): |
| 126 | + response = self.client.get(self.list_url, data={'include': 'comments,comments.entry'}) |
| 127 | + |
| 128 | + expected = { |
| 129 | + "links": { |
| 130 | + "first": "http://testserver/authors-nested?include=comments%2Ccomments.entry&page%5Bnumber%5D=1", |
| 131 | + "last": "http://testserver/authors-nested?include=comments%2Ccomments.entry&page%5Bnumber%5D=5", |
| 132 | + "next": "http://testserver/authors-nested?include=comments%2Ccomments.entry&page%5Bnumber%5D=2", |
| 133 | + "prev": None |
| 134 | + }, |
| 135 | + "data": [ |
| 136 | + { |
| 137 | + "type": "authors", |
| 138 | + "id": "1", |
| 139 | + "attributes": { |
| 140 | + "name": "some_author1", |
| 141 | + |
| 142 | + }, |
| 143 | + "relationships": { |
| 144 | + "comments": { |
| 145 | + "data": [ |
| 146 | + { |
| 147 | + "type": "comments", |
| 148 | + "id": "1" |
| 149 | + } |
| 150 | + ] |
| 151 | + } |
| 152 | + } |
| 153 | + } |
| 154 | + ], |
| 155 | + "included": [ |
| 156 | + { |
| 157 | + "type": "comments", |
| 158 | + "id": "1", |
| 159 | + "attributes": { |
| 160 | + "body": "testing one two three" |
| 161 | + }, |
| 162 | + "relationships": { |
| 163 | + "entry": { |
| 164 | + "data": { |
| 165 | + "type": "entries", |
| 166 | + "id": "1" |
| 167 | + } |
| 168 | + } |
| 169 | + } |
| 170 | + }, |
| 171 | + { |
| 172 | + "type": "entries", |
| 173 | + "id": "1", |
| 174 | + "attributes": {}, |
| 175 | + "relationships": { |
| 176 | + "tags": { |
| 177 | + "data": [] |
| 178 | + } |
| 179 | + }, |
| 180 | + "links": { |
| 181 | + "self": "http://testserver/drf-blogs/1" |
| 182 | + } |
| 183 | + } |
| 184 | + ], |
| 185 | + "meta": { |
| 186 | + "pagination": { |
| 187 | + "page": 1, |
| 188 | + "pages": 5, |
| 189 | + "count": 5 |
| 190 | + } |
| 191 | + } |
| 192 | + } |
| 193 | + assert expected == response.json() |
| 194 | + |
82 | 195 |
|
83 | 196 | class TestRenderingStrategySettings(TestBase):
|
84 | 197 |
|
|
0 commit comments