feat: Refactor activity data handling and improve CSV import logic
- Updated `ACTIVITY_PRODUCTION_ARCHITECTURE_AND_PHASES.md` to clarify the derivation of `ACTIVITY_MODULE_REGISTRY_FIELD_KEYS` from `csv_parser.module_registry`. - Enhanced `activity_data_canon.py` to eliminate hardcoded key lists, ensuring all registry fields are derived dynamically. - Refactored the `_import_activity` function to remove redundant parameters and streamline the import process. - Improved the `insert_activity_csv_minimal` function to handle metrics exclusively through `update_activity_columns`, preventing hardcoded values. - Updated frontend components to manage editable activity log fields more effectively, ensuring proper handling of metrics during CSV imports. - Added unit tests to validate the new logic and ensure consistency in activity session metrics handling.
This commit is contained in:
1 parent
cd29c7d433
commit
5cda485458
9 files changed
+191
-135
No files matched your search
@@ -11,6 +11,7 @@ from data_layer.activity_session_metrics import (
|
||||
enrich_sessions_with_metrics,
|
||||
merge_column_backed_and_eav_metrics,
|
||||
merge_parameter_schema_rows,
|
||||
replace_activity_session_metrics,
|
||||
resolve_activity_attribute_schema,
|
||||
upsert_session_metrics_from_csv_mapped,
|
||||
_row_value_tuple,
|
||||
@@ -358,6 +359,61 @@ def test_upsert_csv_skips_parameter_with_source_field(mock_schema):
|
||||
assert cur.asm_inserts == 0
|
||||
|
||||
|
||||
@patch("data_layer.activity_session_metrics.fetch_activity_session_metrics")
|
||||
@patch("data_layer.activity_session_metrics.resolve_activity_attribute_schema")
|
||||
def test_replace_metrics_skips_column_backed_kcal(mock_schema, mock_fetch):
|
||||
"""PUT /metrics: keine EAV-Zeile für kcal_active (liegt in activity_log)."""
|
||||
pid = str(uuid.uuid4())
|
||||
eid = str(uuid.uuid4())
|
||||
mock_schema.return_value = [
|
||||
{
|
||||
"training_parameter_id": 1,
|
||||
"key": "kcal_active",
|
||||
"data_type": "float",
|
||||
"validation_rules": {},
|
||||
"source_field": "kcal_active",
|
||||
"required": False,
|
||||
},
|
||||
{
|
||||
"training_parameter_id": 2,
|
||||
"key": "custom_reps",
|
||||
"data_type": "integer",
|
||||
"validation_rules": {"min": 0},
|
||||
"source_field": None,
|
||||
"required": False,
|
||||
},
|
||||
]
|
||||
mock_fetch.return_value = []
|
||||
|
||||
class Cur:
|
||||
def __init__(self):
|
||||
self.asm_inserts = 0
|
||||
|
||||
def execute(self, sql, params=None):
|
||||
if "INSERT INTO activity_session_metrics" in sql:
|
||||
self.asm_inserts += 1
|
||||
|
||||
def fetchone(self):
|
||||
return {
|
||||
"profile_id": pid,
|
||||
"training_category": "strength",
|
||||
"training_type_id": 1,
|
||||
}
|
||||
|
||||
cur = Cur()
|
||||
replace_activity_session_metrics(
|
||||
cur,
|
||||
pid,
|
||||
eid,
|
||||
[
|
||||
{"parameter_key": "kcal_active", "value": 450.0},
|
||||
{"parameter_key": "custom_reps", "value": 12},
|
||||
],
|
||||
)
|
||||
assert cur.asm_inserts == 1
|
||||
mock_fetch.assert_called_once_with(cur, eid)
|
||||
|
||||
|
||||
def test_merge_eav_primary_falls_back_to_legacy_hr_min_column():
|
||||
"""Kanon: min_hr ohne source_field / ohne EAV — Lesefallback Spalte hr_min."""
|
||||
schema = [
|
||||
@@ -374,3 +430,18 @@ def test_merge_eav_primary_falls_back_to_legacy_hr_min_column():
|
||||
assert len(out) == 1
|
||||
assert out[0]["key"] == "min_hr"
|
||||
assert out[0]["value"] == 88
|
||||
|
||||
|
||||
def test_activity_module_registry_field_keys_match_csv_module_definition():
|
||||
"""Phase A: Kanon-Spine = module_registry „activity“.fields (keine zweite Liste)."""
|
||||
from csv_parser.module_registry import get_module_definition
|
||||
from data_layer.activity_data_canon import (
|
||||
ACTIVITY_MODULE_REGISTRY_FIELD_KEYS,
|
||||
get_activity_module_registry_field_keys,
|
||||
)
|
||||
|
||||
mod = get_module_definition("activity")
|
||||
assert mod is not None
|
||||
expected = frozenset((mod.get("fields") or {}).keys())
|
||||
assert get_activity_module_registry_field_keys() == expected
|
||||
assert ACTIVITY_MODULE_REGISTRY_FIELD_KEYS == expected
|
||||
Reference in new issue
Block a user