def test_deep_find_all_dfs_when_found_more_keys_then_return_them_all(self): data = {1: 'first', 2: 'second', 3: {4: {2: 'third'}}} key = 2 data2 = {1: {2: {3: 'first'}}, 3: 'second'} key2 = 3 data3 = {1: {4: {5: 'first'}}, 2: {6: 'second', 5: 'third'}} key3 = 5 data4 = { 1: 'first', 2: { 1: 'second', 4: { 1: 'third' } }, 3: { 1: 'fourth' } } key4 = 1 self.assertEqual(deep_find_all_dfs(data, key), ['second', 'third']) self.assertEqual(deep_find_all_dfs(data2, key2), ['first', 'second']) self.assertEqual(deep_find_all_dfs(data3, key3), ['first', 'third']) self.assertEqual(deep_find_all_dfs(data4, key4), ['first', 'second', 'third', 'fourth'])
def test_deep_find_all_dfs_when_value_is_tuple(self): data = {1: ({2: 'first'}, {2: {2: 'third'}}), 2: 'fourth'} key = 2 self.assertEqual(deep_find_all_dfs(data, key), ['first', { 2: 'third' }, 'third', 'fourth'])
def test_find_all_whean_there_is_no_occurence(self): root = { 'a': { 'aa': 2, 'aaa': { 'ani': 23, 'pesho': ['p', 'e', 's', 'h', 'o'] } }, 'b': 2, 'c': 3 } res = deep_find_all_dfs(root, 'Yoanna') self.assertEqual(res, [])
def test_find_all_keys_with_simple_dictionary(self): root = { 'a': { 'aa': 2, 'aaa': { 'ani': 23, 'pesho': ['p', 'e', 's', 'h', 'o'] } }, 'b': 2, 'c': 3 } res = deep_find_all_dfs(root, 'aa') self.assertEqual(res, [2])
def test_deep_find_all_dfs_when_only_one_key_is_found_on_deeper_level( self): data = { 1: 'first', 2: 'second', 3: { 4: 'third' }, 5: { 6: { 7: 'fourth' } } } key = 7 self.assertEqual(deep_find_all_dfs(data, key), ['fourth'])
def test_deep_find_dfs_with_more_occurances_in_the_dictionary_returns_all_values( self): root = { 'a': { 'aa': 2, 'aaa': { 'ani': 23, 'pesho': ['p', 'e', 's', 'h', 'o'] } }, 'aa': ['doggy', 'catty'], 'b': 2, 'c': 3 } res = deep_find_all_dfs(root, 'aa') self.assertEqual(res, [['doggy', 'catty'], 2])
def test_dfs_finds_all_values_in_the_correct_order(self): data = { 'keys1': { 'key1': 'val3', 'keys2': { 'key1': 'val4' } }, 'key1': 'val1', 'keys2': { 'key1': 'val5' } } key = 'key1' expected = ['val1', 'val3', 'val4', 'val5'] result = deep_find_all_dfs(data, key) self.assertEqual(result, expected)
def test_deep_find_dfs_with_more_occurances_in_the_dictionary_and_more_nested_structeres_returns_al_values( self): root = { 'a': { 'aa': 2, 'aaa': { 'ani': 23, 'pesho': ['p', 'e', 's', 'h', 'o'] }, 'ani': 'panda' }, 'aa': ['doggy', 'catty'], 'ani': 'girl', 'b': 2, 'c': 3 } res = deep_find_all_dfs(root, 'ani') self.assertEqual(res, ['girl', 'panda', 23])
def test_deep_find_all_dfs_when_only_one_key_is_found_on_second_level( self): data = {1: 'first', 2: 'second', 3: {4: 'third'}} key = 4 self.assertEqual(deep_find_all_dfs(data, key), ['third'])
def test_deep_find_all_dfs_when_no_keys_found_then_return_false(self): data = {1: 'first', 2: 'second'} key = 4 self.assertFalse(deep_find_all_dfs(data, key))
def test_deep_find_all_dfs_when_value_is_list(self): data = {1: [{2: 'first'}, {3: {2: 'second'}}], 2: 'third'} key = 2 self.assertEqual(deep_find_all_dfs(data, key), ['first', 'second', 'third'])