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_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_to_string(): # 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) 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 measure1 = ConvertMeasure(17.5, test_unit) # Get string measure_str = measure1.to_string() # Check assert str(measure1) == measure_str assert measure_str == "1.750 tenname1"
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_to_string_with_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) prefix_group = ConvertPrefixGroup(test_repo, "test_group") test_prefix1 = ConvertPrefix(prefix_group, "ten", "10", 10) test_prefix2 = ConvertPrefix(prefix_group, "hundred", "100", 100) prefix_group.add_prefix(test_prefix1) prefix_group.add_prefix(test_prefix2) test_unit.valid_prefix_group = prefix_group measure1 = ConvertMeasure(17.5, test_unit) # Get string measure_str = measure1.to_string_with_prefix(test_prefix2) # Check assert measure_str == "0.175 hundredname1"