2026-09-14 13:06:53 +02:00
|
|
|
from bls.attribute_labels import (
|
2026-09-14 13:09:44 +02:00
|
|
|
annotate_attribute,
|
2026-09-14 13:06:53 +02:00
|
|
|
apply_official_name,
|
|
|
|
|
fill_placeholder_labels,
|
|
|
|
|
label_is_placeholder,
|
|
|
|
|
official_label,
|
2026-09-14 13:09:44 +02:00
|
|
|
resolve_group,
|
|
|
|
|
sort_attributes,
|
2026-09-14 13:06:53 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_official_macro_labels():
|
|
|
|
|
assert official_label("ENERCC")["name_de"].startswith("Energie")
|
|
|
|
|
assert official_label("cho")["name_de"].startswith("Kohlenhydrate")
|
|
|
|
|
assert label_is_placeholder("ENERCC", "ENERCC")
|
|
|
|
|
assert label_is_placeholder("", "FAT")
|
|
|
|
|
assert not label_is_placeholder("Fett", "FAT")
|
|
|
|
|
assert apply_official_name("FAT", "FAT") == "Fett"
|
|
|
|
|
assert apply_official_name("FAT", "Fett gesamt") == "Fett gesamt"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_fill_placeholder_labels_skips_custom_names():
|
|
|
|
|
class _Cur:
|
|
|
|
|
def __init__(self):
|
|
|
|
|
self.rows = [
|
|
|
|
|
{"id": 1, "attr_key": "FAT", "name_de": "FAT", "unit": None, "category": None},
|
|
|
|
|
{"id": 2, "attr_key": "EPA", "name_de": "Mein EPA", "unit": "g", "category": None},
|
|
|
|
|
]
|
|
|
|
|
self.updated = []
|
|
|
|
|
|
|
|
|
|
def execute(self, sql, params=None):
|
|
|
|
|
self.last = (sql, params)
|
|
|
|
|
if "UPDATE" in sql:
|
|
|
|
|
self.updated.append(params)
|
|
|
|
|
|
|
|
|
|
def fetchall(self):
|
|
|
|
|
return self.rows
|
|
|
|
|
|
|
|
|
|
cur = _Cur()
|
|
|
|
|
stats = fill_placeholder_labels(cur)
|
2026-09-14 13:09:44 +02:00
|
|
|
assert stats["updated"] == 2
|
2026-09-14 13:06:53 +02:00
|
|
|
assert cur.updated[0][0] == "Fett"
|
|
|
|
|
assert cur.updated[0][3] == 1
|
2026-09-14 13:09:44 +02:00
|
|
|
assert cur.updated[1][0] == "Mein EPA"
|
|
|
|
|
assert cur.updated[1][2] == "fatty_acids"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_group_and_sort_macros_first():
|
|
|
|
|
assert resolve_group("ENERCC")[0] == "energy"
|
|
|
|
|
assert resolve_group("NA", None, None)[1] == "Mineralstoffe"
|
|
|
|
|
assert resolve_group("XYZ", None, "extension")[0] == "extension"
|
|
|
|
|
rows = sort_attributes([
|
|
|
|
|
annotate_attribute({"attr_key": "NA", "name_de": "Natrium", "sort_order": 2, "origin": "official_bls"}),
|
|
|
|
|
annotate_attribute({"attr_key": "FAT", "name_de": "Fett", "sort_order": 9, "origin": "official_bls"}),
|
|
|
|
|
annotate_attribute({"attr_key": "ENERCC", "name_de": "Energie", "sort_order": 1, "origin": "official_bls"}),
|
|
|
|
|
])
|
|
|
|
|
assert [r["attr_key"] for r in rows] == ["ENERCC", "FAT", "NA"]
|