281 lines
9.4 KiB
Python
281 lines
9.4 KiB
Python
"""Annotation store on in-memory SQLite, plus the Postgres DDL branch."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import datetime as dt
|
|
from unittest.mock import MagicMock
|
|
|
|
import pytest
|
|
from sqlalchemy import create_engine, text
|
|
from sqlalchemy.pool import StaticPool
|
|
|
|
from focus.schema import ensure, statements_for
|
|
from focus.store import NotFound, Store, _iso
|
|
|
|
KEY = "ABC123"
|
|
OTHER = "DEF456"
|
|
|
|
|
|
def _engine():
|
|
return create_engine(
|
|
"sqlite://",
|
|
poolclass=StaticPool,
|
|
connect_args={"check_same_thread": False},
|
|
)
|
|
|
|
|
|
@pytest.fixture
|
|
def store():
|
|
engine = _engine()
|
|
ensure(engine)
|
|
try:
|
|
yield Store(engine)
|
|
finally:
|
|
engine.dispose()
|
|
|
|
|
|
def _add(store: Store, **overrides):
|
|
fields = {
|
|
"item_key": KEY,
|
|
"attachment": "letter.pdf",
|
|
"protocol": "comment-stance",
|
|
"label": "support",
|
|
"quote": "we support the proposal",
|
|
"prefix": "and ",
|
|
"suffix": " as",
|
|
"start_char": 4,
|
|
"end_char": 28,
|
|
"page": 2,
|
|
"rects": [{"x": 1, "y": 2, "w": 3, "h": 4}],
|
|
"note": "",
|
|
"actor": "kert",
|
|
}
|
|
fields.update(overrides)
|
|
return store.add(**fields)
|
|
|
|
|
|
def test_ensure_sqlite_is_idempotent_and_untyped():
|
|
engine = _engine()
|
|
ensure(engine)
|
|
ensure(engine)
|
|
with engine.connect() as conn:
|
|
sql = " ".join(
|
|
row[0]
|
|
for row in conn.execute(
|
|
text("SELECT sql FROM sqlite_master WHERE sql IS NOT NULL")
|
|
)
|
|
)
|
|
engine.dispose()
|
|
assert "focus_annotation" in sql
|
|
assert "focus_link" in sql
|
|
assert "focus_event" in sql
|
|
assert "jsonb" not in sql
|
|
assert "timestamptz" not in sql
|
|
assert "uuid" not in sql.lower()
|
|
assert "bigserial" not in sql.lower()
|
|
assert all("jsonb" not in stmt for stmt in statements_for("sqlite"))
|
|
|
|
|
|
def test_ensure_postgres_branch_uses_the_live_types():
|
|
engine = MagicMock()
|
|
engine.dialect.name = "postgresql"
|
|
conn = engine.begin.return_value.__enter__.return_value
|
|
ensure(engine)
|
|
executed = " ".join(str(call.args[0]) for call in conn.execute.call_args_list)
|
|
assert "CREATE TABLE IF NOT EXISTS focus_annotation" in executed
|
|
assert "uuid" in executed
|
|
assert "jsonb" in executed
|
|
assert "timestamptz" in executed
|
|
assert "bigserial" in executed
|
|
joined = " ".join(statements_for("postgresql"))
|
|
assert "jsonb" in joined and "timestamptz" in joined
|
|
|
|
|
|
def test_iso_normalises_datetimes_and_text():
|
|
aware = dt.datetime(2026, 1, 2, 3, 4, 5, tzinfo=dt.timezone.utc)
|
|
naive = dt.datetime(2026, 1, 2, 3, 4, 5)
|
|
assert _iso(None) is None
|
|
assert _iso("2026-01-02T03:04:05+00:00") == "2026-01-02T03:04:05+00:00"
|
|
assert _iso(aware) == "2026-01-02T03:04:05+00:00"
|
|
assert _iso(naive).startswith("2026-01-02T03:04:05")
|
|
|
|
|
|
def test_add_round_trip_and_event(store: Store):
|
|
ann = _add(store)
|
|
assert ann.item_key == KEY
|
|
assert ann.protocol == "comment-stance"
|
|
assert ann.protocol_version == 1
|
|
assert ann.label == "support"
|
|
assert ann.quote == "we support the proposal"
|
|
assert ann.prefix == "and "
|
|
assert ann.suffix == " as"
|
|
assert ann.page == 2
|
|
assert ann.rects == [{"x": 1, "y": 2, "w": 3, "h": 4}]
|
|
assert ann.provenance == "human"
|
|
assert ann.model is None
|
|
assert ann.deleted_at is None
|
|
assert store.list(KEY) == [ann]
|
|
assert store.list(OTHER) == []
|
|
events = store.history(ann.id)
|
|
assert [e.op for e in events] == ["add"]
|
|
assert events[0].payload["label"] == "support"
|
|
assert events[0].actor == "kert"
|
|
|
|
|
|
def test_add_rejects_bad_inputs(store: Store):
|
|
with pytest.raises(ValueError, match="unknown protocol"):
|
|
_add(store, protocol="no-such-protocol")
|
|
with pytest.raises(ValueError, match="not in comment-stance"):
|
|
_add(store, label="nope")
|
|
with pytest.raises(ValueError, match="quote is empty"):
|
|
_add(store, quote=" ")
|
|
with pytest.raises(ValueError, match="quote must be a string"):
|
|
_add(store, quote=1)
|
|
with pytest.raises(ValueError, match="item_key is empty"):
|
|
_add(store, item_key=" ")
|
|
with pytest.raises(ValueError, match="actor is empty"):
|
|
_add(store, actor="")
|
|
with pytest.raises(ValueError, match="provenance"):
|
|
_add(store, provenance="humanoid")
|
|
with pytest.raises(ValueError, match="requires model"):
|
|
_add(store, provenance="model", model=None)
|
|
with pytest.raises(ValueError, match="requires model"):
|
|
_add(store, provenance="model", model=" ")
|
|
with pytest.raises(ValueError, match="model must be a string"):
|
|
_add(store, model=3)
|
|
with pytest.raises(ValueError, match="rects"):
|
|
_add(store, rects="nope")
|
|
with pytest.raises(ValueError, match="page must be an int"):
|
|
_add(store, page=True)
|
|
with pytest.raises(ValueError, match="attachment must be a string"):
|
|
_add(store, attachment=1)
|
|
|
|
|
|
def test_add_accepts_blank_optional_text_and_a_model_label(store: Store):
|
|
human = _add(
|
|
store,
|
|
attachment=None,
|
|
prefix=None,
|
|
suffix=None,
|
|
note=None,
|
|
model=" ",
|
|
start_char=None,
|
|
end_char=None,
|
|
page=None,
|
|
)
|
|
assert human.attachment == ""
|
|
assert human.prefix == ""
|
|
assert human.note == ""
|
|
assert human.model is None
|
|
assert human.start_char is None
|
|
assert human.end_char is None
|
|
assert human.page is None
|
|
modelled = _add(
|
|
store,
|
|
provenance="model",
|
|
model=" local ",
|
|
quote="a suggested span",
|
|
label="do-not-support",
|
|
)
|
|
assert modelled.provenance == "model"
|
|
assert modelled.model == "local"
|
|
assert {a.id for a in store.list(KEY)} == {human.id, modelled.id}
|
|
|
|
|
|
def test_protocol_errors_other_than_missing_propagate(store: Store, monkeypatch):
|
|
def boom(_name: str):
|
|
raise ValueError("bad file")
|
|
|
|
monkeypatch.setattr("focus.store.load", boom)
|
|
with pytest.raises(ValueError, match="bad file"):
|
|
_add(store)
|
|
|
|
|
|
def test_add_rolls_back_when_the_event_fails(store: Store, monkeypatch):
|
|
def boom(*_args, **_kwargs):
|
|
raise RuntimeError("event failed")
|
|
|
|
monkeypatch.setattr("focus.store._write_event", boom)
|
|
with pytest.raises(RuntimeError):
|
|
_add(store)
|
|
assert store.list(KEY) == []
|
|
|
|
|
|
def test_list_filters_protocol_and_soft_delete(store: Store):
|
|
stance = _add(store)
|
|
disposition = _add(
|
|
store,
|
|
protocol="cms-disposition",
|
|
label="other",
|
|
quote="finalized something else",
|
|
)
|
|
assert [a.id for a in store.list(KEY, protocol="comment-stance")] == [stance.id]
|
|
gone = store.delete(stance.id, "kert")
|
|
assert gone.deleted_at
|
|
assert store.list(KEY) == [disposition]
|
|
assert {a.id for a in store.list(KEY, include_deleted=True)} == {
|
|
stance.id,
|
|
disposition.id,
|
|
}
|
|
with pytest.raises(NotFound):
|
|
store.delete(stance.id, "kert")
|
|
with pytest.raises(NotFound):
|
|
store.delete("missing-id", "kert")
|
|
|
|
|
|
def test_relabel_and_note_record_events(store: Store):
|
|
ann = _add(store)
|
|
noted = store.update_note(ann.id, "check the docket", "ada")
|
|
assert noted.note == "check the docket"
|
|
relabeled = store.relabel(ann.id, "do-not-support", "ada")
|
|
assert relabeled.label == "do-not-support"
|
|
assert [e.op for e in store.history(ann.id)] == ["add", "update_note", "relabel"]
|
|
assert store.history(ann.id)[2].payload == {
|
|
"label": "do-not-support",
|
|
"previous": "support",
|
|
}
|
|
with pytest.raises(ValueError, match="not in comment-stance"):
|
|
store.relabel(ann.id, "finalized-as-proposed", "ada")
|
|
assert store.list(KEY)[0].label == "do-not-support"
|
|
store.delete(ann.id, "ada")
|
|
with pytest.raises(NotFound):
|
|
store.update_note(ann.id, "later", "ada")
|
|
with pytest.raises(NotFound):
|
|
store.relabel(ann.id, "support", "ada")
|
|
with pytest.raises(NotFound):
|
|
store.update_note("missing-id", "x", "ada")
|
|
# history of a deleted annotation is still readable
|
|
assert store.history(ann.id)[-1].op == "delete"
|
|
with pytest.raises(NotFound):
|
|
store.history("missing-id")
|
|
|
|
|
|
def test_link_unlink_and_relink(store: Store):
|
|
left = _add(store, quote="the comment")
|
|
right = _add(store, item_key=OTHER, quote="the response")
|
|
made = store.link(left.id, right.id, "responds_to", "kert")
|
|
assert made.deleted_at is None
|
|
assert made.kind == "responds_to"
|
|
again = store.link(left.id, right.id, "responds_to", "kert")
|
|
assert again.deleted_at is None
|
|
assert [e.op for e in store.history(left.id)].count("link") == 1
|
|
removed = store.unlink(left.id, right.id, "responds_to", "ada")
|
|
assert removed.deleted_at
|
|
assert store.history(left.id)[-1].op == "unlink"
|
|
with pytest.raises(NotFound):
|
|
store.unlink(left.id, right.id, "responds_to", "ada")
|
|
revived = store.link(left.id, right.id, "responds_to", "ada")
|
|
assert revived.deleted_at is None
|
|
assert [e.op for e in store.history(left.id)].count("link") == 2
|
|
with pytest.raises(ValueError, match="kind"):
|
|
store.link(left.id, right.id, "cites", "ada")
|
|
with pytest.raises(ValueError, match="two annotations"):
|
|
store.link(left.id, left.id, "supports", "ada")
|
|
with pytest.raises(NotFound):
|
|
store.link(left.id, "missing-id", "contradicts", "ada")
|
|
with pytest.raises(ValueError, match="kind"):
|
|
store.unlink(left.id, right.id, "cites", "ada")
|
|
store.delete(right.id, "ada")
|
|
with pytest.raises(NotFound):
|
|
store.link(left.id, right.id, "contradicts", "ada")
|