def testCubicFeetPerDayLegacyUnits() -> None: # test creating scalar using legacy representation and default category q = Scalar(1.0, "1000ft3/d") assert q.GetUnit() == "Mcf/d" # test creating scalar using legacy representation q = Scalar(category="volume flow rate", value=1.0, unit="1000ft3/d") assert q.GetUnit() == "Mcf/d" # test getting value using legacy representation assert approx(q.GetValue("M(ft3)/d")) == q.GetValue("MMcf/d") q = Scalar(category="volume flow rate", value=1.0, unit="M(ft3)/d") assert q.GetUnit() == "MMcf/d" assert approx(q.GetValue("M(ft3)/d")) == q.GetValue("MMcf/d") assert q.GetUnitName() == "million cubic feet per day"
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 testConvertLegacyUnit(): from barril.units.unit_database import _LEGACY_TO_CURRENT # test creating scalar using legacy representation and default category q = Scalar(1.0, "1000ft3/d") assert q.GetUnit() == "Mcf/d" # test creating scalar using legacy representation q = Scalar(category="volume flow rate", value=1.0, unit="1000ft3/d") assert q.GetUnit() == "Mcf/d" # test getting value using legacy representation assert approx(q.GetValue("M(ft3)/d")) == q.GetValue("MMcf/d") q = Scalar(category="volume flow rate", value=1.0, unit="M(ft3)/d") assert q.GetUnit() == "MMcf/d" assert approx(q.GetValue("M(ft3)/d")) == q.GetValue("MMcf/d") assert q.GetUnitName() == "million cubic feet per day" # Test all possible legacy formats for legacy, current in _LEGACY_TO_CURRENT: assert Scalar(1.0, legacy).GetUnit() == current
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
def testConversionToFullScalar() -> None: y = _LightweightScalar(value=1.0, unit="kg/m3", category="density") x = Scalar(value=y.GetValue(), unit=y.GetUnit(), category=y.GetCategory()) assert x.GetValue() == 1.0 assert x.GetUnit() == "kg/m3" assert x.GetCategory() == "density" assert x.GetQuantityType() == "density" assert x.GetQuantity().GetUnit() == "kg/m3" assert x.GetQuantity().GetCategory() == "density" assert x.GetQuantity().GetQuantityType() == "density" # Full Scalar supports conversion assert x.GetValue(unit="g/cm3") == 0.001
def testAllLegacyUnits(legacy: str, current: str, value: float) -> None: test_scalar = Scalar(value, legacy) assert test_scalar.GetUnit() == current assert approx(test_scalar.GetValue(legacy)) == value assert approx(test_scalar.GetValue(current)) == value