def test_merged_dict_has_all_keys(self): """ Test that the merged dictionary key set is the combined set of keys from both dictionaries. """ merged_dict = merge_dicts(self.dict1, self.dict2, self.func) union_keys = self.dict1.viewkeys() | self.dict2.viewkeys() self.assertItemsEqual(merged_dict.viewkeys(), union_keys)
def test_merged_dict_keeps_unique_key_values(self): """ Test that the merged dictionary does not alter values of keys only found in one set. """ merged_dict = merge_dicts(self.dict1, self.dict2, self.func) symmetric_difference_keys = self.dict1.viewkeys() ^ self.dict2.viewkeys() # Ensures that this test is run on at least one key. for key in symmetric_difference_keys: self.assertEqual(merged_dict[key], self.dict1[key] if key in self.dict1 else self.dict2[key])
def test_merged_dict_has_merged_values(self): """ Test that intersecting keys have their values merged. """ merged_dict = merge_dicts(self.dict1, self.dict2, self.func) intersecting_keys = self.dict1.viewkeys() & self.dict2.viewkeys() # Ensures that this test is run on at least one key. self.assertTrue(intersecting_keys) for key in intersecting_keys: self.assertEqual(merged_dict[key], self.func(self.dict1[key], self.dict2[key]))
def do_merge_hashes(self, args): if args["all"]: directory_paths = glob(join(self.data_directory_path, "*")) else: directory_paths = args.get("directory_paths") directory_paths = [self.__data_directory_path(directory_path) for directory_path in directory_paths] def __merge_helper(dict1, dict2): return merge_dicts(dict1, dict2, set.union) output = {} for directory_path in directory_paths: directory_yamls = DirectoryYAML(directory_path) directory_yamls.load() output = merge_dicts(output, directory_yamls.data, __merge_helper) with open(self.merged_hashes_output_file_path, "wb") as output_file: output_file.write(dump(output, default_flow_style=False, indent=4, allow_unicode=True)) print "\t> Output saved to: {}".format(abspath(output_file.name))
def __merge_helper(dict1, dict2): return merge_dicts(dict1, dict2, set.union)