feat: Enhance CSV header normalization and mapping for activity data
- Introduced a new utility function `canonical_csv_header_label` to standardize CSV header labels, improving consistency in field mapping. - Updated the `_lookup_db_field` function to support prefix matching for longer manual keys, enhancing the accuracy of field resolution. - Added tests to validate handling of non-breaking space characters in CSV headers and ensure correct mapping to normalized keys, improving robustness of CSV parsing.
This commit is contained in:
1 parent
c570e67a09
commit
e4e8c70cd2
3 files changed
+82
-11
No files matched your search
@@ -38,6 +38,29 @@ def test_parse_csv_sample_header():
|
||||
assert rows[0]["kcal"] == "2000"
|
||||
|
||||
|
||||
def test_parse_csv_sample_nbsp_in_header_matches_normal_space_key():
|
||||
"""Excel/Apple: NBSP (U+00A0) im Spaltennamen — gleicher Key wie normales Leerzeichen."""
|
||||
text = "Aufgestiegene\u00a0Höhe (m);Wert\n12;3\n"
|
||||
headers, rows, delim = parse_csv_sample(text, delimiter=";", max_data_rows=3)
|
||||
assert headers == ["Aufgestiegene Höhe (m)", "Wert"]
|
||||
assert rows[0]["Aufgestiegene Höhe (m)"] == "12"
|
||||
|
||||
|
||||
def test_iter_csv_dict_rows_nbsp_header_canonical():
|
||||
text = "col\u00a0one;b\n1;2\n"
|
||||
rows = list(iter_csv_dict_rows(text, ";", has_header=True))
|
||||
assert rows == [{"col one": "1", "b": "2"}]
|
||||
|
||||
|
||||
def test_build_row_field_mapping_space_vs_nbsp_in_csv_header():
|
||||
"""Vorlage (Dialog) mit normalem Leerzeichen, CSV mit NBSP — Zuordnung muss greifen."""
|
||||
csv_row = {"Aufgestiegene\u00a0Höhe (m)": "10"}
|
||||
fm = {"Aufgestiegene Höhe (m)": "stola"}
|
||||
tc = {"stola": {"type": "float", "decimal_separator": ".", "flexible": True}}
|
||||
out = build_row_after_mapping(csv_row, fm, tc, module="activity")
|
||||
assert out.get("stola") == 10.0
|
||||
|
||||
|
||||
def test_column_signature_sorted_unique():
|
||||
sig = column_signature(["B", "a", "a"])
|
||||
assert sig == ["a", "b"]
|
||||
@@ -183,6 +206,16 @@ def test_build_row_fddb_raw_header_keys_match_normalized_template():
|
||||
assert out["kcal"] is not None and abs(float(out["kcal"]) - (42000 / 4.184)) < 0.1
|
||||
|
||||
|
||||
def test_build_row_apple_workout_elevation_header_prefix_matches_shorter_mapping_key():
|
||||
"""Apple Workouts: „Aufgestiegene Höhe (m)“ normalisiert anders als manuell „aufgestiegene Höhe“."""
|
||||
csv_row = {"Aufgestiegene Höhe (m)": "47.13", "Workout Type": "Outdoor Spaziergang"}
|
||||
fm = {"aufgestiegene Höhe": "stola", "Workout Type": "activity_type"}
|
||||
tc = {"stola": {"type": "string"}}
|
||||
out = build_row_after_mapping(csv_row, fm, tc, module="activity")
|
||||
assert out.get("stola") == "47.13"
|
||||
assert out.get("activity_type") == "Outdoor Spaziergang"
|
||||
|
||||
|
||||
def test_convert_date_ddmm_with_seconds():
|
||||
d = convert_value(
|
||||
"15.01.2024 14:30:00",
|
||||
|
||||
Reference in new issue
Block a user