def test_deep_find_dfs_with_one_occurance_in_the_dictionary_with_list_of_dictionaries( self): root = { 'a': { 'aa': 2, 'aaa': { 'ani': 23, 'pesho': ['p', 'e', 's', 'h', 'o'] } }, 'aa': [{ 'doggy': 2, 'names': ['jeny', 'peny'] }, { 'catty': 2, 'names': ['kitty', 'missy'] }], 'b': 2, 'c': 3 } res = deep_find_dfs(root, 'doggy') self.assertEqual(res, 2)
def test_without_dict_in_dict(self): dict = { 'key1': 'val1', 'key2': 'val2' } key = 'key1' result = deep_find_dfs(dict, key) self.assertEqual(result, 'val1')
def test_key_is_in_dict_in_the_dictionary(self): dict = { 'key1': 'val1', 'key2': 'val2', 'keys': { 'key3': 'val3' } } key = 'key3' result = deep_find_dfs(dict, key) self.assertEqual(result, 'val3')
def test_deep_find_dfs_when_found_two_keys_then_return_the_value_of_the_first_found( 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: 'third_level' } }, 2: { 6: 'second_level', 5: 'second_level' } } key3 = 5 self.assertEqual(deep_find_dfs(data, key), 'second') self.assertEqual(deep_find_dfs(data2, key2), 'first') self.assertEqual(deep_find_dfs(data3, key3), 'third_level')
def test_with_many_inside_dictionaries(self): dict = { 'key1': 'val1', 'key2': 'val2', 'keys': { 'key3': 'val3', 'key4': 'val4', 'key5': 'val5' } } key = 'key5' result = deep_find_dfs(dict, key) self.assertEqual(result, 'val5')
def test_deep_find_dfs_with_simple_dictionary_and_no_occurances(self): root = { 'a': { 'aa': 2, 'aaa': { 'ani': 23, 'pesho': ['p', 'e', 's', 'h', 'o'] } }, 'b': 2, 'c': 3 } res = deep_find_dfs(root, 'Anni') self.assertIsNone(res)
def test_deep_find_dfs_with_simple_dictionary(self): root = { 'a': { 'aa': 2, 'aaa': { 'ani': 23, 'pesho': ['p', 'e', 's', 'h', 'o'] } }, 'b': 2, 'c': 3 } res = deep_find_dfs(root, 'ani') self.assertEqual(res, 23)
def test_function_will_return_the_first_found_key_if_many_are(self): dict = { 'keys1': { 'key1': 'val1', 'key2': 'val2' }, 'keys2': { 'key1': 'val3', 'key2': 'val4' } } key = 'key2' result = deep_find_dfs(dict, key) self.assertEqual(result, 'val2')
def test_deep_find_dfs_when_key_is_in_a_deeper_level_then_return_its_value( self): data = { 1: 'first', 2: 'second', 3: { 4: 'third' }, 5: { 6: { 7: 'fourth' } } } key = 7 self.assertEqual(deep_find_dfs(data, key), 'fourth')
def test_with_dictionary_of_dictionaries(self): d = { 'd1': { 'e1': 5, 'e2': 's' }, 'd2': { 'e3': 8, 'e4': 't' }, } # result1 = deep_find_dfs(d, 'e1') result2 = deep_find_dfs(d, 'e4') # self.assertEqual(result1, 5) self.assertEqual(result2, 't')
def test_deep_find_dfs_with_more_occurances_in_the_dictionary_returns_first_occurance( self): root = { 'a': { 'aa': 2, 'aaa': { 'ani': 23, 'pesho': ['p', 'e', 's', 'h', 'o'] } }, 'aa': ['doggy', 'catty'], 'b': 2, 'c': 3 } res = deep_find_dfs(root, 'aa') self.assertEqual(res, ['doggy', 'catty'])
def test_deep_find_dfs_when_key_is_one_level_deeper_then_return_its_value( self): data = {1: 'first', 2: 'second', 3: {4: 'third'}} key = 4 self.assertEqual(deep_find_dfs(data, key), 'third')
def test_deep_find_dfs_when_key_is_not_in_any_level_then_return_false( self): data = {1: 'first', 2: 'second'} key = 6 self.assertFalse(deep_find_dfs(data, key))
def test_deep_find_dfs_when_key_is_in_the_first_level_then_return_its_value( self): data = {1: 'first', 2: 'second'} key = 1 self.assertEqual(deep_find_dfs(data, key), 'first')
def test_deep_find_dfs_when_value_is_tuple(self): data = {1: ({2: 'first'}, {3: 'second', 4: 'third'}), 2: 'fourth'} key = 2 self.assertEqual(deep_find_dfs(data, key), 'first')