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 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()
assert "(Table size) ** 2 / (Time) ** 2" == q4.GetCategory()
def testConvertionWithDerivedUnits(unit_database_len_time):
empty = Quantity.CreateDerived(OrderedDict())
m = Quantity.CreateDerived(OrderedDict([("Table size", ["m", 1])]))
m_city = Quantity.CreateDerived(OrderedDict([("City size", ["m", 1])]))
cm = Quantity.CreateDerived(OrderedDict([("Table size", ["cm", 1])]))
km_city = Quantity.CreateDerived(OrderedDict([("City size", ["km", 1])]))
m2 = Quantity.CreateDerived(OrderedDict([("Table size", ["m", 2])]))
s = Quantity.CreateDerived(OrderedDict([("Time", ["s", -1])]))
m2s = Quantity.CreateDerived(OrderedDict([("Table size", ["m", 2]), ("Time", ["s", -1])]))
cat_mix_m2 = Quantity.CreateDerived(
OrderedDict([("Table size", ["m", 1]), ("City size", ["m", 1])])
)
unit_database = unit_database_len_time
# multiplication
assert (m2, 2) == unit_database.Multiply(m, m, 1, 2)
assert (m2s, 2) == unit_database.Multiply(m2, s, 1, 2)
assert (m2s, 2) == unit_database.Multiply(m2, s, 1, 2)
assert (m2, 1) == unit_database.Multiply(m, cm, 1, 100)
assert (cat_mix_m2, 1) == unit_database.Multiply(m, m_city, 1, 1)
assert (cat_mix_m2, 1) == unit_database.Multiply(m, km_city, 1, 0.001)
# division
assert (m, 0.001) == unit_database.Divide(cat_mix_m2, km_city, 1, 1)
# check division with cancelling units (and different categories)
def testConvertionWithDerivedUnits(unit_database_len_time):
empty = Quantity.CreateDerived(OrderedDict())
m = Quantity.CreateDerived(OrderedDict([("Table size", ["m", 1])]))
m_city = Quantity.CreateDerived(OrderedDict([("City size", ["m", 1])]))
cm = Quantity.CreateDerived(OrderedDict([("Table size", ["cm", 1])]))
km_city = Quantity.CreateDerived(OrderedDict([("City size", ["km", 1])]))
m2 = Quantity.CreateDerived(OrderedDict([("Table size", ["m", 2])]))
s = Quantity.CreateDerived(OrderedDict([("Time", ["s", -1])]))
m2s = Quantity.CreateDerived(OrderedDict([("Table size", ["m", 2]), ("Time", ["s", -1])]))
cat_mix_m2 = Quantity.CreateDerived(
OrderedDict([("Table size", ["m", 1]), ("City size", ["m", 1])])
)
unit_database = unit_database_len_time
# multiplication
assert (m2, 2) == unit_database.Multiply(m, m, 1, 2)
assert (m2s, 2) == unit_database.Multiply(m2, s, 1, 2)
assert (m2s, 2) == unit_database.Multiply(m2, s, 1, 2)
assert (m2, 1) == unit_database.Multiply(m, cm, 1, 100)
assert (cat_mix_m2, 1) == unit_database.Multiply(m, m_city, 1, 1)
def testQuantityCaption(unit_database_posc_len):
unit_database = unit_database_posc_len
unit_database.AddUnitBase(UNKNOWN_QUANTITY_TYPE, UNKNOWN_QUANTITY_TYPE, UNKNOWN_UNIT)
unit_database.AddCategory(UNKNOWN_QUANTITY_TYPE, UNKNOWN_QUANTITY_TYPE)
q0 = ObtainQuantity(unit=UNKNOWN_UNIT, category=UNKNOWN_QUANTITY_TYPE)
assert "" == q0.GetUnknownCaption()
q = ObtainQuantity(
unit=UNKNOWN_UNIT, unknown_unit_caption="Feeeet", category=UNKNOWN_QUANTITY_TYPE
)
assert "Feeeet" == q.GetUnknownCaption()
assert "Feeeet " == q.GetUnitCaption()
q = Quantity.CreateDerived(OrderedDict([("length", ("m", 1))]), unknown_unit_caption="Feeeet")
assert "m" == q.GetUnitCaption()
def testReadOnlyOperation(unit_database_len_time):
unit_database = unit_database_len_time
m_ro = ObtainQuantity("m", "Table size")
m_rw = ObtainQuantity("m", "Table size")
m2 = Quantity.CreateDerived(OrderedDict([("Table size", ["m", 2])]))
# multiplication
assert (m2, 2) == unit_database.Multiply(m_rw, m_rw, 1, 2)
assert (m2, 2) == unit_database.Multiply(m_ro, m_rw, 1, 2)
assert (m2, 2) == unit_database.Multiply(m_ro, m_ro, 1, 2)
assert (m2, 2) == unit_database.Multiply(m_rw, m_ro, 1, 2)
quantity, _ = unit_database.Multiply(m_ro, m_ro, 1, 2)
assert isinstance(quantity, Quantity)
quantity, _ = unit_database.Multiply(m_rw, m_rw, 1, 2)
assert isinstance(quantity, Quantity)
quantity, _ = unit_database.Multiply(m_rw, m_ro, 1, 2)
assert isinstance(quantity, Quantity)
quantity, _ = unit_database.Multiply(m_ro, m_rw, 1, 2)
assert isinstance(quantity, Quantity)