feat: Add CSV import support for additional training parameters
- Introduced `resolve_activity_attribute_schema_for_csv_import` to enhance the handling of training parameters during CSV imports, allowing for the inclusion of active parameters not present in the category/type profile. - Updated `apply_activity_mapped_column_aliases` and `upsert_session_metrics_from_csv_mapped` to utilize the new CSV import function, ensuring comprehensive mapping and insertion of metrics. - Added unit tests to validate the new functionality and ensure correct behavior when handling mapped training parameters during CSV imports.
This commit is contained in:
1 parent
7d6fdab812
commit
026c51b6b5
2 files changed
+152
-2
No files matched your search
@@ -13,6 +13,7 @@ from data_layer.activity_session_metrics import (
|
||||
merge_parameter_schema_rows,
|
||||
replace_activity_session_metrics,
|
||||
resolve_activity_attribute_schema,
|
||||
resolve_activity_attribute_schema_for_csv_import,
|
||||
upsert_session_metrics_from_csv_mapped,
|
||||
_row_value_tuple,
|
||||
_validate_single_value,
|
||||
@@ -323,6 +324,83 @@ def test_apply_mapped_aliases_does_not_overwrite_existing_column():
|
||||
assert out["hr_avg"] == 120
|
||||
|
||||
|
||||
@patch("data_layer.activity_session_metrics.resolve_activity_attribute_schema", return_value=[])
|
||||
def test_resolve_csv_import_adds_mapped_keys_from_training_parameters(mock_resolve):
|
||||
"""Ohne Kategorie/Typ-Profil: gemappte Keys aus training_parameters ergänzen."""
|
||||
|
||||
class Cur:
|
||||
def __init__(self):
|
||||
self.sql = ""
|
||||
|
||||
def execute(self, sql, params=None):
|
||||
self.sql = sql
|
||||
self.params = params
|
||||
|
||||
def fetchone(self):
|
||||
if "training_parameters" in self.sql:
|
||||
return {
|
||||
"id": 7,
|
||||
"key": "cadence",
|
||||
"name_de": "Kadenz",
|
||||
"name_en": "Cadence",
|
||||
"param_category": "physical",
|
||||
"data_type": "integer",
|
||||
"unit": "rpm",
|
||||
"validation_rules": {},
|
||||
"source_field": None,
|
||||
}
|
||||
return None
|
||||
|
||||
cur = Cur()
|
||||
out = resolve_activity_attribute_schema_for_csv_import(cur, None, None, {"cadence": 90})
|
||||
assert len(out) == 1
|
||||
assert out[0]["key"] == "cadence"
|
||||
assert out[0]["training_parameter_id"] == 7
|
||||
mock_resolve.assert_called_once_with(cur, None, None)
|
||||
|
||||
|
||||
@patch("data_layer.activity_session_metrics.resolve_activity_attribute_schema", return_value=[])
|
||||
def test_upsert_csv_inserts_eav_when_only_mapped_training_parameter(mock_resolve):
|
||||
class Cur:
|
||||
def __init__(self):
|
||||
self.asm_inserts = 0
|
||||
self.sql = ""
|
||||
|
||||
def execute(self, sql, params=None):
|
||||
self.sql = sql
|
||||
self.params = params
|
||||
if "INSERT INTO activity_session_metrics" in sql:
|
||||
self.asm_inserts += 1
|
||||
|
||||
def fetchone(self):
|
||||
if "activity_log" in self.sql:
|
||||
return {"profile_id": "00000000-0000-0000-0000-000000000001"}
|
||||
if "training_parameters" in self.sql:
|
||||
return {
|
||||
"id": 7,
|
||||
"key": "cadence",
|
||||
"name_de": "K",
|
||||
"name_en": "K",
|
||||
"param_category": "physical",
|
||||
"data_type": "integer",
|
||||
"unit": None,
|
||||
"validation_rules": {"min": 0, "max": 300},
|
||||
"source_field": None,
|
||||
}
|
||||
return None
|
||||
|
||||
cur = Cur()
|
||||
upsert_session_metrics_from_csv_mapped(
|
||||
cur,
|
||||
"00000000-0000-0000-0000-000000000001",
|
||||
"00000000-0000-0000-0000-000000000002",
|
||||
{"cadence": 90},
|
||||
None,
|
||||
None,
|
||||
)
|
||||
assert cur.asm_inserts == 1
|
||||
|
||||
|
||||
@patch("data_layer.activity_session_metrics.resolve_activity_attribute_schema")
|
||||
def test_upsert_csv_skips_parameter_with_source_field(mock_schema):
|
||||
"""Kein INSERT in activity_session_metrics für Spalten-Parameter (avg_hr → hr_avg)."""
|
||||
|
||||
Reference in new issue
Block a user