Esempio n. 1
0
def testScalarInvalidValue(unit_database_len_time):
    db = unit_database_len_time

    db.AddCategory("another-length", "length", min_value=0, max_value=15)

    scalar = Scalar("another-length", value=15, unit="m")
    assert scalar.IsValid()

    scalar = Scalar("another-length", value=-5, unit="m")
    assert not scalar.IsValid()

    # By default the validation will be performed. 15 is a valid value.
    scalar = Scalar("another-length", value=15, unit="m")
    assert scalar.IsValid()

    # Even invalid ,the scalar returns the value, unit and a formatted text.
    another = Scalar("another-length", value=3000, unit="m")
    assert not another.IsValid()
    assert another.GetValue("m") == 3000
    assert another.GetUnit() == "m"
    assert another.GetFormatted() == "3000 [m]"

    # By default the validation will be performed, and in this cases will raise ValueError.
    another_2 = Scalar("another-length", unit="m", value=5000)
    assert not another_2.IsValid()

    # Performing copy between invalid scalars. The validation is not performed on copy.
    copied = another.CreateCopy(unit="cm")
    assert not copied.IsValid()
    assert copied.GetValue("m") == 3000
    assert copied.GetUnit() == "cm"
    assert copied.GetFormatted() == "300000 [cm]"
Esempio n. 2
0
def testCopyPropertiesAndValidation(unit_database_well_length):
    category = "well-length-with-min-and-max"  # the minimum value is zero
    unit = "m"
    value = -1

    # Not raises exception because validation is False on the copy operation
    scalar_source = Scalar(category, value, unit)
    assert not scalar_source.IsValid()

    # Raises exception because validation is True and the value -1 is invalid accordingly to
    # the category 'well-length-with-min-and-max'
    another = scalar_source.CreateCopy()
    assert not another.IsValid()
Esempio n. 3
0
def testCategoryParameters(unit_database_empty):
    db = unit_database_empty

    db.AddUnitBase("length", "meters", "m")
    db.AddUnit("length", "centimeters", "cm", "x * 100.0", "x / 100.0")

    db.AddCategory(
        "my length",
        "length",
        ["cm", "m"],
        default_unit="m",
        default_value=18.5,
        min_value=-30.0,
        max_value=100.0,
        is_min_exclusive=True,
        is_max_exclusive=False,
    )

    s1 = Scalar("my length")
    assert s1.value == 18.5
    assert s1.unit == "m"

    # test maximum boundary
    s2 = Scalar("my length", 100.0, "m")
    assert s2.value == 100.0
    assert s2.unit == "m"

    s3 = Scalar("my length", 500.0, "cm")
    assert s3.value == 500.0
    assert s3.unit == "cm"

    # higher than maximum value
    another = Scalar("my length", 120.0, "m")
    assert not another.IsValid()
    with pytest.raises(ValueError):
        another.CheckValidity()

    another = Scalar("my length", -30.0, "m")
    assert not another.IsValid()

    s4 = Scalar("my length", unit="cm")
    assert s4.value == 1850.0
    assert s4.unit == "cm"

    another = s4.CreateCopy(12500.00, "cm")
    assert not another.IsValid()
Esempio n. 4
0
def testScalarCopyAndRepresentation(unit_database_empty):
    unit_database = unit_database_empty

    unit_database.AddUnitBase("length", "meters", "m")
    unit_database.AddUnit("length", "milimeters", "mm", "x * 1000.0", "x / 1000.0")
    unit_database.AddUnit("length", "centimeters", "cm", "x * 100.0", "x / 100.0")
    unit_database.AddCategory("well-diameter", "length")
    s = Scalar("well-diameter", 10, "m")

    assert "Scalar(10.0, 'm', 'well-diameter')" == repr(s)
    assert "10.0" == six.text_type(s.value)
    assert "m" == s.unit
    assert "well-diameter" == s.GetCategory()
    assert "length" == s.GetQuantityType()

    s = s.CreateCopy(unit="cm")
    assert "Scalar(1000.0, 'cm', 'well-diameter')" == repr(s)
    assert "1000 [cm]" == six.text_type(s)