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]"
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()
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()