-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp_helpers_test.py
183 lines (123 loc) · 6.25 KB
/
app_helpers_test.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
import unittest
import app_helpers as a
# to run:
# from project root:
# python -m unittest discover -s modules -p "*_test.py" --verbose
# to run using green:
# from project root:
# green
# target specific test (in general, dotted names):
# green modules.combinations_test.CombinationsTest
# green modules.combinations_test.NChooseRTest
class IsStringObjectTest(unittest.TestCase):
def test_determines_that_a_non_json_string_with_string_values_is_an_object(self):
source_string: str = "{ key1: 'value 1', key2: 'value 2' }"
expected_result: bool = True
result: bool = a.is_string_object(source_string)
self.assertEqual(expected_result, result)
def test_determines_that_a_json_string_with_string_values_is_an_object(self):
source_string: str = "{ 'key1': 'value 1', 'key2': 'value 2' }"
expected_result: bool = True
result: bool = a.is_string_object(source_string)
self.assertEqual(expected_result, result)
def test_determines_that_a_non_json_string_with_int_values_is_an_object(self):
source_string: str = "{ key1: 1, key2: 2 }"
expected_result: bool = True
result: bool = a.is_string_object(source_string)
self.assertEqual(expected_result, result)
def test_determines_that_a_spaceless_non_json_string_with_string_values_is_an_object(self):
source_string: str = "{key1:'value 1',key2:'value 2'}"
expected_result: bool = True
result: bool = a.is_string_object(source_string)
self.assertEqual(expected_result, result)
def test_determines_that_a_spaceless_json_string_with_string_values_is_an_object(self):
source_string: str = "{'key1':'value 1','key2':'value 2'}"
expected_result: bool = True
result: bool = a.is_string_object(source_string)
self.assertEqual(expected_result, result)
def test_determines_that_a_spaceless_non_json_string_with_int_values_is_an_object(self):
source_string: str = "{key1:1,key2:2}"
expected_result: bool = True
result: bool = a.is_string_object(source_string)
self.assertEqual(expected_result, result)
def test_determines_that_an_int_string_is_not_an_object(self):
source_string: str = "12345"
expected_result: bool = False
result: bool = a.is_string_object(source_string)
self.assertEqual(expected_result, result)
def test_determines_that_a_char_string_is_not_an_object(self):
source_string: str = "hello there"
expected_result: bool = False
result: bool = a.is_string_object(source_string)
self.assertEqual(expected_result, result)
class IsStringJsonObjectTest(unittest.TestCase):
def test_determines_that_a_json_string_with_string_values_is_a_json_object(self):
source_string: str = '{ "key1": "value 1", "key2": "value 2" }'
expected_result: bool = True
result: bool = a.is_string_json_object(source_string)
self.assertEqual(expected_result, result)
def test_determines_that_a_spaceless_json_string_with_string_values_is_a_json_object(self):
source_string: str = '{"key1":"value 1","key2":"value 2"}'
expected_result: bool = True
result: bool = a.is_string_json_object(source_string)
self.assertEqual(expected_result, result)
def test_determines_that_a_json_string_with_int_values_is_a_json_object(self):
source_string: str = '{ "key1": 123, "key2": 234 }'
expected_result: bool = True
result: bool = a.is_string_json_object(source_string)
self.assertEqual(expected_result, result)
def test_determines_that_an_object_string_with_unquoted_keys_is_not_a_json_object(self):
source_string: str = '{ key1: 123, key2: 234 }'
expected_result: bool = False
result: bool = a.is_string_json_object(source_string)
self.assertEqual(expected_result, result)
def test_determines_that_a_json_string_with_reversed_quotation_marks_is_not_a_json_object(self):
source_string: str = "{'key1':'value 1','key2':'value 2'}"
expected_result: bool = False
result: bool = a.is_string_json_object(source_string)
self.assertEqual(expected_result, result)
def test_determines_that_an_int_string_is_not_a_json_object(self):
source_string: str = "12345"
expected_result: bool = False
result: bool = a.is_string_object(source_string)
self.assertEqual(expected_result, result)
def test_determines_that_a_char_string_is_not_a_json_object(self):
source_string: str = "hello there"
expected_result: bool = False
result: bool = a.is_string_object(source_string)
self.assertEqual(expected_result, result)
class StringJsonObjectToDictTest(unittest.TestCase):
def test_converts_a_json_string_with_string_values_to_a_dict(self):
source_string: str = '{ "key1": "value 1", "key2": "value 2" }'
expected_result: dict = {'key1': 'value 1', 'key2': 'value 2'}
result: dict = a.string_json_object_to_dict(source_string)
self.assertEqual(expected_result, result)
def test_converts_a_spaceless_json_string_with_string_values_to_a_dict(self):
source_string: str = '{"key1":"value 1","key2":"value 2"}'
expected_result: dict = {'key1': 'value 1', 'key2': 'value 2'}
result: dict = a.string_json_object_to_dict(source_string)
self.assertEqual(expected_result, result)
def test_throws_an_error_if_given_a_non_json_object_string(self):
source_string: str = "{key1:1,key2:2}"
try:
a.string_json_object_to_dict(source_string)
except ValueError:
self.assertTrue(1, 1)
return
self.fail('no exception raised')
def test_throws_an_error_if_given_an_int_string(self):
source_string: str = "12345"
try:
a.string_json_object_to_dict(source_string)
except ValueError:
self.assertTrue(1, 1)
return
self.fail('no exception raised')
def returns_a_char_string_unchanged(self):
source_string: str = "hello there"
try:
a.string_json_object_to_dict(source_string)
except ValueError:
self.assertTrue(1, 1)
return
self.fail('no exception raised')