forked from vbabiy/djangorestframework-camel-case
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
189 lines (157 loc) · 5.98 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
from copy import deepcopy
from unittest import TestCase
from django.conf import settings
from django.http import QueryDict
from django.utils.functional import lazy
from djangorestframework_camel_case.util import camelize, underscoreize
settings.configure()
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
assert parser
assert render
assert settings
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)
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,
"anotherKey10": 11,
"optionS10": 12,
}
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_10": 11,
"option_s_10": 12,
}
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)
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.assertEquals(camelized, {"testKey": "test_value value"})
result = underscoreize(camelized)
self.assertEqual(result, {"test_key": "test_value value"})
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,
"anotherKey10": 11,
"optionS10": 12,
}
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_10": 11,
"option_s_10": 12,
}
output_query.update(output)
self.assertEqual(underscoreize(query_dict), output_query)