def test_unit_interface_get_per_values(): pcs = UnitInterface(PiecesSalesUnit()) gram = get_g_in_kg_unit(decimals=3, display_decimals=1) gram.display_unit.comparison_value = 100 with translation.override('en'): assert pcs.get_per_values() == (1, '') assert pcs.get_per_values(force_symbol=True) == (1, 'pc.') assert gram.get_per_values() == (Decimal('0.1'), '100.0g')
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_unit_interface_get_per_values(): pcs = UnitInterface(PiecesSalesUnit()) gram = get_g_in_kg_unit(decimals=3, display_decimals=1) gram.display_unit.comparison_value = 100 with translation.override("en"): assert pcs.get_per_values() == (1, "") assert pcs.get_per_values(force_symbol=True) == (1, "pc.") assert gram.get_per_values() == (Decimal("0.1"), "100.0g")
def get_content(self, instance): shipment_products = ShipmentProduct.objects.filter( shipment=instance).select_related("product", "product__sales_unit") content = [] for shipment_product in shipment_products: unit = UnitInterface(shipment_product.product.sales_unit) quantity = unit.render_quantity(shipment_product.quantity) content.append(f"{shipment_product.product} ({quantity})") return ", ".join(content)
def test_unit_interface_display_precision(): sales_unit = SalesUnit(symbol="t") assert UnitInterface( display_unit=DisplayUnit(internal_unit=sales_unit, decimals=9 )).display_precision == Decimal('0.000000001') assert UnitInterface( display_unit=DisplayUnit(internal_unit=sales_unit, decimals=4 )).display_precision == Decimal('0.0001') assert UnitInterface( display_unit=DisplayUnit(internal_unit=sales_unit, decimals=2 )).display_precision == Decimal('0.01') assert UnitInterface( display_unit=DisplayUnit(internal_unit=sales_unit, decimals=0)).display_precision == Decimal('1')
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_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 test_unit_interface_render_quantity_internal_pieces(): pcs = UnitInterface(PiecesSalesUnit()) with translation.override("en"): assert pcs.render_quantity_internal(123) == "123" assert pcs.render_quantity_internal(1234567) == "1,234,567" assert pcs.render_quantity_internal(0.123) == "0" assert pcs.render_quantity_internal("52.1") == "52" assert pcs.render_quantity_internal("52.5") == "53" assert pcs.render_quantity_internal("53.5") == "54" assert pcs.render_quantity_internal(123, force_symbol=True) == "123pc."
def test_unit_interface_render_quantity_internal_pieces(): pcs = UnitInterface(PiecesSalesUnit()) with translation.override('en'): assert pcs.render_quantity_internal(123) == '123' assert pcs.render_quantity_internal(1234567) == '1,234,567' assert pcs.render_quantity_internal(0.123) == '0' assert pcs.render_quantity_internal('52.1') == '52' assert pcs.render_quantity_internal('52.5') == '53' assert pcs.render_quantity_internal('53.5') == '54' assert pcs.render_quantity_internal(123, force_symbol=True) == '123pc.'
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_unit_interface_init_with_non_default_display_unit(): sales_unit = SalesUnit.objects.create(name="Test", symbol="tst") default_du = DisplayUnit.objects.create( name="Default Display Unit", symbol="ddu", internal_unit=sales_unit, default=True) non_default_du = DisplayUnit.objects.create( name="Non-default Display Unit", symbol="ndu", internal_unit=sales_unit, default=False) assert sales_unit.display_unit == default_du unit = UnitInterface(sales_unit, non_default_du) assert unit.internal_unit == sales_unit assert unit.display_unit == non_default_du assert unit.internal_unit.display_unit == default_du
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_unit_interface_init_from_internal_unit(): sales_unit = SalesUnit(name="Test", symbol="tst") unit = UnitInterface(sales_unit) assert unit.internal_unit == sales_unit assert isinstance(unit.display_unit, SalesUnitAsDisplayUnit) assert unit.display_unit.internal_unit == sales_unit
def test_unit_interface_init_without_args(): unit = UnitInterface() assert isinstance(unit.internal_unit, PiecesSalesUnit) assert isinstance(unit.display_unit, SalesUnitAsDisplayUnit) assert unit.display_unit.internal_unit == unit.internal_unit