spb/food-ka Public
Food-Ka — agrégateur de produits d'épicerie du Québec — www.food-ka.com
Python 57.7%
TypeScript 24.9%
CSS 16.7%
HTML 0.6%
1# -----------------------------------------------------------------------------2# Food-Ka — tests de la couche de normalisation3# Auteur : Simon-Pierre Boucher — contact@spboucher.ai4# -----------------------------------------------------------------------------5from foodka.normalize import (6 normalize_category,7 parse_price,8 parse_size,9 unit_price,10)11from foodka.schema import Product121314def test_parse_price():15 assert parse_price("3,49 $") == 3.4916 assert parse_price("$3.49") == 3.4917 assert parse_price("2 / 5,00 $") == 2.518 assert parse_price("3 pour 12,00 $") == 4.019 assert parse_price("99 ¢") == 0.9920 assert parse_price("1 249,99 $") == 1249.9921 assert parse_price(4.29) == 4.2922 assert parse_price("prix en magasin") is None23 assert parse_price(None) is None242526def test_parse_size():27 assert parse_size("450 g") == (450.0, "g")28 assert parse_size("2 L") == (2000.0, "ml")29 assert parse_size("1,5 L") == (1500.0, "ml")30 assert parse_size("12 x 355 ml") == (4260.0, "ml")31 assert parse_size("6 un") == (6.0, "un")32 assert parse_size("1 lb") == (453.592, "g")33 assert parse_size("") is None343536def test_unit_price():37 assert unit_price(3.5, "500 g") == (0.7, "100 g")38 assert unit_price(6.0, "2 L") == (0.3, "100 ml")39 assert unit_price(12.0, "6 un") == (2.0, "un")40 assert unit_price(None, "500 g") is None414243def test_normalize_category():44 assert normalize_category("Fruits et légumes frais") == "Fruits et légumes"45 assert normalize_category("Dairy & Eggs") == "Produits laitiers et œufs"46 assert normalize_category("Boissons gazeuses") == "Boissons"47 assert normalize_category("Surgelés") == "Surgelés"48 assert normalize_category("Mystère") == "Autres"495051def test_product_finalize_sale():52 p = Product(source="t", external_id="1", url="u", name=" Beurre ",53 price_label="4,99 $", size_label="454 g",54 regular_price=6.49).finalize()55 assert p.price == 4.9956 assert p.on_sale is True57 assert p.unit_price is not None58 assert p.category == "Autres"596061def test_product_finalize_bogus_regular():62 # prix régulier <= prix courant : pas un solde, on le laisse tomber63 p = Product(source="t", external_id="1", url="u", name="x",64 price=5.0, regular_price=5.0).finalize()65 assert p.regular_price is None66 assert p.on_sale is False67