Files
seismo-relay/tests/test_import_mlg_dir.py
T
serversdownandClaude Opus 5.5 4bc669df4d feat(mlg): bulk import, and a cross-check against the event files
scripts/import_mlg_dir.py imports every .MLG under a directory (files
with records first, then the empty ones, so each unit's serial is known
first; dry run writes nothing).  scratch/check_mlg_vs_events.py matches
each Event Recorded record to an event file's footer (stop, flag word,
trigger source) in both directions, inside each unit's .MLG period.

Run on the dev box against DL2's 1,273 sample files (2026-11-13):
  import: 1273 ok, 0 errors, 231 empty, 0 unresolved, 2180 records, 13 s
  BE8081:  785 matched, 0 record-only, 0 file-only
  BE6907:    6 matched, 5 record-only, 0 file-only
  BE12844:  19 matched, 37 record-only, 0 file-only
  every record-only one stops after 2026-08-25, where the DL2 copy ends;
  the 4,303 footers of those three units' event files all pass their CRC.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01ALX2R9LdWawLzUxwewZGA1
2026-11-13 13:54:50 -05:00

58 lines
2.2 KiB
Python

"""
Tests for scripts/import_mlg_dir.py — bulk import of a directory of .MLGs (spec §7).
"""
from __future__ import annotations
import os
import shutil
import sys
from pathlib import Path
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from scripts.import_mlg_dir import main
from sfm.database import SeismoDb
FIX = Path(__file__).parent / "fixtures" / "mlg-11-9-26"
def _counts(text: str) -> dict:
return {k: int(v) for k, v in (line.split() for line in text.strip().splitlines())}
def test_import_dir_counts(tmp_path, capsys):
src = tmp_path / "in" / "nested"
shutil.copytree(FIX, src)
db_path, store = tmp_path / "t.db", tmp_path / "store"
args = [str(tmp_path / "in"), "--db-path", str(db_path), "--store-root", str(store)]
assert main(args + ["--dry-run"]) == 0
dry = _counts(capsys.readouterr().out)
assert (dry["files"], dry["error"], dry["empty"]) == (12, 0, 1)
assert not db_path.exists() and not store.exists()
assert main(args) == 0
first = _counts(capsys.readouterr().out)
assert (first["files"], first["ok"], first["error"], first["empty"], first["unresolved"]) == (12, 12, 0, 1, 0)
assert first["skipped"] == 3 # BE115292.MLG repeats BE11529.MLG's records
assert [f["file_name"] for f in SeismoDb(db_path).query_mlg_files("BE6907")] == [
"BE6907750.MLG", "BE6907751.MLG"]
assert main(args) == 0
again = _counts(capsys.readouterr().out)
assert (again["duplicate"], again["ok"], again["inserted"]) == (12, 0, 0)
def test_empty_files_are_resolved_whatever_the_folder_order(tmp_path, capsys):
"""The empty file sorts first by name and by time here; the two-pass order still
gives it its unit."""
d = tmp_path / "in"
d.mkdir()
shutil.copy2(FIX / "BE6907751.MLG", d / "BE6907751.MLG")
shutil.copy2(FIX / "BE6907750.MLG", d / "BE6907750.MLG")
os.utime(d / "BE6907751.MLG", (1_700_000_000, 1_700_000_000)) # older than its real file
os.utime(d / "BE6907750.MLG", (1_800_000_000, 1_800_000_000))
assert main([str(d), "--db-path", str(tmp_path / "t.db"), "--store-root", str(tmp_path / "s")]) == 0
assert _counts(capsys.readouterr().out)["unresolved"] == 0