def test_to_float(self): """Return floats for any given integer, decimal, or fraction.""" assert isinstance(Quantity.to_float(3.145), float) assert isinstance(Quantity.to_float(Decimal('2.544')), float) assert isinstance(Quantity.to_float('5.456'), float) assert isinstance(Quantity.to_float(132), float) assert isinstance(Quantity.to_float(Fraction(3, 4)), float)
def test_from_queryable_values(self, db): """Return a Quantity with the given value and units.""" qty1 = Quantity(value='1/4', units='gram') qty2 = Quantity(value=0.25, units='gram') db.session.add_all([qty1, qty2]) assert Quantity.from_queryable_values(value='1/4', units='gram') is qty1 assert Quantity.from_queryable_values(value=0.25, units='gram') is qty2
def test_dec_check(self): """Dec check returns True if value looks like a decimal number.""" assert Quantity.dec_check(3.145) assert Quantity.dec_check(Decimal('1.75')) assert Quantity.dec_check('4.55') assert not Quantity.dec_check(Fraction(3, 4)) assert not Quantity.dec_check('$3.50') assert not Quantity.dec_check(33) assert not Quantity.dec_check([4.3, 35.34])
def test_value_getter(self): """Return value in appropriate format.""" qty = Quantity() qty.value = '3.1415' assert qty.value == 3.1415 qty.value = Decimal('5.21') assert qty.value == 5.21 qty.value = 100 assert qty.value == 100 qty.value = '100' assert qty.value == 100 qty.value = '4/3' assert qty.value == Fraction(4, 3) qty.value = Fraction(1, 2) assert qty.value == Fraction(1, 2) qty.value = None assert qty.value is None
def test_value_setter(self): """Set value appropriately given valid data.""" qty = Quantity() qty.value = 100 assert qty._numerator == 100 assert qty._denominator == 1 assert not qty.is_decimal assert qty._float == 100.0 qty.value = 100 assert qty._numerator == 100 assert qty._denominator == 1 assert not qty.is_decimal assert qty._float == 100.0 qty.value = 3.1415 assert qty.is_decimal assert qty._numerator is None assert qty._denominator is None assert qty._float == 3.1415 qty.value = '3.1415' assert qty.is_decimal assert qty._numerator is None assert qty._denominator is None assert qty._float == 3.1415 qty.value = Decimal('3.1415') assert qty.is_decimal assert qty._numerator is None assert qty._denominator is None assert qty._float == 3.1415 qty.value = Fraction(1, 2) assert not qty.is_decimal assert qty._numerator == 1 assert qty._denominator == 2 assert qty._float == 0.5 qty.value = Fraction(5, 2) assert not qty.is_decimal assert qty._numerator == 5 assert qty._denominator == 2 assert qty._float == 2.5 qty.value = '3/4' assert not qty.is_decimal assert qty._numerator == 3 assert qty._denominator == 4 assert qty._float == 0.75 qty.value = '1 1/4' assert not qty.is_decimal assert qty._numerator == 5 assert qty._denominator == 4 assert qty._float == 1.25 qty.value = None assert qty.is_decimal is None assert qty._numerator is None assert qty._denominator is None assert qty._float is None
def test_html_value(self): """Return HTML entities or special HTML for fractions. Return a string of self.value if it is not a fraction. """ qty = Quantity() qty.value = Fraction(1, 4) assert qty.html_value == '¼' qty.value = Fraction(1, 2) assert qty.html_value == '½' qty.value = Fraction(3, 4) assert qty.html_value == '¾' qty.value = Fraction(1, 3) assert qty.html_value == '⅓' qty.value = Fraction(2, 3) assert qty.html_value == '⅔' qty.value = Fraction(1, 5) assert qty.html_value == '⅕' qty.value = Fraction(2, 5) assert qty.html_value == '⅖' qty.value = Fraction(3, 5) assert qty.html_value == '⅗' qty.value = Fraction(4, 5) assert qty.html_value == '⅘' qty.value = Fraction(1, 6) assert qty.html_value == '⅙' qty.value = Fraction(5, 6) assert qty.html_value == '⅚' qty.value = Fraction(1, 8) assert qty.html_value == '⅛' qty.value = Fraction(3, 8) assert qty.html_value == '⅜' qty.value = Fraction(5, 8) assert qty.html_value == '⅝' qty.value = Fraction(7, 8) assert qty.html_value == '⅞' qty.value = Fraction(9, 11) assert qty.html_value == '<span class="fraction"><sup>9</sup>⁄'\ '<sub>11</sub></span>' qty.value = Decimal('3.1415') assert qty.html_value == '3.1415' qty.value = 100 assert qty.html_value == '100' qty.value = '100' assert qty.html_value == '100'
def test_str_to_fraction(self): """Return a Fraction given a valid string containing a fraction. If val is not parseable, raise ValueError. """ assert Quantity.str_to_fraction('3/4') == Fraction(3, 4) assert Quantity.str_to_fraction('1 1/2') == Fraction(3, 2) assert Quantity.str_to_fraction('3 4/11') == Fraction(37, 11) assert Quantity.str_to_fraction('13/3') == Fraction(13, 3) with pytest.raises(ValueError): Quantity.str_to_fraction('3/4/3') with pytest.raises(ValueError): Quantity.str_to_fraction('3 4 3/4') with pytest.raises(ValueError): Quantity.str_to_fraction('$2.5') with pytest.raises(ValueError): Quantity.str_to_fraction('$2 3/4')
def test_fraction_to_str(self): """Return fractions in a human-friendly string format. Raise TypeError if not given a Fraction. """ assert Quantity.fraction_to_str(Fraction(3, 4)) == '3/4' assert Quantity.fraction_to_str(Fraction(13, 3)) == '4 1/3' assert Quantity.fraction_to_str(Fraction(235, 22)) == '10 15/22' with pytest.raises(TypeError): Quantity.fraction_to_str(3.1415) with pytest.raises(TypeError): Quantity.fraction_to_str(Decimal('3.253')) with pytest.raises(TypeError): Quantity.fraction_to_str(2432) with pytest.raises(TypeError): Quantity.fraction_to_str('4/3')
def test_repr(self): """Return a string representing a Quantity.""" qty = Quantity(100, 'seeds') assert qty.__repr__() == '<Quantity "100 seeds">'