def test_json(): # Set up test objects test_repo = ConvertRepo() test_type = ConvertType(test_repo, "test_type") test_repo.add_type(test_type) prefix_group = ConvertPrefixGroup(test_repo, "test_group") test_repo.add_prefix_group(prefix_group) test_type.base_unit = ConvertUnit(test_type, ["base_unit"], 1) test_unit_names = ["name1", "name2"] test_value = 1337 # Create test unit test_unit = ConvertUnit(test_type, test_unit_names, test_value) test_unit.update_offset(10) test_unit.add_abbr("abbr1") test_unit.valid_prefix_group = prefix_group # Convert to json and back json_data = test_unit.to_json() json_unit = ConvertUnit.from_json( test_type, json_data, ) assert len(test_unit.abbr_list) == 1 assert "abbr1" in json_unit.abbr_list assert json_unit.type == test_type assert len(test_unit.name_list) == 2 assert "name1" in json_unit.name_list assert "name2" in json_unit.name_list assert json_unit.value == test_value assert json_unit.offset == 10 assert json_unit.last_updated_date == test_unit.last_updated_date assert json_unit.valid_prefix_group == prefix_group
def test_get_prefix_group_by_name(): # Set up test objects test_repo = ConvertRepo() test_group1 = ConvertPrefixGroup(test_repo, "group1") test_group2 = ConvertPrefixGroup(test_repo, "group2") test_repo.add_prefix_group(test_group1) test_repo.add_prefix_group(test_group2) # Test assert test_repo.get_prefix_group_by_name("group1") == test_group1 assert test_repo.get_prefix_group_by_name("group2") == test_group2
def test_add_prefix_group(): test_repo = ConvertRepo() test_group1 = ConvertPrefixGroup(test_repo, "group1") test_group2 = ConvertPrefixGroup(test_repo, "group2") # Add group test_repo.add_prefix_group(test_group1) # Check assert len(test_repo.prefix_group_list) == 1 assert test_repo.prefix_group_list[0] == test_group1 # Add group test_repo.add_prefix_group(test_group2) # Check assert len(test_repo.prefix_group_list) == 2 assert test_group2 in test_repo.prefix_group_list
def test_remove_prefix_group(): # Set up test objects test_repo = ConvertRepo() test_group1 = ConvertPrefixGroup(test_repo, "group1") test_group2 = ConvertPrefixGroup(test_repo, "group2") test_repo.add_prefix_group(test_group1) test_repo.add_prefix_group(test_group2) # Remove group test_repo.remove_prefix_group(test_group1) # Check assert len(test_repo.prefix_group_list) == 1 assert test_repo.prefix_group_list[0] == test_group2 # Remove other group test_repo.remove_prefix_group(test_group2) # Check assert test_repo.prefix_group_list == []
def test_json(): test_repo = ConvertRepo() test_type1 = ConvertType(test_repo, "test_type1") test_type2 = ConvertType(test_repo, "test_type2") test_repo.add_type(test_type1) test_repo.add_type(test_type2) test_unit1 = ConvertUnit(test_type1, ["unit1"], 1) test_unit2 = ConvertUnit(test_type2, ["unit2"], 1) test_type1.base_unit = test_unit1 test_type2.base_unit = test_unit2 test_group1 = ConvertPrefixGroup(test_repo, "group1") test_group2 = ConvertPrefixGroup(test_repo, "group2") test_repo.add_prefix_group(test_group1) test_repo.add_prefix_group(test_group2) # Save to JSON and load try: try: os.rename("store/convert.json", "store/convert.json.tmp") except OSError: pass test_repo.save_json() new_repo = ConvertRepo.load_json() assert len(new_repo.type_list) == 2 assert len(new_repo.prefix_group_list) == 2 assert "test_type1" in [x.name for x in new_repo.type_list] assert "test_type2" in [x.name for x in new_repo.type_list] assert "group1" in [x.name for x in new_repo.prefix_group_list] assert "group2" in [x.name for x in new_repo.prefix_group_list] finally: try: os.remove("store/convert.json") except OSError: pass try: os.rename("store/convert.json.tmp", "store/convert.json") except OSError: pass
class ConvertFunctionTestBase(TestBase, unittest.TestCase): def setUp(self): super().setUp() # Create test repo self.test_repo = ConvertRepo() self.test_type1 = ConvertType(self.test_repo, "test_type1") self.test_repo.add_type(self.test_type1) self.test_unit1a = ConvertUnit(self.test_type1, ["unit1a"], 1) self.test_type1.base_unit = self.test_unit1a self.test_unit1b = ConvertUnit(self.test_type1, ["unit1b", "same_name"], 2) self.test_unit1b.abbr_list = ["abbr1b", "abbr1bz"] self.test_type1.add_unit(self.test_unit1b) self.test_unit1c = ConvertUnit(self.test_type1, ["unit1c"], 4) self.test_unit1b.abbr_list = ["abbr1c"] self.test_type1.add_unit(self.test_unit1c) # Add a second type self.test_type2 = ConvertType(self.test_repo, "test_type2") self.test_repo.add_type(self.test_type2) self.test_unit2a = ConvertUnit(self.test_type2, ["unit2a"], 1) self.test_type2.base_unit = self.test_unit2a self.test_unit2b = ConvertUnit(self.test_type2, ["unit2b", "same_name"], 5) self.test_type2.add_unit(self.test_unit2b) # Add a prefix group self.test_group1 = ConvertPrefixGroup(self.test_repo, "test_group1") self.test_repo.add_prefix_group(self.test_group1) self.test_prefix1a = ConvertPrefix(self.test_group1, "prefix1a", "pref1a", 5) self.test_group1.add_prefix(self.test_prefix1a) self.test_prefix1b = ConvertPrefix(self.test_group1, "prefixb", "pref1b", 15) self.test_group1.add_prefix(self.test_prefix1b) # Add a second prefix group self.test_group2 = ConvertPrefixGroup(self.test_repo, "test_group2") self.test_repo.add_prefix_group(self.test_group2) self.test_prefix2a = ConvertPrefix(self.test_group2, "prefix2a", "pref2a", 7) self.test_group2.add_prefix(self.test_prefix2a) self.test_prefix2b = ConvertPrefix(self.test_group2, "prefixb", "pref2b", 42) self.test_group2.add_prefix(self.test_prefix2b) # Move current convert.json try: os.rename("store/convert.json", "store/convert.json.tmp") except OSError: pass # Put this test repo into the Convert object convert_function = self.function_dispatcher.get_function_by_name( "convert") convert_function_obj = self.function_dispatcher.get_function_object( convert_function) # type: Convert convert_function_obj.convert_repo = self.test_repo def tearDown(self): super().tearDown() # Put all the normal convert json back where it should be try: os.remove("store/convert.json") except OSError: pass try: os.rename("store/convert.json.tmp", "store/convert.json") except OSError: pass