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_get_type_by_name(): # Set up test objects 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 assert test_repo.get_type_by_name("test_type1") == test_type1 assert test_repo.get_type_by_name("test_type2") == test_type2
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 test_build_list_from_user_input_no_match(): # Setup test objects test_repo = ConvertRepo() test_type = ConvertType(test_repo, "test_type") test_type.base_unit = ConvertUnit(test_type, ["base_unit"], 1) test_repo.add_type(test_type) test_unit = ConvertUnit(test_type, ["name_a", "name_b"], 1337) test_type.add_unit(test_unit) # Run method try: ConvertMeasure.build_list_from_user_input(test_repo, "32 name_c") assert False, "Should have failed to find a valid unit." except Exception as e: assert "unrecognised unit" in str(e).lower()
def test_build_list_from_user_input_no_amount(): # Setup test objects test_repo = ConvertRepo() test_type = ConvertType(test_repo, "test_type") test_type.base_unit = ConvertUnit(test_type, ["base_unit"], 1) test_repo.add_type(test_type) test_unit = ConvertUnit(test_type, ["name_a", "name_b"], 1337) test_type.add_unit(test_unit) # Run method try: ConvertMeasure.build_list_from_user_input(test_repo, "name_a") assert False, "Should have failed to create convert measures." except Exception as e: assert "cannot find amount" in str(e).lower()
def test_build_list_from_user_input_end(): # Setup test objects test_repo = ConvertRepo() test_type = ConvertType(test_repo, "test_type") test_type.base_unit = ConvertUnit(test_type, ["base_unit"], 1) test_repo.add_type(test_type) test_unit = ConvertUnit(test_type, ["name1", "name2"], 1337) test_type.add_unit(test_unit) # Run method data = ConvertMeasure.build_list_from_user_input(test_repo, "name2 27") # Check results assert len(data) == 1 assert data[0].amount == 27 assert data[0].unit == test_unit
def test_get_prefix_by_abbr(): # Set up stuff test_repo = ConvertRepo() prefix_group = ConvertPrefixGroup(test_repo, "test_group") prefix_name1 = "test_name1" prefix_abbr1 = "test_abbr1" prefix_mult1 = 1001 test_prefix1 = ConvertPrefix(prefix_group, prefix_name1, prefix_abbr1, prefix_mult1) prefix_name2 = "test_name2" prefix_abbr2 = "test_abbr2" prefix_mult2 = 1002 test_prefix2 = ConvertPrefix(prefix_group, prefix_name2, prefix_abbr2, prefix_mult2) prefix_group.add_prefix(test_prefix1) prefix_group.add_prefix(test_prefix2) # Test things assert prefix_group.get_prefix_by_abbr(prefix_abbr1) == test_prefix1 assert prefix_group.get_prefix_by_abbr(prefix_abbr2) == test_prefix2 assert prefix_group.get_prefix_by_abbr( prefix_abbr1.upper()) == test_prefix1 assert prefix_group.get_prefix_by_abbr( prefix_abbr2.upper()) == test_prefix2 assert (prefix_group.get_prefix_by_abbr( prefix_abbr1.capitalize()) == test_prefix1) assert (prefix_group.get_prefix_by_abbr( prefix_abbr2.capitalize()) == test_prefix2)
def test_get_appropriate_prefix(): # Set up stuff test_repo = ConvertRepo() prefix_group = ConvertPrefixGroup(test_repo, "test_group") prefix_name1 = "test_name1" prefix_abbr1 = "test_abbr1" prefix_mult1 = 1000 test_prefix1 = ConvertPrefix(prefix_group, prefix_name1, prefix_abbr1, prefix_mult1) prefix_name2 = "test_name2" prefix_abbr2 = "test_abbr2" prefix_mult2 = 10 test_prefix2 = ConvertPrefix(prefix_group, prefix_name2, prefix_abbr2, prefix_mult2) prefix_name3 = "test_name3" prefix_abbr3 = "test_abbr3" prefix_mult3 = 0.01 test_prefix3 = ConvertPrefix(prefix_group, prefix_name3, prefix_abbr3, prefix_mult3) prefix_group.add_prefix(test_prefix1) prefix_group.add_prefix(test_prefix2) prefix_group.add_prefix(test_prefix3) # Test things assert prefix_group.get_appropriate_prefix(2) is None assert prefix_group.get_appropriate_prefix(24) == test_prefix2 assert prefix_group.get_appropriate_prefix(1234) == test_prefix1 assert prefix_group.get_appropriate_prefix(0.5) == test_prefix3
def test_get_prefix_from_user_input_abbr(): # Set up prefix group test_repo = ConvertRepo() prefix_group = ConvertPrefixGroup(test_repo, "test_group") prefix_name1 = "test_name1" prefix_abbr1 = "test_abbr1" prefix_mult1 = 1001 test_prefix1 = ConvertPrefix(prefix_group, prefix_name1, prefix_abbr1, prefix_mult1) prefix_name2 = "test_name2" prefix_abbr2 = "test_abbr2" prefix_mult2 = 1002 test_prefix2 = ConvertPrefix(prefix_group, prefix_name2, prefix_abbr2, prefix_mult2) prefix_group.add_prefix(test_prefix1) prefix_group.add_prefix(test_prefix2) # Set up test object test_type = ConvertType(test_repo, "test_type") test_type.base_unit = ConvertUnit(test_type, ["base_unit"], 1) test_unit_names = ["name1", "name2", "NaMe3"] test_value = 1337 test_unit = ConvertUnit(test_type, test_unit_names, test_value) test_unit.add_abbr("ABbr1") test_unit.add_abbr("abbr2") test_unit.valid_prefix_group = prefix_group # Get prefix from input assert test_unit.get_prefix_from_user_input( "test_abbr1abbr2") == test_prefix1 assert test_unit.get_prefix_from_user_input( "test_abbr1not_a_unit") is False assert test_unit.get_prefix_from_user_input("not_a_prefixabbr2") is False assert test_unit.get_prefix_from_user_input("abbr2") is None # Remove prefix group and see what happens test_unit.valid_prefix_group = None assert test_unit.get_prefix_from_user_input("test_abbr1abbr2") is False
def test_remove_prefix(): # Set up stuff test_repo = ConvertRepo() prefix_group = ConvertPrefixGroup(test_repo, "test_group") prefix_name1 = "test_name1" prefix_abbr1 = "test_abbr1" prefix_mult1 = 1001 test_prefix1 = ConvertPrefix(prefix_group, prefix_name1, prefix_abbr1, prefix_mult1) prefix_name2 = "test_name2" prefix_abbr2 = "test_abbr2" prefix_mult2 = 1002 test_prefix2 = ConvertPrefix(prefix_group, prefix_name2, prefix_abbr2, prefix_mult2) prefix_group.add_prefix(test_prefix1) prefix_group.add_prefix(test_prefix2) assert test_prefix1 in prefix_group.prefix_list assert test_prefix2 in prefix_group.prefix_list assert len(prefix_group.prefix_list) == 2 # Remove some prefixes and test prefix_group.remove_prefix(test_prefix1) assert len(prefix_group.prefix_list) == 1 assert test_prefix2 in prefix_group.prefix_list prefix_group.remove_prefix(test_prefix2) assert len(prefix_group.prefix_list) == 0
def test_build_list_from_user_input_prefix(): # Setup test objects test_repo = ConvertRepo() test_type = ConvertType(test_repo, "test_type") test_type.base_unit = ConvertUnit(test_type, ["base_unit"], 1) test_repo.add_type(test_type) test_unit = ConvertUnit(test_type, ["name1", "name2"], 1337) test_type.add_unit(test_unit) prefix_group = ConvertPrefixGroup(test_repo, "test_group") test_prefix = ConvertPrefix(prefix_group, "ten", "10", 10) prefix_group.add_prefix(test_prefix) test_unit.valid_prefix_group = prefix_group # Run method data = ConvertMeasure.build_list_from_user_input(test_repo, "tenname2 27") # Check results assert len(data) == 1 assert data[0].amount == 270 assert data[0].unit == test_unit
def test_add_unit(): # Set up test objects test_repo = ConvertRepo() test_type = ConvertType(test_repo, "test_type") test_type.base_unit = ConvertUnit(test_type, ["base_unit"], 1) test_unit = ConvertUnit(test_type, ["name1", "name2"], 1337) # Add unit to type test_type.add_unit(test_unit) # Check assert len(test_type.unit_list) == 1 assert test_type.unit_list[0] == test_unit
def test_init(): # Set up test objects test_repo = ConvertRepo() # Test init test_type = ConvertType(test_repo, "test_type") assert len(test_type.unit_list) == 0 assert test_type.unit_list == [] assert test_type.repo == test_repo assert test_type.name == "test_type" assert test_type.decimals == 2 assert test_type.base_unit is None
def test_init(): # Setup test objects test_repo = ConvertRepo() test_type = ConvertType(test_repo, "test_type") test_type.base_unit = ConvertUnit(test_type, ["base_unit"], 1) test_unit = ConvertUnit(test_type, ["name1", "name2"], 1337) # Init test_measure = ConvertMeasure(12.34, test_unit) # Check assert test_measure.amount == 12.34 assert test_measure.unit == test_unit
def test_build_list_from_user_input_multi_match(): # Setup test objects test_repo = ConvertRepo() test_type1 = ConvertType(test_repo, "test_type1") test_type1.base_unit = ConvertUnit(test_type1, ["base_unit1"], 1) test_type2 = ConvertType(test_repo, "test_type2") test_type2.base_unit = ConvertUnit(test_type2, ["base_unit2"], 1) test_repo.add_type(test_type1) test_repo.add_type(test_type2) test_unit1 = ConvertUnit(test_type1, ["name1", "name2"], 1337) test_unit2 = ConvertUnit(test_type2, ["name2", "name3"], 567) test_type1.add_unit(test_unit1) test_type2.add_unit(test_unit2) # Run method data = ConvertMeasure.build_list_from_user_input(test_repo, "7 name2") # Check results assert len(data) == 2 assert data[0].amount == 7 assert data[1].amount == 7 assert test_unit1 in [data[x].unit for x in [0, 1]] assert test_unit2 in [data[x].unit for x in [0, 1]]
def test_init(): test_repo = ConvertRepo() prefix_group = ConvertPrefixGroup(test_repo, "test_group") prefix_name = "test_name" prefix_abbr = "test_abbr" prefix_mult = 1337 test_prefix = ConvertPrefix(prefix_group, prefix_name, prefix_abbr, prefix_mult) assert test_prefix.prefix_group == prefix_group assert test_prefix.prefix == "test_name" assert test_prefix.abbreviation == "test_abbr" assert test_prefix.multiplier == 1337
def test_convert_to_base(): # Setup test objects test_repo = ConvertRepo() test_type = ConvertType(test_repo, "test_type") test_type.base_unit = ConvertUnit(test_type, ["base_unit"], 1) test_unit = ConvertUnit(test_type, ["name1", "name2"], 1337) measure1 = ConvertMeasure(17.5, test_unit) # Convert to base test_result = measure1.convert_to_base() # Check assert test_result.unit.name_list[0] == "base_unit" assert test_result.amount == 17.5 * 1337
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_is_equal(): # Setup test objects test_repo = ConvertRepo() test_type = ConvertType(test_repo, "test_type") test_type.base_unit = ConvertUnit(test_type, ["base_unit"], 1) test_unit = ConvertUnit(test_type, ["name1", "name2"], 1337) measure1 = ConvertMeasure(17.5, test_unit) measure2 = ConvertMeasure(17.5, test_unit) # Check not the same object assert not measure1 == measure2 # Check is equal assert measure1.is_equal(measure2) assert measure2.is_equal(measure1)
def test_to_string_no_prefix(): # Setup test objects test_repo = ConvertRepo() test_type = ConvertType(test_repo, "test_type") test_type.base_unit = ConvertUnit(test_type, ["base_unit"], 1) test_type.decimals = 3 test_unit = ConvertUnit(test_type, ["name1", "name2"], 1337) measure1 = ConvertMeasure(17.5, test_unit) # Get string measure_str = measure1.to_string() # Check assert str(measure1) == measure_str assert measure_str == "17.500 name1"
def test_json(): # Set up stuff test_repo = ConvertRepo() prefix_group = ConvertPrefixGroup(test_repo, "test_group") # Collapse to XML and rebuild json_data = prefix_group.to_json() rebuild_group = ConvertPrefixGroup.from_json( test_repo, json_data, ) assert rebuild_group.repo == test_repo assert len(rebuild_group.prefix_list) == 0 assert rebuild_group.name == "test_group"
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_get_full_unit_list(): # Set up test objects test_repo = ConvertRepo() test_type = ConvertType(test_repo, "test_type") test_unitb = ConvertUnit(test_type, ["base_unit"], 1) test_type.base_unit = test_unitb test_unit1 = ConvertUnit(test_type, ["name1", "name2"], 1337) test_unit2 = ConvertUnit(test_type, ["name3", "name4"], 505) test_type.add_unit(test_unit1) test_type.add_unit(test_unit2) # Check full unit list assert len(test_type.get_full_unit_list()) == 3 assert test_unitb in test_type.get_full_unit_list() assert test_unit1 in test_type.get_full_unit_list() assert test_unit2 in test_type.get_full_unit_list()
def test_convert_to_offset(): # Setup test objects test_repo = ConvertRepo() test_type = ConvertType(test_repo, "test_type") test_type.base_unit = ConvertUnit(test_type, ["base_unit"], 1) test_unit1 = ConvertUnit(test_type, ["name1", "name2"], 1337) test_unit1.update_offset(54) test_unit2 = ConvertUnit(test_type, ["name3"], 505) test_unit2.update_offset(10) measure1 = ConvertMeasure(17.5, test_unit1) # Convert to base test_result = measure1.convert_to(test_unit2) # Check assert test_result.unit.name_list[0] == "name3" assert test_result.amount == ((17.5 * 1337) + 54 - 10) / 505
def test_get_full_unit_list(): # Set up test objects test_repo = ConvertRepo() test_type1 = ConvertType(test_repo, "test_type1") test_repo.add_type(test_type1) test_unit1 = ConvertUnit(test_type1, ["unit1"], 1) test_unit2 = ConvertUnit(test_type1, ["unit2"], 10) test_unit3 = ConvertUnit(test_type1, ["unit3"], 100) test_type1.base_unit = test_unit1 test_type1.add_unit(test_unit2) test_type1.add_unit(test_unit3) test_type2 = ConvertType(test_repo, "test_type2") test_unit4 = ConvertUnit(test_type2, ["unit4"], 1) test_unit5 = ConvertUnit(test_type2, ["unit5"], 1000) test_type2.base_unit = test_unit4 test_type2.add_unit(test_unit5) test_repo.add_type(test_type2) # Test assert test_unit1 in test_repo.get_full_unit_list() assert test_unit2 in test_repo.get_full_unit_list() assert test_unit3 in test_repo.get_full_unit_list() assert test_unit4 in test_repo.get_full_unit_list() assert test_unit5 in test_repo.get_full_unit_list() assert len(test_repo.get_full_unit_list()) == 5
def test_remove_unit(): # Set up test objects test_repo = ConvertRepo() test_type = ConvertType(test_repo, "test_type") test_type.base_unit = ConvertUnit(test_type, ["base_unit"], 1) test_unit1 = ConvertUnit(test_type, ["name1", "name2"], 1337) test_unit2 = ConvertUnit(test_type, ["name3", "name4"], 505) test_type.add_unit(test_unit1) test_type.add_unit(test_unit2) # Check it's all set up correctly assert len(test_type.unit_list) == 2 # Remove unit from type test_type.remove_unit(test_unit1) # Check assert len(test_type.unit_list) == 1 assert test_type.unit_list[0] == test_unit2
def test_get_unit_by_name(): # Set up test objects test_repo = ConvertRepo() test_type = ConvertType(test_repo, "test_type") test_unitb = ConvertUnit(test_type, ["base_unit"], 1) test_type.base_unit = test_unitb test_unit1 = ConvertUnit(test_type, ["name1", "name2"], 1337) test_unit2 = ConvertUnit(test_type, ["name3", "name4"], 505) test_unit2.add_abbr("u2") test_type.add_unit(test_unit1) test_type.add_unit(test_unit2) # test some stuff assert test_type.get_unit_by_name("base_unit") == test_unitb assert test_type.get_unit_by_name("NAME1") == test_unit1 assert test_type.get_unit_by_name("NaMe4") == test_unit2 assert test_type.get_unit_by_name("u2") == test_unit2
def test_convert_to_different_types(): # Setup test objects test_repo = ConvertRepo() test_type1 = ConvertType(test_repo, "test_type1") test_type1.base_unit = ConvertUnit(test_type1, ["base_unit"], 1) test_unit1 = ConvertUnit(test_type1, ["name1", "name2"], 1337) test_type2 = ConvertType(test_repo, "test_type2") test_type2.base_unit = ConvertUnit(test_type2, ["another_base"], 1) test_unit2 = ConvertUnit(test_type2, ["name3"], 505) measure1 = ConvertMeasure(17.5, test_unit1) # Convert to base try: measure1.convert_to(test_unit2) assert False except Exception as e: assert "not the same unit type" in str(e)
def test_json(): test_repo = ConvertRepo() prefix_group = ConvertPrefixGroup(test_repo, "test_group") prefix_name = "test_name" prefix_abbr = "test_abbr" prefix_mult = 1337 test_prefix = ConvertPrefix(prefix_group, prefix_name, prefix_abbr, prefix_mult) json = test_prefix.to_json() rebuild_prefix = ConvertPrefix.from_json( prefix_group, json, ) assert rebuild_prefix.prefix == test_prefix.prefix assert rebuild_prefix.abbreviation == test_prefix.abbreviation assert rebuild_prefix.multiplier == test_prefix.multiplier
def test_set_offset(): # Set up test object test_repo = ConvertRepo() test_type = ConvertType(test_repo, "test_type") test_type.base_unit = ConvertUnit(test_type, ["base_unit"], 1) test_unit_names = ["name1", "name2"] test_value = 1337 test_unit = ConvertUnit(test_type, test_unit_names, test_value) # Check value and time updated assert test_unit.offset == 0 assert test_unit.last_updated_date is None # Change value test_unit.update_offset(10) # Check value assert test_unit.offset == 10 assert test_unit.last_updated_date is not None