32 lines
871 B
Python
32 lines
871 B
Python
from __future__ import annotations
|
|
|
|
import unittest
|
|
|
|
from iaai_scraper.core.utils import deep_find_key
|
|
|
|
|
|
class TestDeepFindKey(unittest.TestCase):
|
|
def test_finds_key_in_nested_structure(self) -> None:
|
|
payload = {
|
|
"root": {
|
|
"target": "a",
|
|
"nested": [{"target": "b"}, {"x": 1}],
|
|
}
|
|
}
|
|
|
|
found = deep_find_key(payload, {"target"})
|
|
self.assertEqual(found, ["a", "b"])
|
|
|
|
def test_respects_max_depth(self) -> None:
|
|
payload = {"l1": {"l2": {"l3": {"target": "value"}}}}
|
|
|
|
found_too_shallow = deep_find_key(payload, {"target"}, max_depth=2)
|
|
found_enough_depth = deep_find_key(payload, {"target"}, max_depth=8)
|
|
|
|
self.assertEqual(found_too_shallow, [])
|
|
self.assertEqual(found_enough_depth, ["value"])
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|