예제 #1
0
def test_adding_unit():
    "Tests adding unit"
    unit_graph = UnitGraph("Distance")
    unit_graph.add_unit("m", "meter", "m")
    unit_graph.add_unit("cm", "centimeter", "cm")
    assert len(unit_graph.get_units()) == 2
    assert unit_graph.get_units() == ["m", "cm"]
    assert unit_graph.has_unit("m") is True
    assert unit_graph.has_unit("nm") is False
예제 #2
0
def test_constructing_full_graph():
    """Tests constructing full graph"""
    unit_graph = UnitGraph("Distance")
    unit_graph.add_unit("m", "meter", "m")
    unit_graph.add_unit("cm", "centimeter", "cm")
    unit_graph.add_unit("mm", "millimeter", "mm")
    unit_graph.add_conversion("m", "cm", 100)
    unit_graph.add_conversion("m", "mm", 1000)
    assert unit_graph.has_edge("cm", "mm") is False
    unit_graph.construct_full_graph()
    assert unit_graph["cm"]["mm"]["weight"] == 10
예제 #3
0
def test_validate_units():
    """Test units validation"""
    unit_graph1 = UnitGraph("U1")
    unit_graph1.add_unit("m", "meter", "m")
    unit_graph1.add_unit("cm", "centimeter", "cm")
    unit_graph1.add_unit("mm", "millimeter", "mm")
    convertor_dictionary = {}
    convertor_dictionary["U1"] = unit_graph1
    unit_convertor = UnitConvertor(convertor_dictionary)
    unit_convertor.validate_units(["m", "cm"], "U1")
    with pytest.raises(UnitsTypeMismatch):
        unit_convertor.validate_units(["m", "um"], "U1")
예제 #4
0
def test_possible_types():
    """Test possible types"""
    unit_graph1 = UnitGraph("U1")
    unit_graph2 = UnitGraph("U2")
    unit_graph3 = UnitGraph("U3")

    unit_graph1.add_unit("m", "meter", "m")
    unit_graph1.add_unit("cm", "centimeter", "cm")
    unit_graph2.add_unit("cm", "centimeter", "cm")
    unit_graph3.add_unit("m", "meter", "m")

    convertor_dictionary = {}
    convertor_dictionary["U1"] = unit_graph1
    convertor_dictionary["U2"] = unit_graph2
    convertor_dictionary["U3"] = unit_graph3

    unit_convertor = UnitConvertor(convertor_dictionary)
    possible_types = unit_convertor.get_possible_types(["m"])
    assert possible_types == ["U1", "U3"]
    with pytest.raises(TypeDetectionNotPossible):
        unit_convertor.validate_possible_type("m", possible_types)

    possible_types = unit_convertor.get_possible_types(["m", "cm"])
    assert possible_types == ["U1"]
    unit_type = unit_convertor.validate_possible_type(["m", "cm"],
                                                      possible_types)
    assert unit_type == "U1"

    possible_types = unit_convertor.get_possible_types(["mm"])
    assert len(possible_types) == 0
    with pytest.raises(TypeDetectionNotPossible):
        unit_convertor.validate_possible_type(["mm"], possible_types)
예제 #5
0
def test_adding_conversion():
    "Tests adding unit"
    unit_graph = UnitGraph("Distance")
    unit_graph.add_unit("m", "meter", "m")
    unit_graph.add_unit("cm", "centimeter", "cm")
    unit_graph.add_conversion("m", "cm", 100)
    assert unit_graph["m"]["cm"]["weight"] == 100
    assert unit_graph["cm"]["m"]["weight"] == 0.01

    with pytest.raises(UnitNotFound):
        unit_graph.add_conversion("m", "mm", 1000)

    unit_graph.add_conversion("m", "mm", 1000, is_force_add=True)
    assert unit_graph["m"]["mm"]["weight"] == 1000
    assert unit_graph["mm"]["m"]["weight"] == 0.001
예제 #6
0
def test_convertion():
    """Testing conversion"""
    unit_graph = UnitGraph("length")
    unit_graph.add_unit("m", "meter", "m")
    unit_graph.add_unit("cm", "centimeter", "cm")
    unit_graph.add_unit("mm", "millimeter", "mm")
    unit_graph.add_conversion("m", "mm", 1000)
    unit_graph.add_conversion("m", "cm", 100)

    convertor_dictionary = {}
    convertor_dictionary["length"] = unit_graph

    unit_convertor = UnitConvertor(convertor_dictionary)
    value_m = 10
    value_cm = unit_convertor.convert(value_m, "m", "cm")
    assert value_cm == 1000

    if sys.version_info.minor >= 8:
        value_cm = unit_convertor.to(value_m, "cm")
        assert value_cm == 1000
예제 #7
0
def test_get_multiplier():
    """Test get multiplier"""
    unit_graph = UnitGraph("length")
    unit_graph.add_unit("m", "meter", "m")
    unit_graph.add_unit("cm", "centimeter", "cm")
    unit_graph.add_unit("mm", "millimeter", "mm")
    unit_graph.add_conversion("m", "mm", 1000)
    unit_graph.add_conversion("m", "cm", 100)

    convertor_dictionary = {}
    convertor_dictionary["length"] = unit_graph

    unit_convertor = UnitConvertor(convertor_dictionary)

    assert unit_convertor.get_multiplier("cm", "m")[2] == 0.01
    assert unit_convertor.get_multiplier("cm", "m", "length")[2] == 0.01

    with pytest.raises(TypeNotFound):
        unit_convertor.get_multiplier("cm", "m", "Weight")

    unit_graph.construct_full_graph()
    assert unit_convertor.get_multiplier("cm", "m", "length")[2] == 0.01
예제 #8
0
def read_unit_table(
    unit_type: str,
    unit_table_directory: Path,
    is_force_add: bool = False,
    is_construct: bool = True,
) -> UnitGraph:
    """Construct a unit graph from the csv file"""

    # Construct the file names
    unit_names_table_filename = unit_table_directory / (unit_type +
                                                        "_names.csv")
    unit_converson_table_filename = unit_table_directory / (unit_type +
                                                            "_conversions.csv")

    # Read the conversion table
    unit_graph = UnitGraph(unit_type)

    # Create the nodes
    with open(unit_names_table_filename, mode="r", encoding="utf-8") as file:
        csv_file = csv.reader(file)
        next(csv_file)
        for line in csv_file:
            unit_graph.add_unit(line[0], line[1], line[2], line[3])

    # Create the nodes
    with open(unit_converson_table_filename, mode="r",
              encoding="utf-8") as file:
        csv_file = csv.reader(file)
        next(csv_file)
        for line in csv_file:
            unit_graph.add_conversion(line[0], line[1], float(line[2]),
                                      is_force_add)

    # Build full graph
    if is_construct:
        unit_graph.construct_full_graph()

    return unit_graph
예제 #9
0
"""Script used for quick testing"""
import qiyas
from qiyas.generator import get_unit_types, generate_graphs
from qiyas.graph import UnitGraph
from qiyas.convertor import load_convertor_from_qs_files

unit_types = get_unit_types(qiyas.UNIT_TABLES_DIRECTORY)

unit_graph = UnitGraph("Distance")
unit_graph.add_unit("m", "meter")
unit_graph.add_unit("cm", "centimeter")
is_unit = unit_graph.has_unit("cm")
# print(unit_graph.graph.nodes)


d = qiyas.UNIT_TABLES_DIRECTORY / "distance_names.csv"

generate_graphs(qiyas.UNIT_TABLES_DIRECTORY, qiyas.UNIT_GRAPHS_DIRECTORY)
uc = load_convertor_from_qs_files(qiyas.UNIT_GRAPHS_DIRECTORY)
multplier = uc.get_multiplier("km", "cm", "length")
print(multplier)
value_m = 10  # pylint: disable=invalid-name
value_cm = uc.to(value_m, "cm")
print(value_cm)
multplier = uc.get_multiplier("km", "cm")