forked from vbabiy/djangorestframework-camel-case
-
Notifications
You must be signed in to change notification settings - Fork 1
/
tests.py
121 lines (105 loc) · 3.59 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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from copy import deepcopy
from unittest import TestCase
from djangorestframework_camel_case.util import camelize, underscoreize
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,
"no_underscore_before123": 8
}
output = {
"twoWord": 1,
"longKeyWithManyUnderscores": 2,
"only1Key": 3,
"onlyOneLetterA": 4,
"bOnlyOneLetter": 5,
"onlyCLetter": 6,
"mix123123aAndLetters": 7,
"noUnderscoreBefore123": 8
}
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,
}
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
}
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)
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)