Skip to content

Commit

Permalink
automated tests
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexLuo602 committed Nov 16, 2024
1 parent 6ffc187 commit dcdf23d
Show file tree
Hide file tree
Showing 24 changed files with 221 additions and 46 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"italian": {
"type": "created",
"created": {
"hello": "bonjourno",
"thanks": "grazie",
"welcome": "benvenuto"
},
"modified": {},
"deleted": {}
},
"spanish": {
"type": "created",
"created": {
"hello": "hola",
"thanks": "gracias"
},
"modified": {},
"deleted": {}
},
"french": {
"type": "created",
"created": {
"hello": "bonjour",
"thanks": "merci"
},
"modified": {},
"deleted": {}
},
"portugese": {
"type": "created",
"created": {
"hello": "ola",
"thanks": "obrigado",
"welcome": "Bem-vindo"
},
"modified": {},
"deleted": {}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"hello": "bonjour",
"thanks": "merci"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"hello": "bonjourno",
"thanks": "grazie",
"welcome": "benvenuto"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"hello": "ola",
"thanks": "obrigado",
"welcome": "Bem-vindo"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"hello": "hola",
"thanks": "gracias"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"italian": {
"type": "deleted",
"created": {},
"modified": {},
"deleted": {}
},
"spanish": {
"type": "deleted",
"created": {},
"modified": {},
"deleted": {}
},
"french": {
"type": "deleted",
"created": {},
"modified": {},
"deleted": {}
},
"portugese": {
"type": "deleted",
"created": {},
"modified": {},
"deleted": {}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"hello": "bonjour",
"thanks": "merci"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"hello": "bonjourno",
"thanks": "grazie",
"welcome": "benvenuto"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"hello": "ola",
"thanks": "obrigado",
"welcome": "Bem-vindo"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"hello": "hola",
"thanks": "gracias"
}
Empty file.
Empty file.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"hello": "bonjour",
"thanks": "merci"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"hello": "bonjourno",
"thanks": "grazie",
"welcome": "benvenuto"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"hello": "ola",
"thanks": "obrigado",
"welcome": "Bem-vindo"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"hello": "hola",
"thanks": "gracias"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"hello": "bonjour",
"thanks": "merci"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"hello": "bonjourno",
"thanks": "grazie",
"welcome": "benvenuto"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"hello": "ola",
"thanks": "obrigado",
"welcome": "Bem-vindo"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"hello": "hola",
"thanks": "gracias"
}
116 changes: 80 additions & 36 deletions i18nilize/tests/test_diffing.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logging
import unittest
import os
import filecmp
Expand All @@ -7,36 +8,49 @@
from src.internationalize.diffing_processor import compute_hashes, read_json_file, DiffingProcessor

class TestDiffing(unittest.TestCase):
def setUp(self):
@classmethod
def setUpClass(cls):
logging.basicConfig(level=logging.INFO)

# 'mock' translations folder to mimic real user interaction
self.test_translations_dir = "tests/resources/diffing_algorithm/test_translations/"
cls.test_translations_dir = "tests/resources/diffing_algorithm/test_translations"

# main directory for diffing tests
self.test_data_location = "tests/resources/diffing_algorithm/all_tests/"
cls.test_data_location = "tests/resources/diffing_algorithm/all_tests"

# standardized names for a test folder
self.initial_translations_dir = "/initial_translations/"
self.modified_translation_dir = "/modified_translations/"
self.expected_output_file = "/expected_output.json"
cls.initial_translations_dir = "initial_translations"
cls.modified_translation_dir = "modified_translations"
cls.expected_output_file = "expected_output.json"

# default test data to basic tests
self.basic_initial_data_location = self.test_data_location + "basic_test" + self.initial_translations_dir
self.basic_modified_data_location = self.test_data_location + "basic_test" + self.modified_translation_dir
self.basic_expected_output = self.test_data_location + "basic_test" + self.expected_output_file
cls.basic_initial_data_location = cls.get_test_path("basic_test", cls.initial_translations_dir)
cls.basic_modified_data_location = cls.get_test_path("basic_test", cls.modified_translation_dir)
cls.basic_expected_output = cls.get_test_path("basic_test", cls.expected_output_file)

# initialize classes
cls.util = DiffingTestUtil(cls.test_translations_dir)
cls.dp = DiffingProcessor(cls.test_translations_dir)

return super().setUpClass()

def setUp(self):
# initialize util class
self.util = DiffingTestUtil(self.test_translations_dir)
self.util.initialize_test_data(self.basic_initial_data_location)

# initialize diffing processor
self.dp = DiffingProcessor(self.test_translations_dir)
self.dp.setup()

# tear down diffing processor instance
# tear down diffing folder
def tearDown(self):
if os.path.exists(self.dp.diff_state_root_dir):
shutil.rmtree(self.dp.diff_state_root_dir)
self.util.clear_test_data()

# remove redundant folder after testing
@classmethod
def tearDownClass(cls):
cls.util.clear_test_data()
return super().tearDownClass()

def test_initialization(self):
self.assertTrue(os.path.exists(self.dp.diff_state_root_dir))
Expand All @@ -57,29 +71,6 @@ def test_initialization(self):
self.assertTrue(len(mismatch) == 0)
self.assertTrue(len(errors) == 0)


def test_find_changed_files_basic(self):
self.util.initialize_test_data(self.basic_modified_data_location)
expected_changed_files = {
"modified": ["italian.json", "spanish.json"],
"created": ["mandarin.json"],
"deleted": ["french.json"]
}
changed_files = self.dp.get_changed_files()

for type, languages in changed_files.items():
self.assertListEqual(languages, expected_changed_files[type])


def test_find_changed_translations_basic(self):
self.util.initialize_test_data(self.basic_modified_data_location)
expected_changed_translations = read_json_file(self.basic_expected_output)

changed_translations = self.dp.get_changed_translations()
self.assertEqual(changed_translations, expected_changed_translations)



def test_updating_state(self):
hashes = compute_hashes(self.test_translations_dir)
changed_files = ["italian.json", "spanish.json"]
Expand All @@ -106,6 +97,59 @@ def test_updating_state(self):
# print(match)
# self.assertTrue(len(match) == 2)

def test_find_changed_files_basic(self):
self.util.initialize_test_data(self.basic_modified_data_location)
expected_changed_files = {
"modified": ["italian.json", "spanish.json"],
"created": ["mandarin.json"],
"deleted": ["french.json"]
}
changed_files = self.dp.get_changed_files()

for type, languages in changed_files.items():
self.assertListEqual(languages, expected_changed_files[type], f"Mismatch in {type} files")

def test_bulk_find_changed_translations(self):
for test_folder in os.listdir(self.test_data_location):
with self.subTest(test_folder=test_folder):
self.run_single_changed_translation_test(test_folder)

def run_single_changed_translation_test(self, test_name):
logging.info("\n\n" + "-" * 40)
logging.info(f"Running test: {test_name}")

# clear diff directory
if os.path.exists(self.dp.diff_state_root_dir):
shutil.rmtree(self.dp.diff_state_root_dir)

# Set test directories
initial_data_location = self.get_test_path(test_name, self.initial_translations_dir)
modified_data_location = self.get_test_path(test_name, self.modified_translation_dir)
expected_output = self.get_test_path(test_name, self.expected_output_file)

# Initialize translations
self.util.initialize_test_data(initial_data_location)
self.dp.setup()

# Modify translations
self.util.initialize_test_data(modified_data_location)
expected_changed_translations = read_json_file(expected_output)

changed_translations = self.dp.get_changed_translations()
try:
self.assertEqual(changed_translations, expected_changed_translations)
except Exception as e:
logging.info(f"test {test_name} failed!: {e}")
logging.info("\n" + "-" * 40 + "\n")
raise

logging.info(f"Test passed: {test_name}")
logging.info("\n" + "-" * 40 + "\n")

@classmethod
def get_test_path(cls, test_name, folder_type):
return os.path.join(cls.test_data_location, test_name, folder_type)


if __name__ == '__main__':
unittest.main()
12 changes: 2 additions & 10 deletions i18nilize/tests/util/test_diffing_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,10 @@ def initialize_test_data(self, directory):
def bulk_modify_test_data(self, directory):
file_names = os.listdir(directory)
for file_name in file_names:
language_data = read_json_file(directory + file_name)
with open(self.test_directory + file_name, 'w') as json_file:
language_data = read_json_file(os.path.join(directory, file_name))
with open(os.path.join(self.test_directory, file_name), 'w') as json_file:
json.dump(language_data, json_file, indent=4)

"""
Modifies a translation based on the given language, word, and translation
Might already be implemented in PIP package
"""
def modify_test_data(self, language, word, translation):
print("NEEDS TO BE IMPLEMENTED")
pass

def clear_test_data(self):
if os.path.exists(self.test_directory):
shutil.rmtree(self.test_directory)

0 comments on commit dcdf23d

Please sign in to comment.