forked from russellballestrini/nested-lookup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_nested_loopkup.py
331 lines (305 loc) · 10.9 KB
/
test_nested_loopkup.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
328
329
330
331
from unittest import TestCase
from nested_lookup import nested_lookup, get_all_keys, get_occurrence_of_key,\
get_occurrence_of_value
class TestNestedLookup(TestCase):
def setUp(self):
self.subject_dict = {"a": 1, "b": {"d": 100}, "c": {"d": 200}}
self.subject_dict2 = {
"name": "Russell Ballestrini",
"email_address": "test1@example.com",
"other": {
"secondary_email": "test2@example.com",
"EMAIL_RECOVERY": "test3@example.com",
"email_address": "test4@example.com",
},
}
self.subject_dict3 = {
"build_version": {
"model_name": "MacBook Pro",
"build_version": {
"processor_name": "Intel Core i7",
"processor_speed": "2.7 GHz",
"core_details": {
"build_version": "4",
"l2_cache(per_core)": "256 KB"
}
},
"number_of_cores": "4",
"memory": "256 KB",
},
"os_details": {
"product_version": "10.13.6",
"build_version": "17G65"
},
"name": "Test",
"date": "YYYY-MM-DD HH:MM:SS"
}
def test_nested_lookup(self):
results = nested_lookup("d", self.subject_dict)
self.assertEqual(2, len(results))
self.assertIn(100, results)
self.assertIn(200, results)
self.assertSetEqual({100, 200}, set(results))
def test_nested_lookup_wrapped_in_list(self):
results = nested_lookup("d", [{}, self.subject_dict, {}])
self.assertEqual(2, len(results))
self.assertIn(100, results)
self.assertIn(200, results)
self.assertSetEqual({100, 200}, set(results))
def test_nested_lookup_wrapped_in_list_in_dict_in_list(self):
results = nested_lookup("d", [{}, {"H": [self.subject_dict]}])
self.assertEqual(2, len(results))
self.assertIn(100, results)
self.assertIn(200, results)
self.assertSetEqual({100, 200}, set(results))
def test_nested_lookup_wrapped_in_list_in_list(self):
results = nested_lookup("d", [{}, [self.subject_dict, {}]])
self.assertEqual(2, len(results))
self.assertIn(100, results)
self.assertIn(200, results)
self.assertSetEqual({100, 200}, set(results))
def test_wild_nested_lookup(self):
results = nested_lookup(
key="mail", document=self.subject_dict2, wild=True)
self.assertEqual(4, len(results))
self.assertIn("test1@example.com", results)
self.assertIn("test2@example.com", results)
self.assertIn("test3@example.com", results)
def test_wild_with_keys_nested_lookup(self):
matches = nested_lookup(
key="mail", document=self.subject_dict2, wild=True, with_keys=True
)
self.assertEqual(3, len(matches))
self.assertIn("email_address", matches)
self.assertIn("secondary_email", matches)
self.assertIn("EMAIL_RECOVERY", matches)
self.assertSetEqual({
"test1@example.com", "test4@example.com"},
set(matches["email_address"])
)
self.assertIn("test2@example.com", matches["secondary_email"])
def test_nested_lookup_with_keys(self):
matches = nested_lookup("d", self.subject_dict, with_keys=True)
self.assertIn("d", matches)
self.assertEqual(2, len(matches["d"]))
self.assertSetEqual({100, 200}, set(matches["d"]))
def test_after_key_is_found(self):
result = nested_lookup(
key='build_version', document=self.subject_dict3
)
self.assertEqual(4, len(result))
self.assertIn('4', result)
self.assertIn('17G65', result)
match1 = {
'processor_name': 'Intel Core i7',
'processor_speed': '2.7 GHz',
'core_details': {
'build_version': '4',
'l2_cache(per_core)': '256 KB'
}
}
self.assertIn(match1, result)
match2 = {
'build_version': {
'processor_name': 'Intel Core i7',
'processor_speed': '2.7 GHz',
'core_details': {
'build_version': '4',
'l2_cache(per_core)': '256 KB'
}
},
'memory': '256 KB',
'model_name': 'MacBook Pro',
'number_of_cores': '4'
}
self.assertIn(match2, result)
class TestGetAllKeys(TestCase):
def setUp(self):
self.sample1 = {
"hardware_details": {
"model_name": "MacBook Pro",
"processor_details": {
"processor_name": "Intel Core i7",
"processor_speed": "2.7 GHz",
"core_details": {
"total_numberof_cores": "4",
"l2_cache(per_core)": "256 KB",
},
},
"total_number_of_cores": "4",
"memory": "16 GB",
},
"os_details": {
"product_version": "10.13.6", "build_version": "17G65"
},
"name": "Test",
"date": "YYYY-MM-DD HH:MM:SS",
}
self.sample2 = {
"hardware_details": {
"model_name": "MacBook Pro",
"processor_details": [
{
"processor_name": "Intel Core i7",
"processor_speed": "2.7 GHz",
"core_details": {
"total_numberof_cores": "4",
"l2_cache(per_core)": "256 KB",
},
}
],
"total_number_of_cores": "4",
"memory": "16 GB",
}
}
self.sample3 = {
"hardware_details": {
"model_name": "MacBook Pro",
"processor_details": [
{
"processor_name": "Intel Core i7",
"processor_speed": "2.7 GHz"
},
{
"total_numberof_cores": "4",
"l2_cache(per_core)": "256 KB"
},
],
"total_number_of_cores": "4",
"memory": "16 GB",
}
}
self.sample4 = {
"values": [
{
"checks": [
{
"monitoring_zones": [
"mzdfw",
"mzfra",
"mzhkg",
"mziad",
"mzlon",
"mzord",
"mzsyd"
]
}
]
}
]
}
def test_sample_data1(self):
result = get_all_keys(self.sample1)
self.assertEqual(15, len(result))
keys_to_verify = [
"model_name",
"core_details",
"l2_cache(per_core)",
"build_version",
"date",
]
for key in keys_to_verify:
self.assertIn(key, result)
def test_sample_data2(self):
result = get_all_keys(self.sample2)
self.assertEqual(10, len(result))
keys_to_verify = [
"hardware_details",
"processor_speed",
"total_numberof_cores",
"memory",
]
for key in keys_to_verify:
self.assertIn(key, result)
def test_sample_data3(self):
result = get_all_keys(self.sample3)
self.assertEqual(9, len(result))
keys_to_verify = [
"processor_details",
"processor_name",
"l2_cache(per_core)",
"total_number_of_cores",
]
for key in keys_to_verify:
self.assertIn(key, result)
def test_sample_data4(self):
result = get_all_keys(self.sample4)
self.assertEqual(3, len(result))
keys_to_verify = [
"values",
"checks",
"monitoring_zones"
]
for key in keys_to_verify:
class TestGetOccurrence(TestCase):
def setUp(self):
self.sample1 = {
"build_version": {
"model_name": "MacBook Pro",
"build_version": {
"processor_name": "Intel Core i7",
"processor_speed": "2.7 GHz",
"core_details": {
"build_version": "4",
"l2_cache(per_core)": "256 KB"
}
},
"number_of_cores": "4",
"memory": "256 KB",
},
"os_details": {
"product_version": "10.13.6",
"build_version": "17G65"
},
"name": "Test",
"date": "YYYY-MM-DD HH:MM:SS"
}
self.sample2 = {
"hardware_details": {
"model_name": "MacBook Pro",
"processor_details": [{
"processor_name": "4",
"processor_speed": "2.7 GHz",
"core_details": {
"total_numberof_cores": "4",
"l2_cache(per_core)": "256 KB"
}
}],
"total_number_of_cores": "4",
"memory": "16 GB",
}
}
self.sample3 = {
"hardware_details": {
"model_name": "MacBook Pro",
"processor_details": [
{
"total_number_of_cores": "4",
"processor_speed": "2.7 GHz",
},
{
"total_number_of_cores": "4",
"l2_cache(per_core)": "256 KB"
}
],
"total_number_of_cores": "4",
"memory": "16 GB",
}
}
def test_sample_data1(self):
result = get_occurrence_of_key(self.sample1, 'build_version')
self.assertEqual(4, result)
result = get_occurrence_of_value(self.sample1, '256 KB')
self.assertEqual(2, result)
def test_sample_data2(self):
result = get_occurrence_of_key(self.sample2, 'core_details')
self.assertEqual(1, result)
result = get_occurrence_of_value(self.sample2, '4')
self.assertEqual(3, result)
def test_sample_data3(self):
result = get_occurrence_of_key(self.sample3, 'total_number_of_cores')
self.assertEqual(3, result)
result = get_occurrence_of_value(self.sample3, '4')
self.assertEqual(3, result)
if __name__ == "__main__":
pass