Spaces:
Running
Running
Download tests/test_compact.py from codelion/mlx-model-explorer: direct link, hf CLI and curl.
- Browser
- Download file 2.22 kB
-
https://huggingface.co/spaces/codelion/mlx-model-explorer/resolve/main/tests/test_compact.py
- Command line
-
hf download hf://spaces/codelion/mlx-model-explorer/tests/test_compact.py
-
curl -L -o test_compact.py https://huggingface.co/spaces/codelion/mlx-model-explorer/resolve/main/tests/test_compact.py
2.22 kB
| import importlib.util | |
| from pathlib import Path | |
| from app.events import COLUMNS | |
| spec = importlib.util.spec_from_file_location("compact", Path(__file__).resolve().parent.parent / "scripts" / "compact.py") | |
| compact = importlib.util.module_from_spec(spec) | |
| spec.loader.exec_module(compact) | |
| def test_plan_selects_only_that_month(): | |
| files = [ | |
| "README.md", | |
| "data/incoming/2026/09/01/a-1.parquet", "data/incoming/2026/09/30/b-2.parquet", | |
| "data/incoming/2026/10/01/c-3.parquet", "data/events/2026-08.parquet", "data/events/2026-09.parquet", | |
| ] | |
| incoming, existing, target = compact.plan(files, "2026-09") | |
| assert incoming == ["data/incoming/2026/09/01/a-1.parquet", "data/incoming/2026/09/30/b-2.parquet"] | |
| assert existing == "data/events/2026-09.parquet" and target == "data/events/2026-09.parquet" | |
| assert compact.plan(files, "2026-11") == ([], None, "data/events/2026-11.parquet") | |
| def test_compact_rows_merges_aligns_and_dedupes(): | |
| old_monthly = [{"event_type": "session", "session_id": "a" * 16, "timestamp": "2026-09-02T00:00:00Z", | |
| "distinct_queries": 1}] | |
| shard1 = [{"event_type": "session", "session_id": "a" * 16, "timestamp": "2026-09-02T00:10:00Z", | |
| "distinct_queries": 3}, | |
| {"event_type": "mlx_benchmark", "selected_model": "mlx-community/x-4bit", "timestamp": "2026-09-02T00:11:00Z"}] | |
| shard2 = [{"event_type": "session", "session_id": "b" * 16, "timestamp": "2026-09-03T00:00:00Z"}] | |
| rows = compact.compact_rows([old_monthly, shard1, shard2]) | |
| assert len(rows) == 3 and all(set(r) == set(COLUMNS) for r in rows) | |
| assert [r["distinct_queries"] for r in rows if r["session_id"] == "a" * 16] == [3] | |
| assert [r["timestamp"] for r in rows] == sorted(r["timestamp"] for r in rows) | |
| def test_previous_month_and_refuses_current(): | |
| from datetime import datetime, timezone | |
| assert compact.previous_month(datetime(2026, 1, 15, tzinfo=timezone.utc)) == "2025-12" | |
| assert compact.previous_month(datetime(2026, 9, 14, tzinfo=timezone.utc)) == "2026-08" | |
| import pytest | |
| now = datetime.now(timezone.utc).strftime("%Y-%m") | |
| with pytest.raises(SystemExit): | |
| compact.main(["--month", now]) | |