Skip to content

Commit

Permalink
added updating metadata tests
Browse files Browse the repository at this point in the history
  • Loading branch information
andrew-fenton committed Nov 19, 2024
1 parent 8d0d90e commit c6855ff
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 38 deletions.
18 changes: 6 additions & 12 deletions i18nilize/src/internationalize/diffing_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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)

Expand Down Expand Up @@ -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

"""
Expand Down Expand Up @@ -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}")
print(f"An exception occured: {e}")
38 changes: 13 additions & 25 deletions i18nilize/tests/test_diffing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion i18nilize/tests/util/test_diffing_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
shutil.rmtree(self.test_directory)

0 comments on commit c6855ff

Please sign in to comment.