コード例 #1
0
ファイル: test_unit_database.py プロジェクト: ESSS/barril
def testCategory(unit_database_custom_conversion) -> None:
    unit_database = unit_database_custom_conversion
    unit_database.AddCategory(
        "my length",
        "length",
        ["mm", "m"],
        default_unit="m",
        default_value=15.5,
        min_value=-6e-10,
        max_value=2e5,
        is_min_exclusive=True,
        is_max_exclusive=False,
        caption="My Length",
    )

    assert unit_database.GetDefaultValue("my length") == 15.5
    assert unit_database.GetDefaultUnit("my length") == "m"
    assert sorted(list(
        unit_database.IterCategories())) == ["length", "my length"]

    quantity = ObtainQuantity("m", "my length")
    formatted_value = FormatFloat("%g", -6e-010)
    with pytest.raises(
            QuantityValidationError,
            match="Invalid value for My Length: %s. Must be > %s." %
        (formatted_value, formatted_value),
    ) as exc_info:
        quantity.CheckValue(-6e-10)
    e = exc_info.value
    assert e.message == "Invalid value for My Length: {}. Must be > {}.".format(
        formatted_value, formatted_value)
    assert e.caption == "My Length"
    assert e.value == float(formatted_value)
    assert e.operator == ">"
    assert e.value == float(formatted_value)

    quantity.CheckValue(0)  # without specifying unit
    mm_quantity = ObtainQuantity("mm", "my length")
    mm_quantity.CheckValue(2e5)

    with pytest.raises(
            QuantityValidationError,
            match="Invalid value for My Length: 200000. Must be <= 200000.0."):
        mm_quantity.CheckValue(2e8 + 1)

    # Check the unit info using a category instead a quantity_type
    with pytest.raises(InvalidQuantityTypeError):
        unit_database.GetInfo("unknown length", "mm")

    unit_database.GetInfo("my length", "mm")
    unit_database.GetInfo(UNKNOWN_QUANTITY_TYPE,
                          "I know it is m3",
                          fix_unknown=True)
コード例 #2
0
    def _ScalarCheckMsgPredicate(cls, scalar: Scalar) -> Optional[str]:
        """
        :param Scalar scalar:
            The scalar to be checked against its limits

        :returns str:
            The built message saying if the scalar is less or greater than its limits
        """
        try:
            quantity = ObtainQuantity(scalar.GetUnit(), scalar.GetCategory())
            quantity.CheckValue(scalar.GetValue(), use_literals=True)

        except ValueError as error:
            return str(error)

        return None