""" 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"]) == (16, 0, 3) 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"]) == (16, 16, 0, 3, 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", "BE6907899.MLG", "BE6907900.MLG", "BE6907901.MLG", "BE6907902.MLG"] assert main(args) == 0 again = _counts(capsys.readouterr().out) assert (again["duplicate"], again["ok"], again["inserted"]) == (16, 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