refactor(csv-import): Enhance nutrition data processing and template rendering
Deploy Development / deploy (push) Successful in 48s
Build Test / pytest-backend (push) Successful in 3s
Build Test / lint-backend (push) Successful in 0s
Build Test / build-frontend (push) Successful in 16s

- Updated the nutrition import logic to utilize a new row processing specification, improving data aggregation and validation.
- Refactored the template rendering process in the workflow executor to use Jinja2's Environment with ChainableUndefined for better handling of missing attributes.
- Added backward-compatible shortcuts for accessing decision signals in node contexts, enhancing flexibility in template usage.
- Introduced import row processing options in CSV templates, allowing for more customizable data handling during imports.
This commit is contained in:
Lars committed 2026-04-10 11:56:43 +02:00
1 parent 8b67f7ab55
commit c0fcdea1fe
9 files changed
+357 -22

No files matched your search

+39
View File
@@ -176,3 +176,42 @@ def test_run_universal_import_activity_garmin_time_plus_date_columns(monkeypatch
for _sql, params in cur.executes
if params
)
def test_run_universal_import_nutrition_two_rows_same_day_aggregates_to_one_row():
"""Modul-Default: mehrere CSV-Zeilen pro Tag → Summe, ein nutrition_log-Eintrag."""
text = (
"Date,Kalorien,Protein,Fett,KH\n"
"2024-01-15,500,10,20,30\n"
"2024-01-15,300,5,10,15\n"
)
mapping = {
"delimiter": ",",
"has_header": True,
"field_mappings": {
"Date": "date",
"Kalorien": "kcal",
"Protein": "protein_g",
"Fett": "fat_g",
"KH": "carbs_g",
},
"type_conversions": {
"date": {"type": "date", "format": "yyyy-mm-dd", "flexible": True},
"kcal": {"type": "float", "decimal_separator": ".", "flexible": True},
"protein_g": {"type": "float", "decimal_separator": ".", "flexible": True},
"fat_g": {"type": "float", "decimal_separator": ".", "flexible": True},
"carbs_g": {"type": "float", "decimal_separator": ".", "flexible": True},
},
}
cur = _SeqCursor([None])
out = run_universal_csv_import(cur, PID, "nutrition", text, "n.csv", mapping)
assert out["rows_total"] == 2
assert out["rows_imported"] == 1
insert_sqls = [q for q in cur.executes if "INSERT INTO nutrition_log" in q[0]]
assert len(insert_sqls) == 1
params = insert_sqls[0][1]
# (eid, profile_id, iso, kcal, prot, fat, carbs)
assert params[3] == 800.0
assert params[4] == 15.0
assert params[5] == 30.0
assert params[6] == 45.0
@@ -0,0 +1,67 @@
"""Tests für CSV-Zeilenaggregation (import_row_processing)."""
from __future__ import annotations
import datetime as dt
import pytest
from csv_parser.import_row_processing import (
aggregate_mapped_rows,
resolve_import_row_processing,
validate_import_row_processing,
)
def test_validate_rejects_unknown_aggregate():
with pytest.raises(ValueError, match="ungültige Operation"):
validate_import_row_processing(
"nutrition",
{"group_by": ["date"], "aggregates": {"kcal": "bogus"}},
{"Kal": "date", "E": "kcal"},
)
def test_validate_group_by_must_be_mapped():
with pytest.raises(ValueError, match="keiner CSV-Spalte zugeordnet"):
validate_import_row_processing(
"nutrition",
{"group_by": ["date"], "aggregates": {"kcal": "sum"}},
{"Kal": "kcal"}, # date nicht gemappt
)
def test_aggregate_mapped_rows_sums_same_group():
d = dt.date(2024, 1, 15)
rows = [
{"date": d, "kcal": 500.0, "protein_g": 20},
{"date": d, "kcal": 300.0, "protein_g": 15},
]
spec = {"group_by": ["date"], "aggregates": {"kcal": "sum", "protein_g": "sum"}}
out = aggregate_mapped_rows(rows, spec)
assert len(out) == 1
assert out[0]["kcal"] == 800.0
assert out[0]["protein_g"] == 35
def test_resolve_explicit_overrides_default():
m = {
"import_row_processing": {"group_by": ["date"], "aggregates": {"kcal": "mean"}},
}
spec = resolve_import_row_processing("nutrition", m)
assert spec is not None
assert spec["aggregates"]["kcal"] == "mean"
def test_resolve_empty_dict_falls_back_to_module_default():
m: dict = {"import_row_processing": {}}
spec = resolve_import_row_processing("nutrition", m)
assert spec is not None
assert spec["group_by"] == ["date"]
assert spec["aggregates"]["kcal"] == "sum"
def test_resolve_none_uses_nutrition_default():
spec = resolve_import_row_processing("nutrition", {})
assert spec is not None
assert "date" in (spec.get("group_by") or [])