示例#1
0
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
示例#2
0
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
示例#3
0
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
示例#4
0
def test_has_name():
    # 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")
示例#5
0
def test_remove_abbr():
    # 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
示例#6
0
def test_json():
    # 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
    json_data = test_type.to_json()
    rebuild_type = ConvertType.from_json(
        test_repo,
        json_data,
    )
    # 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"