Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def testDerivedQuantities(unit_database_len_time):
# define a simple quantity
ObtainQuantity(unit="s", category="Time") # see if it works
ObtainQuantity(unit="m", category="Table size") # see if it works
q3 = Quantity.CreateDerived(OrderedDict([("Table size", ["m", 2]), ("Time", ["s", -1])]))
q4 = Quantity.CreateDerived(OrderedDict([("Table size", ["m", 2]), ("Time", ["s", -2])]))
q5 = Quantity.CreateDerived(
OrderedDict([("Table size", ["m", 1]), ("City size", ["m", 1]), ("Time", ["s", -2])])
)
q6 = Quantity.CreateDerived(OrderedDict([("Time", ["s", -2])]))
q7 = Quantity.CreateDerived(OrderedDict([("Table size", ["m", 1]), ("Time", ["s", 2])]))
with pytest.raises(InvalidUnitError):
Quantity.CreateDerived(
OrderedDict(
[("Table size", ["invalid", 1]), ("City size", ["m", 1]), ("Time", ["s", -2])]
)
)
assert "(Table size) ** 2 / Time" == q3.GetCategory()
assert "m2/s" == q3.GetUnit()
def testReadOnlyQuantity(unit_database_empty):
unit_database = unit_database_empty
unit_database.AddUnitBase("length", "meters", "m")
unit_database.AddUnit("length", "centimeters", "cm", "%f * 100.0", "%f / 100.0")
unit_database.AddCategory("length", "length")
read_only_quantity = ObtainQuantity("m", "length")
with pytest.raises(AttributeError):
read_only_quantity.SetUnit("cm")
# When creating a copy of a read only quantity we'll make it not read only anymore!
copy = read_only_quantity.MakeCopy(Quantity)
assert copy.GetUnitDatabase() is unit_database
# let'scalar clear up the unit manager
unit_database = unit_database_empty
unit_database.AddUnitBase("length", "meters", "m")
unit_database.AddUnit("length", "centimeters", "cm", "%f * 100.0", "%f / 100.0")
unit_database.AddUnit("length", "milimeters", "mm", "%f * 1000.0", "%f / 1000.0")
unit_database.AddUnit("length", "kilometers", "km", "%f / 1000.0", "%f * 1000.0")
unit_database.AddUnit("length", "miles", "mi", "%f / 1609.347", "%f * 1609.347")
unit_database.AddUnit("length", "inches", "in", "%f / 0.0254", "%f * 0.0254")
valid_units = ["m", "cm", "mm"]
unit_database.AddCategory("length", "length")
unit_database.AddCategory("well-length", "length", valid_units=valid_units)
unit_database.AddCategory("well-diameter", "length")
Quantity("well-length", "km")
scalar = units.Scalar("well-length", 1, "m")
assert valid_units == scalar.GetValidUnits()
scalar = units.Scalar("well-diameter", 1, "m")
assert ["m", "cm", "mm", "km", "mi", "in"] == scalar.GetValidUnits()
# Creating a scalar in a unit that isn't valid shouldn't raise an error and the unit
# will be added to valid units
scalar = units.Scalar("well-length", 1, "mi")
assert scalar.GetValidUnits() == ["m", "cm", "mm", "mi"]
def testArrayOperations(unit_database_len_time):
unit_database = unit_database_len_time
m = Quantity.CreateDerived(OrderedDict([("Table size", ["m", 1])]))
km_city = Quantity.CreateDerived(OrderedDict([("City size", ["km", 1])]))
s1 = Array.CreateWithQuantity(m, [1])
s2 = Array.CreateWithQuantity(km_city, [0.01])
initial1 = s1.GetQuantity().GetComposingUnits()
initial2 = s2.GetQuantity().GetComposingUnits()
# Check that they doesn't raise ComposedUnitError
s1.GetValues()
s2.GetValues()
quantity, value = unit_database.Multiply(m, km_city, 1, 0.01)
assert initial1 == s1.GetQuantity().GetComposingUnits()
assert initial2 == s2.GetQuantity().GetComposingUnits()
calculated1 = Array.CreateWithQuantity(quantity, [value])
array = s1 * s2
str(array) # just to see if it works...
def testArrayOperations(unit_database_len_time):
unit_database = unit_database_len_time
m = Quantity.CreateDerived(OrderedDict([("Table size", ["m", 1])]))
km_city = Quantity.CreateDerived(OrderedDict([("City size", ["km", 1])]))
s1 = Array.CreateWithQuantity(m, [1])
s2 = Array.CreateWithQuantity(km_city, [0.01])
initial1 = s1.GetQuantity().GetComposingUnits()
initial2 = s2.GetQuantity().GetComposingUnits()
# Check that they doesn't raise ComposedUnitError
s1.GetValues()
s2.GetValues()
quantity, value = unit_database.Multiply(m, km_city, 1, 0.01)
assert initial1 == s1.GetQuantity().GetComposingUnits()
assert initial2 == s2.GetQuantity().GetComposingUnits()
calculated1 = Array.CreateWithQuantity(quantity, [value])
array = s1 * s2
def testQuantity(unit_database_well_length):
Quantity("well-length", "m")
with pytest.raises(units.InvalidQuantityTypeError):
Quantity("foo", "m")
def testCreationWithDerivedQuantity(unit_database_len_time):
unit_database = unit_database_len_time
m = Quantity.CreateDerived(OrderedDict([("Table size", ["m", 1])]))
km_city = Quantity.CreateDerived(OrderedDict([("City size", ["km", 1])]))
quantity, value = unit_database.Multiply(m, km_city, 1, 0.01)
calculated1 = Scalar.CreateWithQuantity(quantity, value)
assert str(calculated1)
s1 = Scalar.CreateWithQuantity(m, 1)
s2 = Scalar.CreateWithQuantity(km_city, 0.01)
assert calculated1 == s1 * s2
def testQuantity(unit_database_well_length):
Quantity("well-length", "m")
with pytest.raises(units.InvalidQuantityTypeError):
Quantity("foo", "m")
def testCreateWithQuantity(unit_database_start_units):
units.FixedArray.CreateWithQuantity(
Quantity.CreateDerived(OrderedDict()), [100, 150, 200], dimension=3
)
quantity = ObtainQuantity("m", "length")
a1 = units.FixedArray(3, quantity, values=[1, 2, 3])
a2 = units.FixedArray(3, quantity, values=[1, 2, 3])
assert a1.GetValues("km") == [0.001, 0.002, 0.003]
assert a2.GetValues() == [1, 2, 3]