# ----------------------------------------------------------------------------- # Food-Ka — tests de la couche de normalisation # Auteur : Simon-Pierre Boucher — contact@spboucher.ai # ----------------------------------------------------------------------------- from foodka.normalize import ( normalize_category, parse_price, parse_size, unit_price, ) from foodka.schema import Product def test_parse_price(): assert parse_price("3,49 $") == 3.49 assert parse_price("$3.49") == 3.49 assert parse_price("2 / 5,00 $") == 2.5 assert parse_price("3 pour 12,00 $") == 4.0 assert parse_price("99 ¢") == 0.99 assert parse_price("1 249,99 $") == 1249.99 assert parse_price(4.29) == 4.29 assert parse_price("prix en magasin") is None assert parse_price(None) is None def test_parse_size(): assert parse_size("450 g") == (450.0, "g") assert parse_size("2 L") == (2000.0, "ml") assert parse_size("1,5 L") == (1500.0, "ml") assert parse_size("12 x 355 ml") == (4260.0, "ml") assert parse_size("6 un") == (6.0, "un") assert parse_size("1 lb") == (453.592, "g") assert parse_size("") is None def test_unit_price(): assert unit_price(3.5, "500 g") == (0.7, "100 g") assert unit_price(6.0, "2 L") == (0.3, "100 ml") assert unit_price(12.0, "6 un") == (2.0, "un") assert unit_price(None, "500 g") is None def test_normalize_category(): assert normalize_category("Fruits et légumes frais") == "Fruits et légumes" assert normalize_category("Dairy & Eggs") == "Produits laitiers et œufs" assert normalize_category("Boissons gazeuses") == "Boissons" assert normalize_category("Surgelés") == "Surgelés" assert normalize_category("Mystère") == "Autres" def test_product_finalize_sale(): p = Product(source="t", external_id="1", url="u", name=" Beurre ", price_label="4,99 $", size_label="454 g", regular_price=6.49).finalize() assert p.price == 4.99 assert p.on_sale is True assert p.unit_price is not None assert p.category == "Autres" def test_product_finalize_bogus_regular(): # prix régulier <= prix courant : pas un solde, on le laisse tomber p = Product(source="t", external_id="1", url="u", name="x", price=5.0, regular_price=5.0).finalize() assert p.regular_price is None assert p.on_sale is False