Exemplo n.º 1
0
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"
Exemplo n.º 2
0
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"
Exemplo n.º 3
0
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"
Exemplo n.º 4
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"