def test_get_prefix_from_user_input_abbr_internal(self): # 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("cubic {X}abbr2") test_unit.valid_prefix_group = prefix_group # Get prefix from input assert test_unit.get_prefix_from_user_input("cubic test_abbr1abbr2") == test_prefix1 assert test_unit.get_prefix_from_user_input("cubic test_abbr1not_a_unit") is False assert test_unit.get_prefix_from_user_input("cubic not_a_prefixabbr2") is False assert test_unit.get_prefix_from_user_input("cubic abbr2") is None # Remove prefix group and see what happens test_unit.valid_prefix_group = None assert test_unit.get_prefix_from_user_input("cubic test_abbr1abbr2") is False
def test_xml(self): # 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.set_offset(10) test_unit.add_abbr("abbr1") test_unit.valid_prefix_group = prefix_group # Convert to XML and back test_xml = test_unit.to_xml() xml_unit = ConvertUnit.from_xml(test_type, test_xml) assert len(test_unit.abbr_list) == 1 assert "abbr1" in xml_unit.abbr_list assert xml_unit.type == test_type assert len(test_unit.name_list) == 2 assert "name1" in xml_unit.name_list assert "name2" in xml_unit.name_list assert xml_unit.value == test_value assert xml_unit.offset == 10 assert xml_unit.last_updated == test_unit.last_updated assert xml_unit.valid_prefix_group == prefix_group
def test_get_unit_by_name(self): # 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_has_name(self): # 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", "NaMe3"] test_value = 1337 test_unit = ConvertUnit(test_type, test_unit_names, test_value) test_unit.add_abbr("ABbr1") test_unit.add_abbr("abbr2") # Do some tests assert test_unit.has_name("name1") assert test_unit.has_name("name3") assert test_unit.has_name("NamE2") assert not test_unit.has_name("name4") assert test_unit.has_name("abbr1") assert test_unit.has_name("ABBR2") assert not test_unit.has_name("abbr3")
def test_remove_abbr(self): # 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_names = ["name1", "name2"] test_value = 1337 test_unit = ConvertUnit(test_type, test_unit_names, test_value) test_unit.add_abbr("abbr1") test_unit.add_abbr("abbr2") # Test setup assert "abbr1" in test_unit.abbr_list assert "abbr2" in test_unit.abbr_list assert len(test_unit.abbr_list) == 2 # Remove abbreviation test_unit.remove_abbr("abbr1") # Test changes assert len(test_unit.abbr_list) == 1 assert "abbr2" in test_unit.abbr_list assert "abbr1" not in test_unit.abbr_list
def test_json(self): # Set up test objects test_repo = ConvertRepo() test_type = ConvertType(test_repo, "test_type") test_type.decimals = 4 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) # Collapse to XML and rebuild test_json = test_type.to_json() rebuild_type = ConvertType.from_json(test_repo, test_json) # Test the type assert rebuild_type.repo == test_repo assert len(rebuild_type.unit_list) == 2 assert rebuild_type.name == "test_type" assert rebuild_type.decimals == 4 assert rebuild_type.base_unit.name_list[0] == "base_unit"