feat(csv-import): Enhance CSV import processing and validation
- Updated the CSV import logic to support new row processing specifications for weight and vitals baseline, allowing for better data aggregation and validation. - Implemented handling for multiple rows on the same day, enabling aggregation of values such as averages for vitals and last values for weight. - Enhanced test coverage for the new import functionalities, ensuring correct behavior during data processing and validation. - Refactored the module registry to include default import row processing options for better flexibility in handling CSV data.
This commit is contained in:
1 parent
c0fcdea1fe
commit
e35d167055
5 files changed
+165
-9
No files matched your search
@@ -130,6 +130,45 @@ def test_run_universal_import_vitals_baseline_upsert_insert_path():
|
||||
assert any("INSERT INTO vitals_baseline" in q[0] for q in cur.executes)
|
||||
|
||||
|
||||
def test_run_universal_import_vitals_baseline_two_rows_same_day_averages():
|
||||
text = (
|
||||
"Start,Resting Heart Rate,Heart Rate Variability,VO2 Max\n"
|
||||
"2024-01-15 07:00:00,50,40,42.0\n"
|
||||
"2024-01-15 18:00:00,60,50,43.0\n"
|
||||
)
|
||||
mapping = {
|
||||
"delimiter": ",",
|
||||
"has_header": True,
|
||||
"field_mappings": {
|
||||
"Start": "date",
|
||||
"Resting Heart Rate": "resting_hr",
|
||||
"Heart Rate Variability": "hrv",
|
||||
"VO2 Max": "vo2_max",
|
||||
},
|
||||
"type_conversions": {
|
||||
"date": {
|
||||
"type": "datetime",
|
||||
"format": "yyyy-mm-dd HH:MM:SS",
|
||||
"extract": "date_only",
|
||||
"flexible": True,
|
||||
},
|
||||
"resting_hr": {"type": "int", "flexible": True},
|
||||
"hrv": {"type": "int", "flexible": True},
|
||||
"vo2_max": {"type": "float", "decimal_separator": ".", "flexible": True},
|
||||
},
|
||||
}
|
||||
cur = _SeqCursor([{"inserted": True, "id": "bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb"}])
|
||||
out = run_universal_csv_import(cur, PID, "vitals_baseline", text, "v2.csv", mapping)
|
||||
assert out["rows_total"] == 2
|
||||
assert out["rows_imported"] == 1
|
||||
upsert = [q for q in cur.executes if "INSERT INTO vitals_baseline" in q[0]]
|
||||
assert len(upsert) == 1
|
||||
params = upsert[0][1]
|
||||
assert params[2] == 55
|
||||
assert params[3] == 45
|
||||
assert params[4] == 42.5
|
||||
|
||||
|
||||
def test_run_universal_import_activity_garmin_time_plus_date_columns(monkeypatch):
|
||||
"""Datum in eigener Spalte, Uhrzeit wie bei Garmin nur als Uhrzeit."""
|
||||
monkeypatch.setattr(
|
||||
@@ -215,3 +254,25 @@ def test_run_universal_import_nutrition_two_rows_same_day_aggregates_to_one_row(
|
||||
assert params[4] == 15.0
|
||||
assert params[5] == 30.0
|
||||
assert params[6] == 45.0
|
||||
|
||||
|
||||
def test_run_universal_import_weight_two_rows_same_day_last_value():
|
||||
"""Mehrere Gewichtszeilen pro Tag → Standard: letzter Wert in der Datei."""
|
||||
text = "Date,Weight\n2024-01-15,85.0\n2024-01-15,83.5\n"
|
||||
mapping = {
|
||||
"delimiter": ",",
|
||||
"has_header": True,
|
||||
"field_mappings": {"Date": "date", "Weight": "weight"},
|
||||
"type_conversions": {
|
||||
"date": {"type": "date", "format": "yyyy-mm-dd", "flexible": True},
|
||||
"weight": {"type": "float", "decimal_separator": ".", "flexible": True},
|
||||
},
|
||||
}
|
||||
cur = _SeqCursor([None])
|
||||
out = run_universal_csv_import(cur, PID, "weight", text, "w.csv", mapping)
|
||||
assert out["rows_total"] == 2
|
||||
assert out["rows_imported"] == 1
|
||||
insert_sqls = [q for q in cur.executes if "INSERT INTO weight_log" in q[0]]
|
||||
assert len(insert_sqls) == 1
|
||||
params = insert_sqls[0][1]
|
||||
assert params[3] == 83.5
|
||||
Reference in new issue
Block a user