File: test_search.py

package info (click to toggle)
deepdiff 8.1.1-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,716 kB
  • sloc: python: 14,702; makefile: 164; sh: 9
file content (503 lines) | stat: -rw-r--r-- 17,641 bytes parent folder | download | duplicates (2)
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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
#!/usr/bin/env python
import pytest
from deepdiff import DeepSearch, grep
from datetime import datetime
import logging
logging.disable(logging.CRITICAL)

item = "somewhere"


class CustomClass:
    def __init__(self, a, b=None):
        self.a = a
        self.b = b

    def __str__(self):
        return "({}, {})".format(self.a, self.b)

    def __repr__(self):
        return self.__str__()


class TestDeepSearch:
    """DeepSearch Tests."""

    def test_number_in_list(self):
        obj = ["a", 10, 20]
        item = 10
        result = {"matched_values": {'root[1]'}}
        assert DeepSearch(obj, item, verbose_level=1) == result

    def test_number_in_list2(self):
        obj = ["a", "10", 10, 20]
        item = 10
        result = {"matched_values": {'root[2]'}}
        assert DeepSearch(obj, item, verbose_level=1) == result

    def test_number_in_list3(self):
        obj = ["a", "10", 10, 20]
        item = "10"
        result = {"matched_values": {'root[1]'}}
        assert DeepSearch(obj, item, verbose_level=1) == result

    def test_number_in_list_strict_false(self):
        obj = ["a", "10", 10, 20]
        item = "20"
        result = {"matched_values": {'root[3]'}}
        assert DeepSearch(obj, item, verbose_level=1, strict_checking=False) == result

    def test_string_in_root(self):
        obj = "long string somewhere"
        result = {"matched_values": {'root'}}
        assert DeepSearch(obj, item, verbose_level=1) == result

    def test_string_in_root_verbose(self):
        obj = "long string somewhere"
        result = {"matched_values": {'root': "long string somewhere"}}
        assert DeepSearch(obj, item, verbose_level=2) == result

    def test_string_in_tuple(self):
        obj = ("long", "string", 0, "somewhere")
        result = {"matched_values": {'root[3]'}}
        assert DeepSearch(obj, item, verbose_level=1) == result

    def test_string_in_list(self):
        obj = ["long", "string", 0, "somewhere"]
        result = {"matched_values": {'root[3]'}}
        assert DeepSearch(obj, item, verbose_level=1) == result

    def test_string_in_list_verbose(self):
        obj = ["long", "string", 0, "somewhere"]
        result = {"matched_values": {'root[3]': "somewhere"}}
        assert DeepSearch(obj, item, verbose_level=2) == result

    def test_string_in_list_verbose2(self):
        obj = ["long", "string", 0, "somewhere great!"]
        result = {"matched_values": {'root[3]': "somewhere great!"}}
        assert DeepSearch(obj, item, verbose_level=2) == result

    def test_string_in_list_verbose3(self):
        obj = ["long somewhere", "string", 0, "somewhere great!"]
        result = {
            "matched_values": {
                'root[0]': 'long somewhere',
                'root[3]': "somewhere great!"
            }
        }
        assert DeepSearch(obj, item, verbose_level=2) == result

    def test_int_in_dictionary(self):
        obj = {"long": "somewhere", "num": 2, 0: 0, "somewhere": "around"}
        item = 2
        result = {'matched_values': {"root['num']"}}
        ds = DeepSearch(obj, item, verbose_level=1)
        assert ds == result

    def test_string_in_dictionary(self):
        obj = {"long": "somewhere", "string": 2, 0: 0, "somewhere": "around"}
        result = {
            'matched_paths': {"root['somewhere']"},
            'matched_values': {"root['long']"}
        }
        ds = DeepSearch(obj, item, verbose_level=1)
        assert ds == result

    def test_string_in_dictionary_case_insensitive(self):
        obj = {"long": "Somewhere over there!", "string": 2, 0: 0, "SOMEWHERE": "around"}
        result = {
            'matched_paths': {"root['SOMEWHERE']"},
            'matched_values': {"root['long']"}
        }
        ds = DeepSearch(obj, item, verbose_level=1, case_sensitive=False)
        assert ds == result

    def test_string_in_dictionary_key_case_insensitive_partial(self):
        obj = {"SOMEWHERE here": "around"}
        result = {
            'matched_paths': {"root['SOMEWHERE here']"}
        }
        ds = DeepSearch(obj, item, verbose_level=1, case_sensitive=False)
        assert ds == result

    def test_string_in_dictionary_verbose(self):
        obj = {"long": "somewhere", "string": 2, 0: 0, "somewhere": "around"}
        result = {
            'matched_paths': {
                "root['somewhere']": "around"
            },
            'matched_values': {
                "root['long']": "somewhere"
            }
        }
        ds = DeepSearch(obj, item, verbose_level=2)
        assert ds == result

    def test_string_in_dictionary_in_list_verbose(self):
        obj = [
            "something somewhere", {
                "long": "somewhere",
                "string": 2,
                0: 0,
                "somewhere": "around"
            }
        ]
        result = {
            'matched_paths': {
                "root[1]['somewhere']": "around"
            },
            'matched_values': {
                "root[1]['long']": "somewhere",
                "root[0]": "something somewhere"
            }
        }
        ds = DeepSearch(obj, item, verbose_level=2)
        assert ds == result

    def test_custom_object(self):
        obj = CustomClass('here, something', 'somewhere')
        result = {'matched_values': {'root.b'}}
        ds = DeepSearch(obj, item, verbose_level=1)
        assert ds == result

    def test_custom_object_verbose(self):
        obj = CustomClass('here, something', 'somewhere out there')
        result = {'matched_values': {'root.b': 'somewhere out there'}}
        ds = DeepSearch(obj, item, verbose_level=2)
        assert ds == result

    def test_custom_object_in_dictionary_verbose(self):
        obj = {1: CustomClass('here, something', 'somewhere out there')}
        result = {'matched_values': {'root[1].b': 'somewhere out there'}}
        ds = DeepSearch(obj, item, verbose_level=2)
        assert ds == result

    def test_named_tuples_verbose(self):
        from collections import namedtuple
        Point = namedtuple('Point', ['x', 'somewhere_good'])
        obj = Point(x="my keys are somewhere", somewhere_good=22)
        ds = DeepSearch(obj, item, verbose_level=2)
        result = {
            'matched_values': {
                'root.x': 'my keys are somewhere'
            },
            'matched_paths': {
                'root.somewhere_good': 22
            }
        }
        assert ds == result

    def test_string_in_set_verbose(self):
        obj = {"long", "string", 0, "somewhere"}
        # result = {"matched_values": {'root[3]': "somewhere"}}
        ds = DeepSearch(obj, item, verbose_level=2)
        assert list(ds["matched_values"].values())[0] == item

    def test_loop(self):
        class LoopTest:
            def __init__(self, a):
                self.loop = self
                self.a = a

        obj = LoopTest("somewhere around here.")

        ds = DeepSearch(obj, item, verbose_level=1)
        result = {'matched_values': {'root.a'}}
        assert ds == result

    def test_loop_in_lists(self):
        obj = [1, 2, 'somewhere']
        obj.append(obj)

        ds = DeepSearch(obj, item, verbose_level=1)
        result = {'matched_values': {'root[2]'}}
        assert ds == result

    def test_skip_path1(self):
        obj = {
            "for life": "vegan",
            "ingredients": ["no meat", "no eggs", "no dairy", "somewhere"]
        }
        ds = DeepSearch(obj, item, exclude_paths={"root['ingredients']"})
        assert ds == {}

    def test_custom_object_skip_path(self):
        obj = CustomClass('here, something', 'somewhere')
        result = {}
        ds = DeepSearch(obj, item, verbose_level=1, exclude_paths=['root.b'])
        assert ds == result

    def test_skip_list_path(self):
        obj = ['a', 'somewhere']
        ds = DeepSearch(obj, item, exclude_paths=['root[1]'])
        result = {}
        assert ds == result

    def test_skip_dictionary_path(self):
        obj = {1: {2: "somewhere"}}
        ds = DeepSearch(obj, item, exclude_paths=['root[1][2]'])
        result = {}
        assert ds == result

    def test_skip_type_str(self):
        obj = "long string somewhere"
        result = {}
        ds = DeepSearch(obj, item, verbose_level=1, exclude_types=[str])
        assert ds == result

    def test_skip_regexp(self):
        obj = [{'a': 1, 'b': "somewhere"}, {'c': 4, 'b': "somewhere"}]
        ds = DeepSearch(obj, item, exclude_regex_paths=[r"root\[\d+\]"])
        result = {}
        assert ds == result

    def test_skip_regexp2(self):
        obj = {'a': [1, 2, [3, [item]]]}
        ds = DeepSearch(obj, item, exclude_regex_paths=[r"\[\d+\]"])
        result = {}
        assert ds == result

    def test_unknown_parameters(self):
        with pytest.raises(ValueError):
            DeepSearch(1, 1, wrong_param=2)

    def test_bad_attribute(self):
        class Bad:
            __slots__ = ['x', 'y']

            def __getattr__(self, key):
                raise AttributeError("Bad item")

            def __str__(self):
                return "Bad Object"

        obj = Bad()

        ds = DeepSearch(obj, item, verbose_level=1)
        result = {'unprocessed': ['root']}
        assert ds == result
        ds = DeepSearch(obj, item, verbose_level=2)
        assert ds == result

    def test_case_insensitive_of_str_in_list(self):
        obj = ["a", "bb", "BBC", "aBbB"]
        item = "BB"
        result = {"matched_values": {'root[1]', 'root[2]', 'root[3]'}}
        assert DeepSearch(obj, item, verbose_level=1, case_sensitive=False) == result

    def test_case_sensitive_of_str_in_list(self):
        obj = ["a", "bb", "BBC", "aBbB"]
        item = "BB"
        result = {"matched_values": {'root[2]'}}
        assert DeepSearch(obj, item, verbose_level=1, case_sensitive=True) == result

    def test_case_sensitive_of_str_in_one_liner(self):
        obj = "Hello, what's up?"
        item = "WHAT"
        result = {}
        assert DeepSearch(obj, item, verbose_level=1, case_sensitive=True) == result

    def test_case_insensitive_of_str_in_one_liner(self):
        obj = "Hello, what's up?"
        item = "WHAT"
        result = {'matched_values': {'root'}}
        assert DeepSearch(obj, item, verbose_level=1, case_sensitive=False) == result

    def test_none(self):
        obj = item = None
        result = {'matched_values': {'root'}}
        assert DeepSearch(obj, item, verbose_level=1) == result

    def test_complex_obj(self):
        obj = datetime(2017, 5, 4, 1, 1, 1)
        item = datetime(2017, 5, 4, 1, 1, 1)
        result = {'matched_values': {'root'}}
        assert DeepSearch(obj, item, verbose_level=1) == result

    def test_keep_searching_after_obj_match(self):

        class AlwaysEqual:

            def __init__(self, recurse=True):
                if recurse:
                    self.some_attr = AlwaysEqual(recurse=False)

            def __eq__(self, other):
                return True

        obj = AlwaysEqual()
        item = AlwaysEqual()
        result = {'matched_values': {'root', 'root.some_attr'}}
        assert DeepSearch(obj, item, verbose_level=1) == result

    def test_search_inherited_attributes(self):
        class Parent:
            a = 1

        class Child(Parent):
            b = 2

        obj = Child()
        item = 1
        result = {'matched_values': {'root.a'}}
        assert DeepSearch(obj, item, verbose_level=1) == result

    def test_dont_use_regex_by_default(self):
        obj = "long string somewhere"
        item = "some.*"
        result = {}
        assert DeepSearch(obj, item, verbose_level=1) == result

    def test_regex_in_string(self):
        obj = "long string somewhere"
        item = "some.*"
        result = {"matched_values": {"root"}}
        assert DeepSearch(obj, item, verbose_level=1, use_regexp=True) == result

    def test_regex_does_not_match_the_regex_string_itself(self):
        obj = ["We like python", "but not (?:p|t)ython"]
        item = "(?:p|t)ython"
        result = {'matched_values': ['root[0]']}
        assert DeepSearch(obj, item, verbose_level=1, use_regexp=True) == result

    def test_regex_in_string_in_tuple(self):
        obj = ("long", "string", 0, "somewhere")
        item = "some.*"
        result = {"matched_values": {"root[3]"}}
        assert DeepSearch(obj, item, verbose_level=1, use_regexp=True) == result

    def test_regex_in_string_in_list(self):
        obj = ["long", "string", 0, "somewhere"]
        item = "some.*"
        result = {"matched_values": {"root[3]"}}
        assert DeepSearch(obj, item, verbose_level=1, use_regexp=True) == result

    def test_regex_in_string_in_dictionary(self):
        obj = {"long": "somewhere", "string": 2, 0: 0, "somewhere": "around"}
        result = {
            "matched_paths": {"root['somewhere']"},
            "matched_values": {"root['long']"},
        }
        item = "some.*"
        ds = DeepSearch(obj, item, verbose_level=1, use_regexp=True)
        assert ds == result

    def test_regex_in_string_in_dictionary_in_list_verbose(self):
        obj = [
            "something somewhere",
            {"long": "somewhere", "string": 2, 0: 0, "somewhere": "around"},
        ]
        result = {
            "matched_paths": {"root[1]['somewhere']": "around"},
            "matched_values": {
                "root[1]['long']": "somewhere",
                "root[0]": "something somewhere",
            },
        }
        item = "some.*"
        ds = DeepSearch(obj, item, verbose_level=2, use_regexp=True)
        assert ds == result

    def test_regex_in_custom_object(self):
        obj = CustomClass("here, something", "somewhere")
        result = {"matched_values": {"root.b"}}
        item = "somew.*"
        ds = DeepSearch(obj, item, verbose_level=1, use_regexp=True)
        assert ds == result

    def test_regex_in_custom_object_in_dictionary_verbose(self):
        obj = {1: CustomClass("here, something", "somewhere out there")}
        result = {"matched_values": {"root[1].b": "somewhere out there"}}
        item = "somew.*"
        ds = DeepSearch(obj, item, verbose_level=2, use_regexp=True)
        assert ds == result

    def test_regex_in_named_tuples_verbose(self):
        from collections import namedtuple

        Point = namedtuple("Point", ["x", "somewhere_good"])
        obj = Point(x="my keys are somewhere", somewhere_good=22)
        item = "some.*"
        ds = DeepSearch(obj, item, verbose_level=2, use_regexp=True)
        result = {
            "matched_values": {"root.x": "my keys are somewhere"},
            "matched_paths": {"root.somewhere_good": 22},
        }
        assert ds == result

    def test_regex_in_string_in_set_verbose(self):
        obj = {"long", "string", 0, "somewhere"}
        item = "some.*"
        ds = DeepSearch(obj, item, verbose_level=2, use_regexp=True)
        assert list(ds["matched_values"].values())[0] == "somewhere"

    def test_regex_in_int_in_dictionary_with_strict_checking(self):
        obj = {"long": "somewhere", "num": 232, 0: 0, "somewhere": "around"}
        item = "2.*"
        result = {}
        ds = DeepSearch(obj, item, verbose_level=1, use_regexp=True)
        assert ds == result

    def test_regex_in_int_in_dictionary(self):
        obj = {"long": "somewhere", "num": 232, 0: 0, "somewhere": "around"}
        item = "2.*"
        result = {"matched_values": {"root['num']"}}
        ds = DeepSearch(obj, item, verbose_level=1, use_regexp=True, strict_checking=False)
        assert ds == result

    def test_regex_in_int_in_dictionary_returns_partial_match(self):
        obj = {"long": "somewhere", "num": 1123456, 0: 0, "somewhere": "around"}
        item = "1234"
        result = {"matched_values": {"root['num']"}}
        ds = DeepSearch(obj, item, verbose_level=1, use_regexp=True, strict_checking=False)
        assert ds == result

    def test_int_cant_become_regex(self):
        obj = {"long": "somewhere", "num": "1123456", 0: 0, "somewhere": "around"}
        item = CustomClass(a=10)
        with pytest.raises(TypeError) as exp:
            DeepSearch(obj, item, verbose_level=1, use_regexp=True, strict_checking=False)
        assert str(exp.value).startswith("The passed item of (10, None) is not usable for regex")

    def test_searching_for_int_in_dictionary_when_strict_false(self):
        obj = {"long": "somewhere", "num": "1234", 0: 0, "somewhere": "around"}
        item = 1234
        result = {"matched_values": {"root['num']"}}
        ds = DeepSearch(obj, item, verbose_level=1, strict_checking=False)
        assert ds == result


class TestGrep:

    def test_grep_dict(self):
        obj = {
            "for life": "vegan",
            "ingredients": ["no meat", "no eggs", "no dairy", "somewhere"]
        }
        ds = obj | grep(item)
        assert ds == {'matched_values': {"root['ingredients'][3]"}}

    def test_grep_dict_in_dict(self):
        obj = {
            "x": {
                "y": [
                    "aaaaaa\u0142 bbbbb"
                ]
            },
            "z": "z",
        }
        item = {"z": "z"}
        result = obj | grep(item)
        assert {} == result

    def test_grep_with_non_utf8_chars(self):
        obj = "aaaaaa\u0142 bbbbb"
        item = {"z": "z"}
        result = obj | grep(item)
        assert {} == result

    def test_grep_regex_in_string_in_tuple(self):
        obj = ("long", "string", 0, "somewhere")
        item = "some.*"
        result = {"matched_values": {"root[3]"}}
        assert obj | grep(item, verbose_level=1, use_regexp=True) == result