diff --git a/src/activity_parser/parser.py b/src/activity_parser/parser.py index 8651451..1165140 100644 --- a/src/activity_parser/parser.py +++ b/src/activity_parser/parser.py @@ -104,6 +104,8 @@ def infer_extension(file: str | PathLike[str]) -> str: ext = p.suffix if ext.lower() == ".gz": ext = Path(p.stem).suffix + if not ext: + ext = ".gz" return normalize_extension(ext) diff --git a/src/activity_parser/postprocess.py b/src/activity_parser/postprocess.py index da6bc07..fc84353 100644 --- a/src/activity_parser/postprocess.py +++ b/src/activity_parser/postprocess.py @@ -43,6 +43,7 @@ def index_by_time(records: pd.DataFrame, column: str) -> pd.DataFrame: if column not in records.columns: if not records.empty: return records.rename_axis(column) + records = records.copy() records[column] = pd.NaT records = records.set_index(column) diff --git a/tests/test_parser.py b/tests/test_parser.py index ae1e115..1835dad 100644 --- a/tests/test_parser.py +++ b/tests/test_parser.py @@ -34,6 +34,11 @@ def test_infer_extension(): assert infer_extension("dir/sub/ride.GPX.GZ") == "gpx" +def test_infer_extension_bare_gz_rejected(): + with pytest.raises(ValueError, match="Ambiguous"): + infer_extension("ride.gz") + + def test_select_and_reorder_cols_selects_and_orders(): df = pd.DataFrame({"b": [1], "a": [2], "ignored": [3]}) out = select_and_reorder_cols(df, ["a", "b", "missing"], include_all_columns=False) diff --git a/tests/test_postprocess.py b/tests/test_postprocess.py index 55191df..1fc790d 100644 --- a/tests/test_postprocess.py +++ b/tests/test_postprocess.py @@ -43,6 +43,12 @@ def test_index_by_time_missing_column_yields_empty_datetime_index(): assert isinstance(out.index, pd.DatetimeIndex) +def test_index_by_time_missing_column_and_empty_does_not_mutate_input(): + df = pd.DataFrame({"value": []}) + index_by_time(df, "time") + assert "time" not in df.columns + + def test_index_by_time_missing_column_with_rows_keeps_them(): df = pd.DataFrame({"value": [1, 2, 3]}) out = index_by_time(df, "time") diff --git a/tests/test_readme.py b/tests/test_readme.py index f4743d0..de6eea7 100644 --- a/tests/test_readme.py +++ b/tests/test_readme.py @@ -1,4 +1,4 @@ -"""Keeps README.md's tables in sync with output.py.""" +"""Keeps README.md's tables in sync with parser.py.""" import re from dataclasses import fields