From 4420c48cd89252495ddede68fda66f5ce9622fbd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Bournhonesque?= Date: Thu, 12 Dec 2024 15:44:52 +0100 Subject: [PATCH 1/3] feat: add `Flavor.from_product_type` method --- openfoodfacts/types.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/openfoodfacts/types.py b/openfoodfacts/types.py index da1416a6..6756510f 100644 --- a/openfoodfacts/types.py +++ b/openfoodfacts/types.py @@ -66,6 +66,19 @@ def is_food(self) -> bool: otherwise.""" return self in (self.off, self.off_pro) + @classmethod + def from_product_type(cls, product_type: str) -> "Flavor": + """Get the `Flavor` associated with a `product_type`.""" + if product_type == "food": + return Flavor.off + elif product_type == "beauty": + return Flavor.obf + elif product_type == "petfood": + return Flavor.opff + elif product_type == "product": + return Flavor.opf + raise ValueError(f"no Flavor matched with product_type '{product_type}'") + class APIVersion(str, enum.Enum): v0 = "v0" From 7de7b93a1b00c7e4840ad8c45bffbeb2b3c31550 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Bournhonesque?= Date: Thu, 12 Dec 2024 15:46:02 +0100 Subject: [PATCH 2/3] fix: fix unused import --- tests/unit/test_taxonomy.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/unit/test_taxonomy.py b/tests/unit/test_taxonomy.py index 6e0a1c47..0482bed4 100644 --- a/tests/unit/test_taxonomy.py +++ b/tests/unit/test_taxonomy.py @@ -9,7 +9,6 @@ create_taxonomy_mapping, map_to_canonical_id, ) -from openfoodfacts.types import TaxonomyType def test_map_to_canonical_id(): From 603acceb55473611438816927152f04fc040d9d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Bournhonesque?= Date: Thu, 12 Dec 2024 15:47:51 +0100 Subject: [PATCH 3/3] tests: add unit tests --- tests/unit/test_types.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 tests/unit/test_types.py diff --git a/tests/unit/test_types.py b/tests/unit/test_types.py new file mode 100644 index 00000000..466cbea2 --- /dev/null +++ b/tests/unit/test_types.py @@ -0,0 +1,26 @@ +import pytest + +from openfoodfacts.types import Flavor + + +def test_from_product_type_food(): + assert Flavor.from_product_type("food") == Flavor.off + + +def test_from_product_type_beauty(): + assert Flavor.from_product_type("beauty") == Flavor.obf + + +def test_from_product_type_petfood(): + assert Flavor.from_product_type("petfood") == Flavor.opff + + +def test_from_product_type_product(): + assert Flavor.from_product_type("product") == Flavor.opf + + +def test_from_product_type_invalid(): + with pytest.raises( + ValueError, match="no Flavor matched with product_type 'invalid'" + ): + Flavor.from_product_type("invalid")