Two new tables, created on first start (no migration): mlg_files (one row per file received, deduped by name + sha256) and mlg_records (one row per log record, unique on serial, start, stop, flag word and trigger source; COALESCE on the stop, since an open record's NULL stop is distinct to a UNIQUE constraint). The text is not part of a record's identity: Blastware writes it, and two copies of the same records differ there. WaveformStore.save_mlg keeps the original file byte for byte under <serial>/mlg/. Operational consequence: two new tables on first start; no migration, no backfill. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01ALX2R9LdWawLzUxwewZGA1
95 lines
3.8 KiB
Python
95 lines
3.8 KiB
Python
"""
|
|
Tests for .MLG storage: SeismoDb's mlg_files / mlg_records tables and
|
|
WaveformStore.save_mlg (spec docs/superpowers/specs/2026-11-09-mlg-unit-timeline-design.md §3).
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import datetime
|
|
import hashlib
|
|
import os
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
|
|
|
from minimateplus.mlg_file import read_mlg
|
|
from sfm.database import SeismoDb
|
|
from sfm.waveform_store import WaveformStore
|
|
|
|
FIX = Path(__file__).parent / "fixtures" / "mlg-11-9-26"
|
|
UTC = datetime.timezone.utc
|
|
|
|
|
|
def _insert(db: SeismoDb, name: str, *, as_name: str | None = None, call_in: bool = True):
|
|
raw = (FIX / name).read_bytes()
|
|
mlg = read_mlg(FIX / name)
|
|
return db.insert_mlg(
|
|
serial=mlg.serial, file_name=as_name or name, sha256=hashlib.sha256(raw).hexdigest(),
|
|
size=len(raw), empty=mlg.empty, call_in=call_in,
|
|
file_mtime=datetime.datetime(2026, 9, 8, 17, 39, 52, tzinfo=UTC),
|
|
stored_path=f"{mlg.serial}/mlg/{as_name or name}", records=mlg.records,
|
|
)
|
|
|
|
|
|
def test_insert_and_query_round_trip(tmp_path):
|
|
db = SeismoDb(tmp_path / "t.db")
|
|
file_id, inserted, skipped = _insert(db, "BE128443067.MLG")
|
|
assert file_id and (inserted, skipped) == (3, 0)
|
|
rows = db.query_mlg_records("BE12844")
|
|
assert [r["start_time"] for r in rows] == [
|
|
"2026-09-07T18:47:27", "2026-09-08T06:00:13", "2026-09-08T13:38:22"]
|
|
assert rows[1]["stop_time"] is None and rows[1]["flags"] == 0xFFFF
|
|
assert (rows[2]["trigger_source"], rows[2]["event_time"], rows[2]["seq"]) == (
|
|
1, "2026-09-08T13:38:22", 3)
|
|
assert rows[2]["trigger_level"] == "Geo: 0.0500 in/s Mic: 125.0 dB(L)"
|
|
assert {r["file_id"] for r in rows} == {file_id}
|
|
f = db.get_mlg_file(file_id)
|
|
assert (f["file_name"], f["record_count"], f["empty"], f["call_in"]) == ("BE128443067.MLG", 3, 0, 1)
|
|
assert f["file_mtime"] == "2026-09-08T17:39:52+00:00"
|
|
assert [x["id"] for x in db.query_mlg_files("BE12844")] == [file_id]
|
|
|
|
|
|
def test_same_file_twice_is_a_duplicate(tmp_path):
|
|
db = SeismoDb(tmp_path / "t.db")
|
|
_insert(db, "BE128443067.MLG")
|
|
assert _insert(db, "BE128443067.MLG") == (None, 0, 0)
|
|
assert len(db.query_mlg_files("BE12844")) == 1
|
|
raw = (FIX / "BE128443067.MLG").read_bytes()
|
|
assert db.mlg_file_exists("BE128443067.MLG", hashlib.sha256(raw).hexdigest())
|
|
assert not db.mlg_file_exists("BE128443067.MLG", "0" * 64)
|
|
|
|
|
|
def test_open_record_is_stored_once(tmp_path):
|
|
db = SeismoDb(tmp_path / "t.db")
|
|
_insert(db, "BE128443067.MLG")
|
|
file_id, inserted, skipped = _insert(db, "BE128443067.MLG", as_name="BE12844.MLG", call_in=False)
|
|
assert file_id and (inserted, skipped) == (0, 3)
|
|
assert len(db.query_mlg_records("BE12844")) == 3
|
|
|
|
|
|
def test_records_differing_only_in_text_are_the_same_record(tmp_path):
|
|
db = SeismoDb(tmp_path / "t.db")
|
|
assert _insert(db, "BE11529.MLG")[1:] == (3, 0)
|
|
file_id, inserted, skipped = _insert(db, "BE115292.MLG")
|
|
assert file_id and (inserted, skipped) == (0, 3)
|
|
|
|
|
|
def test_known_serials(tmp_path):
|
|
db = SeismoDb(tmp_path / "t.db")
|
|
assert db.known_serials() == set()
|
|
_insert(db, "BE128443067.MLG")
|
|
_insert(db, "BE8081.MLG")
|
|
assert db.known_serials() == {"BE12844", "BE8081"}
|
|
|
|
|
|
def test_save_mlg_keeps_both_when_names_collide(tmp_path):
|
|
store = WaveformStore(tmp_path / "waveforms")
|
|
a = store.save_mlg("BE11529", "BE11529.MLG", b"one")
|
|
assert a == "BE11529/mlg/BE11529.MLG"
|
|
assert store.save_mlg("BE11529", "BE11529.MLG", b"one") == a # same bytes: reused
|
|
b = store.save_mlg("BE11529", "BE11529.MLG", b"two")
|
|
assert b != a and b.startswith("BE11529/mlg/BE11529.") and b.endswith(".MLG")
|
|
assert (store.root / a).read_bytes() == b"one" and (store.root / b).read_bytes() == b"two"
|
|
assert store.save_mlg(None, "BE6907751.MLG", b"x") == "_unresolved/mlg/BE6907751.MLG"
|