Skip to content

Commit

Permalink
test: move unit tests from robotoff to openfoodfacts-python (#309)
Browse files Browse the repository at this point in the history
  • Loading branch information
raphael0202 authored Dec 12, 2024
1 parent 9880ed9 commit 75001ae
Showing 1 changed file with 92 additions and 0 deletions.
92 changes: 92 additions & 0 deletions tests/unit/test_taxonomy.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@
TaxonomyNode,
create_brand_taxonomy_mapping,
create_taxonomy_mapping,
get_taxonomy,
map_to_canonical_id,
)

label_taxonomy = get_taxonomy("label")
category_taxonomy = get_taxonomy("category")


def test_map_to_canonical_id():
taxonomy_mapping = {
Expand Down Expand Up @@ -140,3 +144,91 @@ def test_create_brand_taxonomy_mapping(self):
"arrighi": "Arrighi",
"voiles-au-vent": "Voiles au Vent",
}


class TestTaxonomy:
@pytest.mark.parametrize(
"taxonomy,item,candidates,output",
[
(label_taxonomy, "en:organic", {"en:fr-bio-01"}, True),
(label_taxonomy, "en:fr-bio-01", {"en:organic"}, False),
(label_taxonomy, "en:fr-bio-01", [], False),
(label_taxonomy, "en:organic", {"en:gluten-free"}, False),
(
label_taxonomy,
"en:organic",
{"en:gluten-free", "en:no-additives", "en:vegan"},
False,
),
(
label_taxonomy,
"en:organic",
{"en:gluten-free", "en:no-additives", "en:fr-bio-16"},
True,
),
],
)
def test_is_child_of_any(
self, taxonomy: Taxonomy, item: str, candidates: list, output: bool
):
assert taxonomy.is_parent_of_any(item, candidates) is output

def test_is_child_of_any_unknwon_item(self):
with pytest.raises(ValueError):
label_taxonomy.is_parent_of_any("unknown-id", set())

@pytest.mark.parametrize(
"taxonomy,item,output",
[
(category_taxonomy, "en:plant-based-foods-and-beverages", set()),
(
category_taxonomy,
"en:plant-based-foods",
{"en:plant-based-foods-and-beverages"},
),
(
category_taxonomy,
"en:brown-rices",
{
"en:rices",
"en:cereal-grains",
"en:cereals-and-their-products",
"en:cereals-and-potatoes",
"en:plant-based-foods",
"en:plant-based-foods-and-beverages",
"en:seeds",
},
),
],
)
def test_get_parents_hierarchy(
self, taxonomy: Taxonomy, item: str, output: set[str]
):
node = taxonomy[item]
parents = node.get_parents_hierarchy()
assert set((x.id for x in parents)) == output

@pytest.mark.parametrize(
"taxonomy,items,output",
[
(category_taxonomy, [], []),
(category_taxonomy, ["en:brown-rices"], ["en:brown-rices"]),
(category_taxonomy, ["en:brown-rices", "en:rices"], ["en:brown-rices"]),
(
category_taxonomy,
["en:brown-rices", "en:rices", "en:cereal-grains"],
["en:brown-rices"],
),
(
category_taxonomy,
["en:brown-rices", "en:teas", "en:cereal-grains"],
["en:brown-rices", "en:teas"],
),
],
)
def test_find_deepest_nodes(
self, taxonomy: Taxonomy, items: list[str], output: list[str]
):
item_nodes = [taxonomy[item] for item in items]
output_nodes = [taxonomy[o] for o in output]
assert taxonomy.find_deepest_nodes(item_nodes) == output_nodes

0 comments on commit 75001ae

Please sign in to comment.