forked from django-json-api/django-rest-framework-json-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_utils.py
315 lines (263 loc) · 9.04 KB
/
test_utils.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
import pytest
from django.db import models
from rest_framework import status
from rest_framework.generics import GenericAPIView
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework_json_api import serializers
from rest_framework_json_api.utils import (
format_field_names,
format_link_segment,
format_resource_type,
format_value,
get_included_serializers,
get_related_resource_type,
get_resource_name,
)
from tests.models import (
BasicModel,
DJAModel,
ForeignKeySource,
ForeignKeyTarget,
ManyToManySource,
ManyToManyTarget,
)
def test_get_resource_name_no_view():
assert get_resource_name({}) is None
@pytest.mark.parametrize(
"format_type,pluralize_type,output",
[
(False, False, "APIView"),
(False, True, "APIViews"),
("dasherize", False, "api-view"),
("dasherize", True, "api-views"),
],
)
def test_get_resource_name_from_view(settings, format_type, pluralize_type, output):
settings.JSON_API_FORMAT_TYPES = format_type
settings.JSON_API_PLURALIZE_TYPES = pluralize_type
view = APIView()
context = {"view": view}
assert output == get_resource_name(context)
@pytest.mark.parametrize(
"format_type,pluralize_type",
[
(False, False),
(False, True),
("dasherize", False),
("dasherize", True),
],
)
def test_get_resource_name_from_view_custom_resource_name(
settings, format_type, pluralize_type
):
settings.JSON_API_FORMAT_TYPES = format_type
settings.JSON_API_PLURALIZE_TYPES = pluralize_type
view = APIView()
view.resource_name = "custom"
context = {"view": view}
assert "custom" == get_resource_name(context)
@pytest.mark.parametrize(
"format_type,pluralize_type,output",
[
(False, False, "BasicModel"),
(False, True, "BasicModels"),
("dasherize", False, "basic-model"),
("dasherize", True, "basic-models"),
],
)
def test_get_resource_name_from_model(settings, format_type, pluralize_type, output):
settings.JSON_API_FORMAT_TYPES = format_type
settings.JSON_API_PLURALIZE_TYPES = pluralize_type
view = APIView()
view.model = BasicModel
context = {"view": view}
assert output == get_resource_name(context)
@pytest.mark.parametrize(
"format_type,pluralize_type,output",
[
(False, False, "BasicModel"),
(False, True, "BasicModels"),
("dasherize", False, "basic-model"),
("dasherize", True, "basic-models"),
],
)
def test_get_resource_name_from_model_serializer_class(
settings, format_type, pluralize_type, output
):
class BasicModelSerializer(serializers.ModelSerializer):
class Meta:
fields = ("text",)
model = BasicModel
settings.JSON_API_FORMAT_TYPES = format_type
settings.JSON_API_PLURALIZE_TYPES = pluralize_type
view = GenericAPIView()
view.serializer_class = BasicModelSerializer
context = {"view": view}
assert output == get_resource_name(context)
@pytest.mark.parametrize(
"format_type,pluralize_type",
[
(False, False),
(False, True),
("dasherize", False),
("dasherize", True),
],
)
def test_get_resource_name_from_model_serializer_class_custom_resource_name(
settings, format_type, pluralize_type
):
class BasicModelSerializer(serializers.ModelSerializer):
class Meta:
fields = ("text",)
model = BasicModel
settings.JSON_API_FORMAT_TYPES = format_type
settings.JSON_API_PLURALIZE_TYPES = pluralize_type
view = GenericAPIView()
view.serializer_class = BasicModelSerializer
view.serializer_class.Meta.resource_name = "custom"
context = {"view": view}
assert "custom" == get_resource_name(context)
@pytest.mark.parametrize(
"format_type,pluralize_type",
[
(False, False),
(False, True),
("dasherize", False),
("dasherize", True),
],
)
def test_get_resource_name_from_plain_serializer_class(
settings, format_type, pluralize_type
):
class PlainSerializer(serializers.Serializer):
class Meta:
resource_name = "custom"
settings.JSON_API_FORMAT_TYPES = format_type
settings.JSON_API_PLURALIZE_TYPES = pluralize_type
view = GenericAPIView()
view.serializer_class = PlainSerializer
context = {"view": view}
assert "custom" == get_resource_name(context)
@pytest.mark.parametrize(
"status_code",
[
status.HTTP_400_BAD_REQUEST,
status.HTTP_403_FORBIDDEN,
status.HTTP_500_INTERNAL_SERVER_ERROR,
],
)
def test_get_resource_name_with_errors(status_code):
view = APIView()
context = {"view": view}
view.response = Response(status=status_code)
assert "errors" == get_resource_name(context)
@pytest.mark.parametrize(
"format_type,output",
[
("camelize", {"fullName": {"last-name": "a", "first-name": "b"}}),
("capitalize", {"FullName": {"last-name": "a", "first-name": "b"}}),
("dasherize", {"full-name": {"last-name": "a", "first-name": "b"}}),
("underscore", {"full_name": {"last-name": "a", "first-name": "b"}}),
],
)
def test_format_field_names(settings, format_type, output):
settings.JSON_API_FORMAT_FIELD_NAMES = format_type
value = {"full_name": {"last-name": "a", "first-name": "b"}}
assert format_field_names(value, format_type) == output
@pytest.mark.parametrize(
"format_type,output",
[
(None, "first_Name"),
("camelize", "firstName"),
("capitalize", "FirstName"),
("dasherize", "first-name"),
("underscore", "first_name"),
],
)
def test_format_field_segment(settings, format_type, output):
settings.JSON_API_FORMAT_RELATED_LINKS = format_type
assert format_link_segment("first_Name") == output
@pytest.mark.parametrize(
"format_type,output",
[
(None, "first_name"),
("camelize", "firstName"),
("capitalize", "FirstName"),
("dasherize", "first-name"),
("underscore", "first_name"),
],
)
def test_format_value(settings, format_type, output):
assert format_value("first_name", format_type) == output
@pytest.mark.parametrize(
"resource_type,pluralize,output",
[
(None, None, "ResourceType"),
("camelize", False, "resourceType"),
("camelize", True, "resourceTypes"),
],
)
def test_format_resource_type(settings, resource_type, pluralize, output):
assert format_resource_type("ResourceType", resource_type, pluralize) == output
@pytest.mark.parametrize(
"model_class,field,output",
[
(ManyToManySource, "targets", "ManyToManyTarget"),
(ManyToManyTarget, "sources", "ManyToManySource"),
(ForeignKeySource, "target", "ForeignKeyTarget"),
(ForeignKeyTarget, "sources", "ForeignKeySource"),
],
)
def test_get_related_resource_type(model_class, field, output):
class RelatedResourceTypeSerializer(serializers.ModelSerializer):
class Meta:
model = model_class
fields = (field,)
serializer = RelatedResourceTypeSerializer()
field = serializer.fields[field]
assert get_related_resource_type(field) == output
@pytest.mark.parametrize(
"related_field_kwargs,output",
[
({"queryset": BasicModel.objects}, "BasicModel"),
({"queryset": BasicModel.objects, "model": BasicModel}, "BasicModel"),
({"model": BasicModel, "read_only": True}, "BasicModel"),
],
)
def test_get_related_resource_type_from_plain_serializer_class(
related_field_kwargs, output
):
class PlainRelatedResourceTypeSerializer(serializers.Serializer):
basic_models = serializers.ResourceRelatedField(
many=True, **related_field_kwargs
)
serializer = PlainRelatedResourceTypeSerializer()
field = serializer.fields["basic_models"]
assert get_related_resource_type(field) == output
class ManyToManyTargetSerializer(serializers.ModelSerializer):
class Meta:
model = ManyToManyTarget
def test_get_included_serializers():
class IncludedSerializersModel(DJAModel):
self = models.ForeignKey("self", on_delete=models.CASCADE)
target = models.ForeignKey(ManyToManyTarget, on_delete=models.CASCADE)
other_target = models.ForeignKey(ManyToManyTarget, on_delete=models.CASCADE)
class Meta:
app_label = "tests"
class IncludedSerializersSerializer(serializers.ModelSerializer):
included_serializers = {
"self": "self",
"target": ManyToManyTargetSerializer,
"other_target": "tests.test_utils.ManyToManyTargetSerializer",
}
class Meta:
model = IncludedSerializersModel
fields = ("self", "other_target", "target")
included_serializers = get_included_serializers(IncludedSerializersSerializer)
expected_included_serializers = {
"self": IncludedSerializersSerializer,
"target": ManyToManyTargetSerializer,
"other_target": ManyToManyTargetSerializer,
}
assert included_serializers == expected_included_serializers