Fix round 1: the sealed-docket test file's _store() helper never closed its in-memory Store, and extract's file-backed lookup connection was never closed either — both leaked sqlite3 connections that surfaced as nondeterministic ResourceWarnings when GC'd during unrelated tests. Replace _store() with a store fixture that closes on teardown, and have extract() close the private read connection _build_bib_helpers opens for file-backed stores (the :memory: case still reuses store's own connection, left untouched). Also reworded the _build_bib_helpers docstring: attach_callback is None for --no-bib or --no-attach; the store is None only for --no-bib.
152 lines
4.3 KiB
Python
152 lines
4.3 KiB
Python
from __future__ import annotations
|
|
|
|
from pathlib import Path
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
import fitz
|
|
import pytest
|
|
from typer.testing import CliRunner
|
|
|
|
from bib.dockets import Docket
|
|
from bib.store import Store
|
|
from cli.comments import app
|
|
|
|
runner = CliRunner()
|
|
|
|
|
|
@pytest.fixture
|
|
def store() -> Store:
|
|
s = Store(":memory:", storage_dir="/tmp/nope")
|
|
try:
|
|
yield s
|
|
finally:
|
|
s.close()
|
|
|
|
|
|
def _seed_dir(root: Path, docket: str) -> Path:
|
|
cdir = root / docket / f"{docket}-0001"
|
|
cdir.mkdir(parents=True)
|
|
doc = fitz.open()
|
|
doc.new_page().insert_text((50, 72), "Long body content " * 10)
|
|
doc.save(str(cdir / "attachment_1.pdf"))
|
|
doc.close()
|
|
return cdir
|
|
|
|
|
|
@patch("bib.connect")
|
|
def test_dockets_table_lists_rows(mc_connect, store):
|
|
s = store
|
|
s.docket_upsert(
|
|
Docket(
|
|
id="CMS-2019-0111",
|
|
rule_cms_id="CMS-1693-P",
|
|
comment_end_date="2019-09-27",
|
|
sealed_at="2020-01-01T00:00:00Z",
|
|
seal_reason="manual",
|
|
)
|
|
)
|
|
s.docket_upsert(
|
|
Docket(
|
|
id="CMS-2026-2377", rule_cms_id="CMS-1848-P", comment_end_date="2026-09-14"
|
|
)
|
|
)
|
|
mc_connect.return_value = s
|
|
result = runner.invoke(app, ["dockets"])
|
|
assert result.exit_code == 0, result.output
|
|
assert "CMS-2019-0111" in result.output and "sealed" in result.output
|
|
assert "CMS-2026-2377" in result.output and "open" in result.output
|
|
|
|
|
|
@patch("bib.connect")
|
|
def test_seal_and_unseal(mc_connect, store):
|
|
s = store
|
|
s.docket_upsert(Docket(id="CMS-2019-0111"))
|
|
mc_connect.return_value = s
|
|
assert (
|
|
runner.invoke(
|
|
app, ["seal", "CMS-2019-0111", "--reason", "historical"]
|
|
).exit_code
|
|
== 0
|
|
)
|
|
assert s.docket_get("CMS-2019-0111").seal_reason == "historical"
|
|
assert runner.invoke(app, ["unseal", "CMS-2019-0111"]).exit_code == 0
|
|
assert not s.docket_get("CMS-2019-0111").sealed
|
|
|
|
|
|
@patch("bib.connect")
|
|
def test_seal_unknown_docket_fails(mc_connect, store):
|
|
mc_connect.return_value = store
|
|
result = runner.invoke(app, ["seal", "CMS-0000-0000"])
|
|
assert result.exit_code == 1
|
|
assert "unknown docket" in result.output
|
|
|
|
|
|
@patch("bib.regulations_gov.discover_docket")
|
|
@patch("bib.regulations_gov.Client")
|
|
@patch("bib.connect")
|
|
def test_dockets_discover_populates_from_tags(mc_connect, mc_client, mc_disc, store):
|
|
from bib.item import Source
|
|
|
|
s = store
|
|
it = Source(title="c", url="https://www.regulations.gov/comment/CMS-2019-0111-1")
|
|
it.add_tag("reg-docket:CMS-2019-0111")
|
|
it.add_tag("rule:CMS-1693-P")
|
|
s.create(it)
|
|
mc_connect.return_value = s
|
|
api = MagicMock()
|
|
api.__enter__ = MagicMock(return_value=api)
|
|
api.__exit__ = MagicMock(return_value=False)
|
|
mc_client.return_value = api
|
|
mc_disc.return_value = Docket(
|
|
id="CMS-2019-0111",
|
|
rule_cms_id="CMS-1693-P",
|
|
fr_object_id="o",
|
|
comment_end_date="2019-09-27",
|
|
)
|
|
|
|
result = runner.invoke(app, ["dockets", "--discover"])
|
|
|
|
assert result.exit_code == 0, result.output
|
|
mc_disc.assert_called_once_with(api, "CMS-2019-0111", rule_cms_id="CMS-1693-P")
|
|
assert s.docket_get("CMS-2019-0111").comment_end_date == "2019-09-27"
|
|
|
|
|
|
@patch("bib.connect")
|
|
def test_extract_skips_sealed_docket(mc_connect, tmp_path: Path, store):
|
|
s = store
|
|
s.docket_upsert(
|
|
Docket(
|
|
id="CMS-2024-0001", sealed_at="2025-01-01T00:00:00Z", seal_reason="manual"
|
|
)
|
|
)
|
|
mc_connect.return_value = s
|
|
cdir = _seed_dir(tmp_path, "CMS-2024-0001")
|
|
result = runner.invoke(
|
|
app, ["extract", "--root", str(tmp_path), "--workers", "1", "--no-attach"]
|
|
)
|
|
assert result.exit_code == 0, result.output
|
|
assert "skipped_sealed: 1" in result.output
|
|
assert not (cdir / "combined.md").exists()
|
|
|
|
|
|
@patch("bib.connect")
|
|
def test_extract_force_runs_sealed_docket(mc_connect, tmp_path: Path, store):
|
|
s = store
|
|
s.docket_upsert(Docket(id="CMS-2024-0001", sealed_at="2025-01-01T00:00:00Z"))
|
|
mc_connect.return_value = s
|
|
cdir = _seed_dir(tmp_path, "CMS-2024-0001")
|
|
result = runner.invoke(
|
|
app,
|
|
[
|
|
"extract",
|
|
"--root",
|
|
str(tmp_path),
|
|
"--workers",
|
|
"1",
|
|
"--no-attach",
|
|
"--force",
|
|
],
|
|
)
|
|
assert result.exit_code == 0, result.output
|
|
assert (cdir / "combined.md").is_file()
|