From a0cbd0139a6df001566c447373a3fe93e3697f24 Mon Sep 17 00:00:00 2001 From: Javier Parada Date: Mon, 27 Dec 2021 12:24:52 +0100 Subject: [PATCH] Add some test and fix default value bug --- Makefile | 2 +- deepfinder/deep_find.py | 23 ++++++------------- tests/unit/deep_find_defaults_test.py | 33 +++++++++++++++++++++++++++ 3 files changed, 41 insertions(+), 17 deletions(-) create mode 100644 tests/unit/deep_find_defaults_test.py diff --git a/Makefile b/Makefile index 43ac686..c676c61 100644 --- a/Makefile +++ b/Makefile @@ -8,5 +8,5 @@ deps: # Run tests .PHONY = test -test: build +test: python -m unittest discover -s ./tests -p '*_test.py' \ No newline at end of file diff --git a/deepfinder/deep_find.py b/deepfinder/deep_find.py index eed57c1..a43972b 100644 --- a/deepfinder/deep_find.py +++ b/deepfinder/deep_find.py @@ -3,30 +3,19 @@ def deep_find( path: str, token: str = '.', default_return: any = None, - default_if_err: any = None, - raise_err: bool = False, ) -> any: """ Description - :param raise_err: - :param default_if_err: :param default_return: default return if function raise an error or param is None. :param token: token to separate attributes. :param obj: obj in which find. :param path: path to wanted attribute. :return: found attribute. """ - result: any = None - try: - path = path.split(token) - if path == ['']: - path = None - result = __rec_helper(obj, path) - except Exception as e: - if raise_err: - raise e - if default_if_err is not None: - return default_if_err + path = path.split(token) + if path == ['']: + path = None + result = __rec_helper(obj, path) if result is not None: return result @@ -45,7 +34,9 @@ def __rec_helper(obj: any, path: [str]): if isinstance(obj, list): if current_path == '*': - return [__rec_helper(sub_obj, path.copy()) for sub_obj in obj] + with_nones_results = [__rec_helper(sub_obj, path.copy()) for sub_obj in obj] + clear_results = [obj for obj in with_nones_results if obj is not None] + return clear_results if current_path == '?': for sub_obj in obj: result = __rec_helper(sub_obj, path.copy()) diff --git a/tests/unit/deep_find_defaults_test.py b/tests/unit/deep_find_defaults_test.py new file mode 100644 index 0000000..4be270a --- /dev/null +++ b/tests/unit/deep_find_defaults_test.py @@ -0,0 +1,33 @@ +import unittest + +from deepfinder import deep_find + + +class TestFindDefault(unittest.TestCase): + def test_with_basic_dict(self): + """ + Return default if attribute is not present + """ + data: dict = {'any': 'any'} + result = deep_find(data, 'some', default_return='default') + self.assertEqual(result, 'default') + + def test_with_list_and_find_all(self): + """ + Return empty list if does not match + """ + data: list = [{'any': 'any'}] + result = deep_find(data, '*.some', default_return='default') + self.assertEqual(result, []) + + def test_with_list_and_find_some(self): + """ + Return default if attribute is not present + """ + data: list = [{'any': 'any'}] + result = deep_find(data, '?.some', default_return='default') + self.assertEqual(result, 'default') + + +if __name__ == '__main__': + unittest.main()