From 20c799ddccc8a5f8fea3910dafe089aaaa377261 Mon Sep 17 00:00:00 2001 From: Greg DiCristofaro Date: Wed, 22 Jul 2020 15:32:51 -0400 Subject: [PATCH] testing --- .../localization_scripts/__init__.py | 0 .../localization_scripts/test/__init__.py | 0 .../test/artifacts/.gitignore | 1 + .../localization_scripts/test/test_csvutil.py | 41 ++++++++ .../test/test_fileutil.py | 52 ++++++++++ .../test/test_itemchange.py | 96 +++++++++++++++++++ .../test/test_propsutil.py | 36 +++++++ .../localization_scripts/test/unittestutil.py | 14 +++ 8 files changed, 240 insertions(+) create mode 100644 release_scripts/localization_scripts/__init__.py create mode 100644 release_scripts/localization_scripts/test/__init__.py create mode 100644 release_scripts/localization_scripts/test/artifacts/.gitignore create mode 100644 release_scripts/localization_scripts/test/test_csvutil.py create mode 100644 release_scripts/localization_scripts/test/test_fileutil.py create mode 100644 release_scripts/localization_scripts/test/test_itemchange.py create mode 100644 release_scripts/localization_scripts/test/test_propsutil.py create mode 100644 release_scripts/localization_scripts/test/unittestutil.py diff --git a/release_scripts/localization_scripts/__init__.py b/release_scripts/localization_scripts/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/release_scripts/localization_scripts/test/__init__.py b/release_scripts/localization_scripts/test/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/release_scripts/localization_scripts/test/artifacts/.gitignore b/release_scripts/localization_scripts/test/artifacts/.gitignore new file mode 100644 index 0000000000..6caf68aff4 --- /dev/null +++ b/release_scripts/localization_scripts/test/artifacts/.gitignore @@ -0,0 +1 @@ +output \ No newline at end of file diff --git a/release_scripts/localization_scripts/test/test_csvutil.py b/release_scripts/localization_scripts/test/test_csvutil.py new file mode 100644 index 0000000000..a5ffd0cb71 --- /dev/null +++ b/release_scripts/localization_scripts/test/test_csvutil.py @@ -0,0 +1,41 @@ +import codecs +import os +import unittest +from typing import TypeVar, List + +from csvutil import records_to_csv, csv_to_records +from test.unittestutil import get_output_path + + +class CsvUtilTest(unittest.TestCase): + T = TypeVar('T') + + def assert_equal_arr(self, a: List[T], b: List[T]): + self.assertEqual(len(a), len(b), 'arrays are not equal length') + for i in range(0, len(a)): + if isinstance(a[i], list) and isinstance(b[i], list): + self.assert_equal_arr(a[i], b[i]) + else: + self.assertEqual(a[i], b[i], "Items: {0} and {1} at index {2} are not equal.".format(a[i], b[i], i)) + + def test_read_write(self): + data = [['header1', 'header2', 'header3', 'additional header'], + ['data1', 'data2', 'data3'], + ['', 'data2-1', 'data2-2']] + + os.makedirs(get_output_path(), exist_ok=True) + test_path = get_output_path('test.csv') + records_to_csv(test_path, data) + + byte_inf = min(32, os.path.getsize(test_path)) + with open(test_path, 'rb') as bom_test_file: + raw = bom_test_file.read(byte_inf) + if not raw.startswith(codecs.BOM_UTF8): + self.fail("written csv does not have appropriate BOM") + + read_records_no_header, no_header = csv_to_records(test_path, header_row=False) + self.assert_equal_arr(read_records_no_header, data) + + read_rows, header = csv_to_records(test_path, header_row=True) + self.assert_equal_arr(header, data[0]) + self.assert_equal_arr(read_rows, [data[1], data[2]]) diff --git a/release_scripts/localization_scripts/test/test_fileutil.py b/release_scripts/localization_scripts/test/test_fileutil.py new file mode 100644 index 0000000000..290396eba7 --- /dev/null +++ b/release_scripts/localization_scripts/test/test_fileutil.py @@ -0,0 +1,52 @@ +import os +import unittest +from typing import Tuple +from pathlib import Path +from fileutil import get_path_pieces, get_new_path, get_filename_addition + + +def joined_paths(pieces: Tuple[str, str, str]) -> str: + return os.path.join(pieces[0], pieces[1] + '.' + pieces[2]) + + +PATH_PIECES1 = ('/test/folder', 'filename', 'ext') +PATH_PIECES2 = ('/test.test2/folder.test2', 'filename.test', 'ext') +PATH_PIECES3 = ('/test.test2/folder.test2/folder', None, None) + +PATH1 = joined_paths(PATH_PIECES1) +PATH2 = joined_paths(PATH_PIECES2) +PATH3 = PATH_PIECES3[0] + +ALL_ITEMS = [ + (PATH_PIECES1, PATH1), + (PATH_PIECES2, PATH2), + (PATH_PIECES3, PATH3) +] + + +class FileUtilTest(unittest.TestCase): + def test_get_path_pieces(self): + for (expected_path, expected_filename, expected_ext), path in ALL_ITEMS: + path, filename, ext = get_path_pieces(path) + self.assertEqual(path, str(Path(expected_path))) + self.assertEqual(filename, expected_filename) + self.assertEqual(ext, expected_ext) + + def test_get_new_path(self): + for (expected_path, expected_filename, expected_ext), path in ALL_ITEMS: + new_name = "newname.file" + new_path = get_new_path(path, new_name) + self.assertEqual(new_path, str(Path(expected_path) / Path(new_name))) + + def test_get_filename_addition(self): + for (expected_path, expected_filename, expected_ext), path in ALL_ITEMS: + addition = "addition" + new_path = get_filename_addition(path, addition) + if expected_filename is None or expected_ext is None: + expected_file_path = Path(expected_path + addition) + else: + expected_file_path = Path(expected_path) / Path("{file_name}{addition}.{extension}".format( + file_name=expected_filename, addition=addition, extension=expected_ext)) + + self.assertEqual( + new_path, str(expected_file_path)) diff --git a/release_scripts/localization_scripts/test/test_itemchange.py b/release_scripts/localization_scripts/test/test_itemchange.py new file mode 100644 index 0000000000..91b7846a11 --- /dev/null +++ b/release_scripts/localization_scripts/test/test_itemchange.py @@ -0,0 +1,96 @@ +import unittest +from typing import Dict + +from itemchange import get_changed, ChangeType + + +def dict_to_prop_str(this_dict: Dict[str, str]) -> str: + toret = '' + for key, val in this_dict.items(): + toret += "{key}={value}\n".format(key=key, value=val) + + return toret + + +class ItemChangeTest(unittest.TestCase): + def test_get_changed(self): + deleted_key = 'deleted.property.key' + deleted_val = 'will be deleted' + + change_key = 'change.property.key' + change_val_a = 'original value' + change_val_b = 'new value' + + change_key2 = 'change2.property.key' + change_val2_a = 'original value 2' + change_val2_b = '' + + change_key3 = 'change3.property.key' + change_val3_a = '' + change_val3_b = 'cur value 3' + + addition_key = 'addition.property.key' + addition_new_val = 'the added value' + + same_key = 'samevalue.property.key' + same_value = 'the same value' + + same_key2 = 'samevalue2.property.key' + same_value2 = '' + + a_dict = { + deleted_key: deleted_val, + change_key: change_val_a, + change_key2: change_val2_a, + change_key3: change_val3_a, + same_key: same_value, + same_key2: same_value2 + } + + b_dict = { + change_key: change_val_b, + change_key2: change_val2_b, + change_key3: change_val3_b, + addition_key: addition_new_val, + same_key: same_value, + same_key2: same_value2 + } + + a_str = dict_to_prop_str(a_dict) + b_str = dict_to_prop_str(b_dict) + + rel_path = 'my/rel/path.properties' + + key_to_change = {} + + for item_change in get_changed(rel_path, a_str, b_str): + self.assertEqual(item_change.rel_path, rel_path) + key_to_change[item_change.key] = item_change + + deleted_item = key_to_change[deleted_key] + self.assertEqual(deleted_item.type, ChangeType.DELETION) + self.assertEqual(deleted_item.prev_val, deleted_val) + self.assertEqual(deleted_item.cur_val, None) + + addition_item = key_to_change[addition_key] + self.assertEqual(addition_item.type, ChangeType.ADDITION) + self.assertEqual(addition_item.prev_val, None) + self.assertEqual(addition_item.cur_val, addition_new_val) + + change_item = key_to_change[change_key] + self.assertEqual(change_item.type, ChangeType.CHANGE) + self.assertEqual(change_item.prev_val, change_val_a) + self.assertEqual(change_item.cur_val, change_val_b) + + change_item2 = key_to_change[change_key2] + self.assertEqual(change_item2.type, ChangeType.CHANGE) + self.assertEqual(change_item2.prev_val, change_val2_a) + self.assertEqual(change_item2.cur_val, change_val2_b) + + change_item3 = key_to_change[change_key3] + self.assertEqual(change_item3.type, ChangeType.CHANGE) + self.assertEqual(change_item3.prev_val, change_val3_a) + self.assertEqual(change_item3.cur_val, change_val3_b) + + self.assertTrue(same_key not in key_to_change) + self.assertTrue(same_key2 not in key_to_change) diff --git a/release_scripts/localization_scripts/test/test_propsutil.py b/release_scripts/localization_scripts/test/test_propsutil.py new file mode 100644 index 0000000000..f69129399a --- /dev/null +++ b/release_scripts/localization_scripts/test/test_propsutil.py @@ -0,0 +1,36 @@ +import os +import unittest + +from propsutil import set_entry_dict, get_entry_dict_from_path, update_entry_dict +from test.unittestutil import get_output_path + + +class PropsUtilTest(unittest.TestCase): + def test_update_entry_dict(self): + orig_key = 'orig_key' + orig_val = 'orig_val 片仮名 ' + to_be_altered_key = 'tobealteredkey' + first_val = 'not yet altered sábado' + second_val = 'altered Stöcke' + + orig_props = { + orig_key: orig_val, + to_be_altered_key: first_val + } + + update_props = { + to_be_altered_key: second_val + } + + os.makedirs(get_output_path(), exist_ok=True) + test_path = get_output_path('test.props') + set_entry_dict(orig_props, test_path) + + orig_read_props = get_entry_dict_from_path(test_path) + self.assertEqual(orig_read_props[orig_key], orig_val) + self.assertEqual(orig_read_props[to_be_altered_key], first_val) + + update_entry_dict(update_props, test_path) + updated_read_props = get_entry_dict_from_path(test_path) + self.assertEqual(updated_read_props[orig_key], orig_val) + self.assertEqual(updated_read_props[to_be_altered_key], second_val) diff --git a/release_scripts/localization_scripts/test/unittestutil.py b/release_scripts/localization_scripts/test/unittestutil.py new file mode 100644 index 0000000000..19face5610 --- /dev/null +++ b/release_scripts/localization_scripts/test/unittestutil.py @@ -0,0 +1,14 @@ +import os +from typing import Union + +from envutil import get_proj_dir + +TEST_ARTIFACT_FOLDER = 'artifacts' +TEST_OUTPUT_FOLDER = 'output' + + +def get_output_path(filename: Union[str, None] = None) -> str: + if filename is None: + return os.path.join(get_proj_dir(__file__), TEST_ARTIFACT_FOLDER, TEST_OUTPUT_FOLDER) + else: + return os.path.join(get_proj_dir(__file__), TEST_ARTIFACT_FOLDER, TEST_OUTPUT_FOLDER, filename)