Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add Flavor.from_product_type method #308

Merged
merged 3 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions openfoodfacts/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 0 additions & 1 deletion tests/unit/test_taxonomy.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
create_taxonomy_mapping,
map_to_canonical_id,
)
from openfoodfacts.types import TaxonomyType


def test_map_to_canonical_id():
Expand Down
26 changes: 26 additions & 0 deletions tests/unit/test_types.py
Original file line number Diff line number Diff line change
@@ -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")
Loading