From c6855ffccb69b1242e2d71fe03c1d6a688994481 Mon Sep 17 00:00:00 2001 From: Andrew Fenton Date: Mon, 18 Nov 2024 22:41:19 -0500 Subject: [PATCH] added updating metadata tests --- .../src/internationalize/diffing_processor.py | 18 +++------ i18nilize/tests/test_diffing.py | 38 +++++++------------ i18nilize/tests/util/test_diffing_util.py | 2 +- 3 files changed, 20 insertions(+), 38 deletions(-) diff --git a/i18nilize/src/internationalize/diffing_processor.py b/i18nilize/src/internationalize/diffing_processor.py index 09f3697..ebc788d 100644 --- a/i18nilize/src/internationalize/diffing_processor.py +++ b/i18nilize/src/internationalize/diffing_processor.py @@ -36,7 +36,7 @@ def setup(self): # Compute all file hashes and store hashes in metadata all_files = os.listdir(self.diff_state_files_dir) all_file_hashes = compute_hashes(self.diff_state_files_dir) - self.update_metadata(all_files, all_file_hashes) + self.update_metadata(all_file_hashes) except FileExistsError: print(f"Old translations directory has already been created.") except PermissionError: @@ -47,11 +47,11 @@ def setup(self): """ Updates translation files with new changes and updates hashes in metadata. """ - def update_to_current_state(self, changed_files_list, hash_dict): - self.update_metadata(changed_files_list, hash_dict) + def update_to_current_state(self, hash_dict): + self.update_metadata(hash_dict) self.sync_translations() - def update_metadata(self, changed_files_list, hash_dict): + def update_metadata(self, hash_dict): with open(self.metadata_file_dir, "w") as outfile: json.dump(hash_dict, outfile) @@ -108,13 +108,7 @@ def get_changed_translations(self): if type == CREATED: changed_translations[language] = self.add_language(file_name, changed_translations[language]) - - """ - commented out updating metadata in this section for now - """ - # # Update metadata and old state - # self.update_to_current_state(changed_files_list, new_hashes_dict) - + return changed_translations """ @@ -201,4 +195,4 @@ def read_json_file(directory): print(f"An error occurred while trying to read the file: {directory}") raise except Exception as e: - print(f"An exception occured: {e}") \ No newline at end of file + print(f"An exception occured: {e}") diff --git a/i18nilize/tests/test_diffing.py b/i18nilize/tests/test_diffing.py index b47737a..9cf4149 100644 --- a/i18nilize/tests/test_diffing.py +++ b/i18nilize/tests/test_diffing.py @@ -10,7 +10,7 @@ class TestDiffing(unittest.TestCase): @classmethod def setUpClass(cls): - logging.basicConfig(level=logging.CRITICAL) + logging.basicConfig(level=logging.INFO) logging.getLogger('dirsync').disabled = True # 'mock' translations folder to mimic real user interaction @@ -73,30 +73,18 @@ def test_initialization(self): self.assertTrue(len(errors) == 0) def test_updating_state(self): - hashes = compute_hashes(self.test_translations_dir) - changed_files = ["italian.json", "spanish.json"] - self.dp.update_to_current_state(changed_files, hashes) - - updated_metadata = {} - with open(self.dp.metadata_file_dir) as file: - updated_metadata = json.load(file) - - self.assertIsNotNone(updated_metadata["spanish"]) - self.assertIsNotNone(updated_metadata["italian"]) - self.assertEqual(hashes["spanish"], updated_metadata["spanish"]) - self.assertEqual(hashes["italian"], updated_metadata["italian"]) - - ### Need to finish this part of test - # must_exist_files = os.listdir(self.modified_translations_dir) - # print(must_exist_files) - # match, mismatch, errors = filecmp.cmpfiles( - # self.modified_translations_dir, - # self.dp.diff_state_files_dir, - # must_exist_files, - # shallow=False - # ) - # print(match) - # self.assertTrue(len(match) == 2) + new_hashes = compute_hashes(self.basic_modified_data_location) + self.util.initialize_test_data(self.basic_modified_data_location) + self.dp.update_to_current_state(new_hashes) + modified_metadata = read_json_file(self.dp.metadata_file_dir) + self.assertEqual(new_hashes, modified_metadata) + + def test_no_updates_to_state(self): + hashes = compute_hashes(self.basic_initial_data_location) + self.util.initialize_test_data(self.basic_initial_data_location) + self.dp.update_to_current_state(hashes) + same_metadata = read_json_file(self.dp.metadata_file_dir) + self.assertEqual(hashes, same_metadata) def test_find_changed_files_basic(self): self.util.initialize_test_data(self.basic_modified_data_location) diff --git a/i18nilize/tests/util/test_diffing_util.py b/i18nilize/tests/util/test_diffing_util.py index 2e2003a..e61182b 100644 --- a/i18nilize/tests/util/test_diffing_util.py +++ b/i18nilize/tests/util/test_diffing_util.py @@ -33,4 +33,4 @@ def bulk_modify_test_data(self, directory): def clear_test_data(self): if os.path.exists(self.test_directory): - shutil.rmtree(self.test_directory) \ No newline at end of file + shutil.rmtree(self.test_directory)