import pytest from app.models import BaseUnit, Product from app.services.units import UnitError, base_to_packages, to_base_quantity def test_base_unit_passthrough(rice): assert to_base_quantity(rice, 200, "g") == 200 assert to_base_quantity(rice, 200, "gramm") == 200 def test_package_conversion(rice): # 3 Packungen * 500 g = 1500 g assert to_base_quantity(rice, 3, "package") == 1500 assert to_base_quantity(rice, 2, "packung") == 1000 def test_wrong_unit_rejected(rice): with pytest.raises(UnitError): to_base_quantity(rice, 1, "ml") def test_package_without_size_rejected(): product = Product(name="Ei", base_unit=BaseUnit.piece, package_size=None) with pytest.raises(UnitError): to_base_quantity(product, 1, "package") def test_non_positive_quantity_rejected(rice): with pytest.raises(UnitError): to_base_quantity(rice, 0, "g") def test_base_to_packages(rice): assert base_to_packages(rice, 1500) == 3 product = Product(name="Ei", base_unit=BaseUnit.piece, package_size=None) assert base_to_packages(product, 10) is None