-
Notifications
You must be signed in to change notification settings - Fork 122
/
Copy pathtests.py
327 lines (286 loc) · 11.6 KB
/
tests.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
316
317
318
319
320
321
322
323
324
325
326
327
from copy import deepcopy
from unittest import TestCase, mock
from django.conf import settings
from django.http import QueryDict
from django.utils.functional import lazy
from django.test.client import RequestFactory
from rest_framework.utils.serializer_helpers import ReturnDict
from djangorestframework_camel_case.util import camelize, underscoreize
settings.configure()
from djangorestframework_camel_case.middleware import CamelCaseMiddleWare
class ImportTest(TestCase):
def test_import_all(self):
"""
A quick test that just imports everything, should crash in case any Django or DRF modules change
"""
from djangorestframework_camel_case import parser
from djangorestframework_camel_case import render
from djangorestframework_camel_case import settings
from djangorestframework_camel_case import middleware
assert parser
assert render
assert settings
assert middleware
class UnderscoreToCamelTestCase(TestCase):
def test_under_to_camel_keys(self):
data = {
"two_word": 1,
"long_key_with_many_underscores": 2,
"only_1_key": 3,
"only_one_letter_a": 4,
"b_only_one_letter": 5,
"only_c_letter": 6,
"mix_123123a_and_letters": 7,
"mix_123123aa_and_letters_complex": 8,
"no_underscore_before123": 9,
}
output = {
"twoWord": 1,
"longKeyWithManyUnderscores": 2,
"only1Key": 3,
"onlyOneLetterA": 4,
"bOnlyOneLetter": 5,
"onlyCLetter": 6,
"mix123123aAndLetters": 7,
"mix123123aaAndLettersComplex": 8,
"noUnderscoreBefore123": 9,
}
self.assertEqual(camelize(data), output)
def test_tuples(self):
data = {"multiple_values": (1, 2), "data": [1, 3, 4]}
output = {"multipleValues": [1, 2], "data": [1, 3, 4]}
self.assertEqual(camelize(data), output)
def test_camel_to_under_input_untouched_for_sequence(self):
data = [{"firstInput": 1}, {"secondInput": 2}]
reference_input = deepcopy(data)
camelize(data)
self.assertEqual(data, reference_input)
def test_recursive_camelize_with_ignored_fields_and_keys(self):
ignore_fields = ("ignored_field", "newKeyIgnoredField", "ignored_field_and_key", "ignoredFieldAndKey2")
ignore_keys = ("ignored_key", "newKeyIgnoredKey", "ignored_field_and_key", "ignoredFieldAndKey2")
data = {
"ignored_field": {"no_change_recursive": 1},
"change_me": {"change_recursive": 2},
"new_key_ignored_field": {"also_no_change": 3},
"ignored_key": {"also_change_recursive": 4},
"new_key_ignored_key": {"change_is_here_to_stay": 5},
"ignored_field_and_key": {"no_change_here": 6},
"ignored_field_and_key2": {"no_change_here_either": 7},
}
output = {
"ignoredField": {"no_change_recursive": 1},
"changeMe": {"changeRecursive": 2},
"newKeyIgnoredField": {"also_no_change": 3},
"ignored_key": {"alsoChangeRecursive": 4},
"new_key_ignored_key": {"changeIsHereToStay": 5},
"ignored_field_and_key": {"no_change_here": 6},
"ignored_field_and_key2": {"no_change_here_either": 7},
}
self.assertEqual(camelize(data, ignore_fields=ignore_fields, ignore_keys=ignore_keys), output)
class CamelToUnderscoreTestCase(TestCase):
def test_camel_to_under_keys(self):
data = {
"twoWord": 1,
"longKeyWithManyUnderscores": 2,
"only1Key": 3,
"onlyOneLetterA": 4,
"bOnlyOneLetter": 5,
"onlyCLetter": 6,
"mix123123aAndLetters": 7,
"mix123123aaAndLettersComplex": 8,
"wordWITHCaps": 9,
"key10": 10,
"anotherKey1": 11,
"anotherKey10": 12,
"optionS1": 13,
"optionS10": 14,
"UPPERCASE": 15,
"PascalCase": 16,
"Pascal10Case": 17,
}
output = {
"two_word": 1,
"long_key_with_many_underscores": 2,
"only_1_key": 3,
"only_one_letter_a": 4,
"b_only_one_letter": 5,
"only_c_letter": 6,
"mix_123123a_and_letters": 7,
"mix_123123aa_and_letters_complex": 8,
"word_with_caps": 9,
"key_10": 10,
"another_key_1": 11,
"another_key_10": 12,
"option_s_1": 13,
"option_s_10": 14,
"uppercase": 15,
"pascal_case": 16,
"pascal_10_case": 17,
}
self.assertEqual(underscoreize(data), output)
def test_camel_to_under_keys_with_no_underscore_before_number(self):
data = {"noUnderscoreBefore123": 1}
output = {"no_underscore_before123": 1}
options = {"no_underscore_before_number": True}
self.assertEqual(underscoreize(data, **options), output)
def test_under_to_camel_input_untouched_for_sequence(self):
data = [{"first_input": 1}, {"second_input": 2}]
reference_input = deepcopy(data)
underscoreize(data)
self.assertEqual(data, reference_input)
def test_recursive_underscoreize_with_ignored_fields_and_keys(self):
ignore_fields = ("ignoredField", "new_key_ignored_field", "ignoredFieldAndKey", "ignored_field_and_key_2")
ignore_keys = ("ignoredKey", "new_key_ignored_key", "ignoredFieldAndKey", "ignored_field_and_key_2")
data = {
"ignoredField": {"noChangeRecursive": 1},
"changeMeField": {"changeRecursive": 2},
"newKeyIgnoredField": {"alsoNoChange": 3},
"ignoredKey": {"changeRecursiveAgain": 4},
"newKeyIgnoredKey": {"changeIsHereToStay": 5},
"ignoredFieldAndKey": {"noChangeHere": 6},
"ignoredFieldAndKey2": {"noChangeHereEither": 7},
}
output = {
"ignored_field": {"noChangeRecursive": 1},
"change_me_field": {"change_recursive": 2},
"new_key_ignored_field": {"alsoNoChange": 3},
"ignoredKey": {"change_recursive_again": 4},
"newKeyIgnoredKey": {"change_is_here_to_stay": 5},
"ignoredFieldAndKey": {"noChangeHere": 6},
"ignoredFieldAndKey2": {"noChangeHereEither": 7},
}
self.assertEqual(underscoreize(data, ignore_fields=ignore_fields, ignore_keys=ignore_keys), output)
class NonStringKeyTest(TestCase):
def test_non_string_key(self):
data = {1: "test"}
self.assertEqual(underscoreize(camelize(data)), data)
def return_string(text):
return text
lazy_func = lazy(return_string, str)
class PromiseStringTest(TestCase):
def test_promise_strings(self):
data = {lazy_func("test_key"): lazy_func("test_value value")}
camelized = camelize(data)
self.assertEqual(camelized, {"testKey": "test_value value"})
result = underscoreize(camelized)
self.assertEqual(result, {"test_key": "test_value value"})
class ReturnDictTest(TestCase):
def test_return_dict(self):
data = ReturnDict({"id": 3, "value": "val"}, serializer=object())
camelized = camelize(data)
self.assertEqual(data, camelized)
self.assertEqual(data.serializer, camelized.serializer)
class NumberToCamelTestCase(TestCase):
def test_dict_with_numbers_as_keys(self):
data = {1: 'test', 'a': 'abc'}
self.assertEqual(camelize(data), data)
class GeneratorAsInputTestCase(TestCase):
def _underscore_generator(self):
yield {"simple_is_better": "than complex"}
yield {"that_is": "correct"}
def _camel_generator(self):
yield {"simpleIsBetter": "than complex"}
yield {"thatIs": "correct"}
def test_camelize_iterates_over_generator(self):
data = self._underscore_generator()
output = [{"simpleIsBetter": "than complex"}, {"thatIs": "correct"}]
self.assertEqual(camelize(data), output)
def test_underscoreize_iterates_over_generator(self):
data = self._camel_generator()
output = [{"simple_is_better": "than complex"}, {"that_is": "correct"}]
self.assertEqual(underscoreize(data), output)
class CamelToUnderscoreQueryDictTestCase(TestCase):
def test_camel_to_under_keys(self):
query_dict = QueryDict("testList=1&testList=2", mutable=True)
data = {
"twoWord": 1,
"longKeyWithManyUnderscores": 2,
"only1Key": 3,
"onlyOneLetterA": 4,
"bOnlyOneLetter": 5,
"onlyCLetter": 6,
"mix123123aAndLetters": 7,
"mix123123aaAndLettersComplex": 8,
"wordWITHCaps": 9,
"key10": 10,
"anotherKey1": 11,
"anotherKey10": 12,
"optionS1": 13,
"optionS10": 14,
"UPPERCASE": 15,
"PascalCase": 16,
"Pascal10Case": 17,
}
query_dict.update(data)
output_query = QueryDict("test_list=1&test_list=2", mutable=True)
output = {
"two_word": 1,
"long_key_with_many_underscores": 2,
"only_1_key": 3,
"only_one_letter_a": 4,
"b_only_one_letter": 5,
"only_c_letter": 6,
"mix_123123a_and_letters": 7,
"mix_123123aa_and_letters_complex": 8,
"word_with_caps": 9,
"key_10": 10,
"another_key_1": 11,
"another_key_10": 12,
"option_s_1": 13,
"option_s_10": 14,
"uppercase": 15,
"pascal_case": 16,
"pascal_10_case": 17,
}
output_query.update(output)
self.assertEqual(underscoreize(query_dict), output_query)
class CamelCaseMiddleWareTestCase(TestCase):
def test_camel_case_to_underscore_query_params(self):
get_response_mock = mock.MagicMock()
middleware = CamelCaseMiddleWare(get_response_mock)
query_dict = QueryDict("testList=1&testList=2", mutable=True)
data = {
"twoWord": "1",
"longKeyWithManyUnderscores": "2",
"only1Key": "3",
"onlyOneLetterA": "4",
"bOnlyOneLetter": "5",
"onlyCLetter": "6",
"mix123123aAndLetters": "7",
"mix123123aaAndLettersComplex": "8",
"wordWITHCaps": "9",
"key10": "10",
"anotherKey1": "11",
"anotherKey10": "12",
"optionS1": "13",
"optionS10": "14",
"UPPERCASE": "15",
"PascalCase": "16",
"Pascal10Case": "17",
}
query_dict.update(data)
output_query = QueryDict("test_list=1&test_list=2", mutable=True)
output = {
"two_word": "1",
"long_key_with_many_underscores": "2",
"only_1_key": "3",
"only_one_letter_a": "4",
"b_only_one_letter": "5",
"only_c_letter": "6",
"mix_123123a_and_letters": "7",
"mix_123123aa_and_letters_complex": "8",
"word_with_caps": "9",
"key_10": "10",
"another_key_1": "11",
"another_key_10": "12",
"option_s_1": "13",
"option_s_10": "14",
"uppercase": "15",
"pascal_case": "16",
"pascal_10_case": "17",
}
output_query.update(output)
request = RequestFactory().get("/", query_dict)
middleware(request)
(args, kwargs) = get_response_mock.call_args
self.assertEqual(args[0].GET, output_query)