def test_nested_lookup_multiple_occurrences_should_return_all_of_them( self): my_list = { "first_layer": [ { "my_key": 1 }, { "key_2": 2 }, { "my_key": 3 }, ], "second_layer": [ { "key_3": 3 }, { "my_key": 5 }, { "key_4": 4 }, { "my_key": 7 }, ] } found_values = list(nested_lookup('my_key', my_list)) self.assertCountEqual([1, 3, 5, 7], found_values)
def test_nested_lookup_key_missing_should_return_None(self): my_list = [ { "key_1": 1 }, { "key_2": 2 }, { "key_3": 3 }, ] result_list = list(nested_lookup('missing_key', my_list)) self.assertEqual(len(result_list), 0)
def test_nested_lookup_dict_in_list(self): my_list = [ { "key_1": 1 }, { "key_2": 2 }, { "my_key": 42 }, { "key_3": 3 }, ] self.assertEqual(42, next(nested_lookup('my_key', my_list)))
def find_external_dirs(self): dirs = [] found_paths = [] for spec in self.toCreate: res = nested_lookup('-external', spec['template']) for x in res: path = os.path.join(x['-dir'], x['-file']) external_nsmap = spec['template'].get('-nsmap', {}).copy() external_nsmap.update(x['-specification'].get('-nsmap', {})) x['-specification']['-nsmap'] = external_nsmap if path not in found_paths: found_paths.append(path) dirs.append((x['-file'], x['-dir'], x['-specification'], x.get('-pointer'), spec['data'])) return dirs
def test_nested_lookup_list_in_dict(self): my_dict = { "first_layer": [ { "key_1": 1 }, { "key_2": 2 }, { "my_key": 42 }, { "key_3": 3 }, ] } self.assertEqual(42, next(nested_lookup('my_key', my_dict)))
def test_nested_lookup_nested_dict_two_layer(self): my_dict = {"first_layer": {"my_key": 42}} self.assertEqual(42, next(nested_lookup('my_key', my_dict)))