Files
stack/tests/focus/test_export.py

213 lines
6.1 KiB
Python

"""Zotero note export. Dry-run writes nothing. One note per item and protocol."""
from __future__ import annotations
from sqlalchemy import create_engine
from sqlalchemy.pool import StaticPool
from typer.testing import CliRunner
from cli import app
from focus.export import export_notes
from focus.schema import ensure
from focus.store import Store
from zot.db import TYPE_MAP, Db
from zot.schema import create_db
runner = CliRunner()
def _engine():
return create_engine(
"sqlite://",
poolclass=StaticPool,
connect_args={"check_same_thread": False},
)
def _add(store: Store, **overrides):
fields = {
"item_key": "AAAAAAAA",
"attachment": "letter.pdf",
"protocol": "comment-stance",
"label": "support",
"quote": "we support the proposal",
"prefix": "",
"suffix": "",
"start_char": None,
"end_char": None,
"page": None,
"rects": None,
"note": "worth keeping",
"actor": "kert",
}
fields.update(overrides)
return store.add(**fields)
def _library(tmp_path):
path = tmp_path / "zotero.sqlite"
create_db(str(path)).close()
db = Db(str(path))
kept = db.create_item(TYPE_MAP["journalArticle"], key="AAAAAAAA")
other = db.create_item(TYPE_MAP["journalArticle"], key="BBBBBBBB")
db.add_note(kept, "<p>keep me</p>", title="Mine")
db.commit()
return db, path, kept, other
def _notes(db: Db, parent_id: int) -> list[tuple[str, str]]:
rows = db.con.execute(
"""SELECT n.title, n.note FROM itemNotes n
WHERE n.parentItemID = ? ORDER BY n.itemID""",
(parent_id,),
).fetchall()
return [(r[0], r[1]) for r in rows]
def test_dry_run_counts_and_writes_nothing(tmp_path):
engine = _engine()
ensure(engine)
store = Store(engine)
ann = _add(store, quote="a <b>& c")
_add(
store,
item_key="NOZOTERO",
quote="a comment with no library row",
note="",
)
_add(
store,
item_key="BBBBBBBB",
protocol="cms-disposition",
label="finalized-as-proposed",
quote="finalized as proposed",
note="",
)
db, _path, kept, _other = _library(tmp_path)
try:
summary = export_notes(store, db, dry_run=True)
assert summary == {
"dry_run": True,
"create": 2,
"replace": 0,
"skipped_no_zotero_key": 1,
}
assert _notes(db, kept) == [("Mine", "<p>keep me</p>")]
body = export_notes(store, db, dry_run=False)
assert body["dry_run"] is False
assert body["create"] == 2
assert body["skipped_no_zotero_key"] == 1
notes = _notes(db, kept)
assert notes[0] == ("Mine", "<p>keep me</p>")
title, html = notes[1]
assert title == "Focus: comment-stance"
assert html.startswith("<!-- focus:comment-stance -->\n<ul>\n")
assert f'data-focus-id="{ann.id}"' in html
assert "Support" in html
assert "a &lt;b&gt;&amp; c" in html
assert "worth keeping" in html
assert "<b>" not in html
again = export_notes(store, db, dry_run=True)
assert again["create"] == 0
assert again["replace"] == 2
assert _notes(db, kept)[1][1] == html
finally:
db.close()
engine.dispose()
def test_replace_is_idempotent_and_skips_deleted_and_model(tmp_path):
engine = _engine()
ensure(engine)
store = Store(engine)
ann = _add(store)
gone = _add(store, quote="this was deleted", note="")
store.delete(gone.id, "kert")
_add(
store,
quote="model said so",
note="not a person",
provenance="model",
model="local",
)
db, _path, kept, _other = _library(tmp_path)
try:
export_notes(store, db, dry_run=False)
store.update_note(ann.id, "edited", "kert")
second = export_notes(store, db, protocol="comment-stance", dry_run=False)
assert second == {
"dry_run": False,
"create": 0,
"replace": 1,
"skipped_no_zotero_key": 0,
}
notes = _notes(db, kept)
assert len(notes) == 2
assert notes[0] == ("Mine", "<p>keep me</p>")
assert "edited" in notes[1][1]
assert "this was deleted" not in notes[1][1]
assert "model said so" not in notes[1][1]
assert notes[1][1].count("data-focus-id") == 1
finally:
db.close()
engine.dispose()
def test_cli_dry_run_then_write(tmp_path):
focus_path = tmp_path / "focus.sqlite"
url = "sqlite:///" + str(focus_path)
engine = create_engine(url)
ensure(engine)
store = Store(engine)
_add(store, quote="from the cli")
engine.dispose()
db, path, kept, _other = _library(tmp_path)
db.close()
dry = runner.invoke(
app,
["focus", "export-notes", "--db", str(path), "--focus-db", url],
)
assert dry.exit_code == 0, dry.output
assert "dry_run: True" in dry.stdout
assert "create: 1" in dry.stdout
with Db(str(path)) as check:
assert _notes(check, kept) == [("Mine", "<p>keep me</p>")]
wrote = runner.invoke(
app,
["focus", "export-notes", "--db", str(path), "--focus-db", url, "--write"],
)
assert wrote.exit_code == 0, wrote.output
assert "dry_run: False" in wrote.stdout
assert "create: 1" in wrote.stdout
with Db(str(path)) as check:
notes = _notes(check, kept)
assert notes[1][0] == "Focus: comment-stance"
assert "from the cli" in notes[1][1]
assert "<!-- focus:comment-stance -->" in notes[1][1]
def test_cli_rejects_an_unknown_protocol(tmp_path):
url = "sqlite:///" + str(tmp_path / "focus.sqlite")
engine = create_engine(url)
ensure(engine)
engine.dispose()
_db, path, _kept, _other = _library(tmp_path)
_db.close()
result = runner.invoke(
app,
[
"focus",
"export-notes",
"--db",
str(path),
"--focus-db",
url,
"--protocol",
"no-such",
],
)
assert result.exit_code != 0
assert "no-such" in result.output