feat: Update activity session metrics handling to skip source field inserts
- Modified the `replace_activity_session_metrics` function to skip EAV inserts for parameters with a defined `source_field`, aligning with the existing logic in `upsert_session_metrics_from_csv_mapped`. - Enhanced the frontend logic to filter out metrics associated with `source_field` during payload construction, improving data integrity and user experience. - Added unit tests to validate the new behavior, ensuring that metrics with `source_field` are correctly excluded from inserts.
This commit is contained in:
1 parent
fd7a2dac6d
commit
94bb4a8199
3 files changed
+104
-2
No files matched your search
@@ -10,6 +10,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,
|
||||
@@ -377,3 +378,58 @@ def test_upsert_csv_writes_eav_when_no_source_field(mock_schema):
|
||||
1,
|
||||
)
|
||||
assert cur.asm_inserts == 1
|
||||
|
||||
|
||||
@patch("data_layer.activity_session_metrics.fetch_activity_session_metrics", return_value=[])
|
||||
@patch("data_layer.activity_session_metrics.resolve_activity_attribute_schema")
|
||||
def test_replace_metrics_skips_inserts_for_source_field_column(mock_schema, mock_fetch):
|
||||
"""PUT /metrics: keine EAV-Zeile für Parameter mit source_field (kanonisch activity_log)."""
|
||||
mock_schema.return_value = [
|
||||
{
|
||||
"key": "avg_hr",
|
||||
"training_parameter_id": 1,
|
||||
"data_type": "integer",
|
||||
"validation_rules": {},
|
||||
"source_field": "hr_avg",
|
||||
"required": False,
|
||||
},
|
||||
{
|
||||
"key": "custom_x",
|
||||
"training_parameter_id": 2,
|
||||
"data_type": "string",
|
||||
"validation_rules": {},
|
||||
"source_field": None,
|
||||
"required": False,
|
||||
},
|
||||
]
|
||||
|
||||
class Cur:
|
||||
def __init__(self):
|
||||
self.asm_inserts = 0
|
||||
|
||||
def execute(self, sql, params=None):
|
||||
self._sql = sql
|
||||
if "INSERT INTO activity_session_metrics" in sql:
|
||||
self.asm_inserts += 1
|
||||
|
||||
def fetchone(self):
|
||||
if "FROM activity_log" in getattr(self, "_sql", ""):
|
||||
return {
|
||||
"id": "00000000-0000-0000-0000-000000000002",
|
||||
"profile_id": "00000000-0000-0000-0000-000000000001",
|
||||
"training_category": "cardio",
|
||||
"training_type_id": 1,
|
||||
}
|
||||
return None
|
||||
|
||||
cur = Cur()
|
||||
replace_activity_session_metrics(
|
||||
cur,
|
||||
"00000000-0000-0000-0000-000000000001",
|
||||
"00000000-0000-0000-0000-000000000002",
|
||||
[
|
||||
{"parameter_key": "avg_hr", "value": 120},
|
||||
{"parameter_key": "custom_x", "value": "y"},
|
||||
],
|
||||
)
|
||||
assert cur.asm_inserts == 1
|
||||
Reference in new issue
Block a user