fixing stuff after splitting jsons

This commit is contained in:
Davide Scaini
2026-04-09 15:27:00 +02:00
parent 8118f6f316
commit 084c652fdd
6 changed files with 315 additions and 17 deletions
+85 -1
View File
@@ -6,7 +6,7 @@ from pathlib import Path
import pytest
from bincio.render.merge import apply_sidecar, merge_all, parse_sidecar
from bincio.render.merge import apply_sidecar, merge_all, merge_one, parse_sidecar
# ── parse_sidecar ─────────────────────────────────────────────────────────────
@@ -204,3 +204,87 @@ def test_merge_all_idempotent(data_dir):
(data_dir / "_merged" / "activities" / "2024-01-01T080000Z-morning-ride.json").read_text()
)
assert data["title"] == "Renamed"
# ── timeseries file handling ──────────────────────────────────────────────────
@pytest.fixture()
def data_dir_with_timeseries(tmp_path):
"""data_dir fixture extended with .timeseries.json sidecar files."""
acts = tmp_path / "activities"
acts.mkdir()
ACT_ID = "2024-01-01T080000Z-morning-ride"
detail = {
"id": ACT_ID, "title": "Morning Ride", "sport": "cycling",
"started_at": "2024-01-01T08:00:00Z",
"description": "", "privacy": "public", "custom": {},
"timeseries_url": f"activities/{ACT_ID}.timeseries.json",
}
ts_data = {"t": [0, 1], "lat": [45.0, 45.1], "lon": [7.0, 7.1],
"elevation_m": [300.0, 301.0], "speed_kmh": [None, None],
"hr_bpm": [None, None], "cadence_rpm": [None, None],
"power_w": [None, None], "temperature_c": [None, None]}
(acts / f"{ACT_ID}.json").write_text(json.dumps(detail))
(acts / f"{ACT_ID}.timeseries.json").write_text(json.dumps(ts_data))
index = {"activities": [
{"id": ACT_ID, "title": "Morning Ride", "sport": "cycling",
"started_at": "2024-01-01T08:00:00Z", "privacy": "public", "custom": {}},
]}
(tmp_path / "index.json").write_text(json.dumps(index))
return tmp_path, ACT_ID
def test_merge_all_symlinks_timeseries(data_dir_with_timeseries):
"""merge_all should symlink .timeseries.json alongside the detail JSON."""
data_dir, act_id = data_dir_with_timeseries
merge_all(data_dir)
ts_dest = data_dir / "_merged" / "activities" / f"{act_id}.timeseries.json"
assert ts_dest.exists(), "timeseries file not present in _merged"
assert ts_dest.is_symlink(), "timeseries file should be a symlink (no merge needed)"
# Points to the original
src = data_dir / "activities" / f"{act_id}.timeseries.json"
assert ts_dest.resolve() == src.resolve()
def test_merge_all_timeseries_survives_sidecar(data_dir_with_timeseries):
"""When a sidecar is applied (detail JSON is rewritten), the timeseries
symlink should still be created alongside it."""
data_dir, act_id = data_dir_with_timeseries
edits = data_dir / "edits"
edits.mkdir()
(edits / f"{act_id}.md").write_text("---\ntitle: Renamed\n---\n")
merge_all(data_dir)
detail_dest = data_dir / "_merged" / "activities" / f"{act_id}.json"
ts_dest = data_dir / "_merged" / "activities" / f"{act_id}.timeseries.json"
assert not detail_dest.is_symlink(), "sidecar detail should be a copy, not symlink"
assert ts_dest.exists(), "timeseries should still be present after sidecar merge"
assert ts_dest.is_symlink(), "timeseries should remain a symlink"
def test_merge_one_symlinks_timeseries(data_dir_with_timeseries):
"""merge_one should symlink the .timeseries.json file for the given activity."""
data_dir, act_id = data_dir_with_timeseries
merged_acts = data_dir / "_merged" / "activities"
merged_acts.mkdir(parents=True)
merge_one(data_dir, act_id)
ts_dest = merged_acts / f"{act_id}.timeseries.json"
assert ts_dest.exists()
assert ts_dest.is_symlink()
def test_merge_all_idempotent_with_timeseries(data_dir_with_timeseries):
"""Running merge_all twice should not break timeseries symlinks."""
data_dir, act_id = data_dir_with_timeseries
merge_all(data_dir)
merge_all(data_dir)
ts_dest = data_dir / "_merged" / "activities" / f"{act_id}.timeseries.json"
assert ts_dest.exists()
assert ts_dest.is_symlink()