This repository has been archived by the owner on Jul 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 49
/
test_nested_lookup.py
445 lines (408 loc) · 15.3 KB
/
test_nested_lookup.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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
from unittest import TestCase
from nested_lookup import (
nested_lookup,
get_all_keys,
get_occurrence_of_key,
get_occurrence_of_value,
get_occurrences_and_values
)
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": "[email protected]",
"other": {
"secondary_email": "[email protected]",
"EMAIL_RECOVERY": "[email protected]",
"email_address": "[email protected]",
},
}
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",
}
self.subject_dict4 = {1: "a", 2: {"b": 44, "C": 55}, 3: "d", 4: "e", "6776": "works"}
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_nested_lookup_key_is_non_str(self):
results = nested_lookup(key=4, document=self.subject_dict4)
self.assertIn("e", 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("[email protected]", results)
self.assertIn("[email protected]", results)
self.assertIn("[email protected]", results)
def test_wild_nested_lookup_integer_keys_in_document(self):
results = nested_lookup(key="c", document=self.subject_dict4, wild=True)
self.assertIn(55, results)
def test_wild_nested_lookup_integer_key_as_substring(self):
# test that wild works converts integers into strings before substring matching.
results = nested_lookup(key=77, document=self.subject_dict4, wild=True)
self.assertIn("works", 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(
{"[email protected]", "[email protected]"}, set(matches["email_address"])
)
self.assertIn("[email protected]", 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",
]
}
]
}
]
}
self.sample5 = [
{
"listings": [
{
"name": "title",
"postcode": "postcode",
"full_address": "fulladdress",
"city": "city",
"lat": "latitude",
"lng": "longitude",
}
]
}
]
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:
self.assertIn(key, result)
def test_sample_data5(self):
result = get_all_keys(self.sample5)
self.assertEqual(7, len(result))
keys_to_verify = [
"listings",
"name",
"postcode",
"full_address",
"city",
"lat",
"lng",
]
for key in keys_to_verify:
self.assertIn(key, result)
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",
}
}
self.sample4 = {
"values": [
{
"checks": [
{
"monitoring_zones": [
"mzdfw",
"mzfra",
"mzhkg",
"mziad",
"mzlon",
"mzord",
"mzsyd",
]
}
]
}
]
}
self.sample5 = {
"hardware_details": {
"model_name": "MacBook Pro",
"total_number_of_cores": 0,
"memory": False,
}
}
self.sample6 = [
{
"processor_name": "4",
"processor_speed": "2.7 GHz",
"core_details": {
"total_numberof_cores": "4",
"l2_cache(per_core)": "256 KB",
}
}
]
self.sample7 = [
{
"processor_name": "4",
"processor_speed": "2.7 GHz",
"core_details": {
"total_numberof_cores": "4",
"l2_cache(per_core)": "256 KB",
}
},
{
"processor_name": "4",
"processor_speed": "2.7 GHz",
"core_details": {
"total_numberof_cores": "4",
"l2_cache(per_core)": "256 KB",
}
}
]
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)
def test_sample_data4(self):
result = get_occurrence_of_key(self.sample4, "checks")
self.assertEqual(1, result)
result = get_occurrence_of_value(self.sample4, "mziad")
self.assertEqual(1, result)
# Add one more value in key "monitoring_zones" and verify
self.sample4["values"][0]["checks"][0]["monitoring_zones"].append("mziad")
self.assertEqual(2, get_occurrence_of_value(self.sample4, "mziad"))
def test_sample_data5(self):
self.assertEqual(
1, get_occurrence_of_key(self.sample5, "total_number_of_cores")
)
self.assertEqual(1, get_occurrence_of_key(self.sample5, "memory"))
# Add key 'memory' and verify
self.sample5["memory"] = 0
self.assertEqual(2, get_occurrence_of_key(self.sample5, "memory"))
def test_sample_data6(self):
value = '4'
result = get_occurrences_and_values(self.sample6, value)
self.assertEqual(2, result[value]['occurrences'])
self.assertEqual(2, len(result[value]['values']))
def test_sample_data7(self):
value = '2.7 GHz'
result = get_occurrences_and_values(self.sample6, value)
self.assertEqual(1, result[value]['occurrences'])
self.assertEqual(1, len(result[value]['values']))
def test_sample_data8(self):
value = '4'
result = get_occurrences_and_values(self.sample7, value)
self.assertEqual(4, result[value]['occurrences'])
self.assertEqual(4, len(result[value]['values']))
def test_sample_data9(self):
value = '5'
result = get_occurrences_and_values(self.sample7, value)
self.assertEqual(0, result[value]['occurrences'])
self.assertEqual(0, len(result[value]['values']))
if __name__ == "__main__":
pass