def test_unit_interface_init_from_display_unit(): sales_unit = SalesUnit(name="Test", symbol="tst") display_unit = DisplayUnit(name="Test2", symbol="t2", internal_unit=sales_unit) unit = UnitInterface(display_unit=display_unit) assert unit.display_unit == display_unit assert unit.internal_unit == sales_unit
def get_g_in_kg_unit(decimals, display_decimals, comparison=100): return UnitInterface( display_unit=DisplayUnit(internal_unit=get_kilogram_sales_unit( decimals=decimals), ratio=Decimal('0.001'), decimals=display_decimals, symbol='g', comparison_value=comparison))
def test_unit_interface_init_unit_compatibility_check(): su1 = SalesUnit(identifier="SU1", symbol="su1") su2 = SalesUnit(identifier="SU2", symbol="su2") du = DisplayUnit(name="DU", symbol="du", internal_unit=su1) UnitInterface(su1, du) # OK with pytest.raises(AssertionError) as exc_info: UnitInterface(su2, du) assert str(exc_info.value) == ( "Incompatible units: <SalesUnit:None-SU2>, <DisplayUnit:None>")
def test_kg_in_oz(): kg_oz = UnitInterface(display_unit=DisplayUnit( internal_unit=get_kilogram_sales_unit(decimals=9), ratio=Decimal('0.028349523'), decimals=3, symbol='oz')) assert kg_oz.comparison_quantity == Decimal('0.028349523') assert kg_oz.render_quantity('0.028349523') == '1.000oz' assert kg_oz.render_quantity(1) == '35.274oz' assert kg_oz.render_quantity(0.001) == '0.035oz' assert kg_oz.from_display(Decimal('0.001')) == Decimal('0.000028350') assert kg_oz.to_display(Decimal('0.000028350')) == approx('0.001')
def test_kg_in_oz(): kg_oz = UnitInterface( display_unit=DisplayUnit( internal_unit=get_kilogram_sales_unit(decimals=9), ratio=Decimal("0.028349523"), decimals=3, symbol="oz" ) ) assert kg_oz.comparison_quantity == Decimal("0.028349523") assert kg_oz.render_quantity("0.028349523") == "1.000oz" assert kg_oz.render_quantity(1) == "35.274oz" assert kg_oz.render_quantity(0.001) == "0.035oz" assert kg_oz.from_display(Decimal("0.001")) == Decimal("0.000028350") assert kg_oz.to_display(Decimal("0.000028350")) == approx("0.001")
def test_display_unit_validator(): sales_unit = SalesUnit.objects.create() display_unit = DisplayUnit(internal_unit=sales_unit, ratio=0) with pytest.raises(ValidationError): display_unit.full_clean()