261 lines
8.4 KiB
Python
261 lines
8.4 KiB
Python
"""/api/focus on an in-memory store. The live engine dependency is overridden."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import pytest
|
|
from fastapi.testclient import TestClient
|
|
from sqlalchemy import create_engine
|
|
from sqlalchemy.pool import StaticPool
|
|
|
|
from bib.item import Item
|
|
from bib.store import Store as BibStore
|
|
from focus.api import get_actor, get_bib, get_store
|
|
from focus.schema import ensure
|
|
from focus.store import Store
|
|
from llm.api import app
|
|
|
|
KEY = "ABC123"
|
|
OTHER = "DEF456"
|
|
|
|
|
|
@pytest.fixture
|
|
def ctx(tmp_path):
|
|
engine = create_engine(
|
|
"sqlite://",
|
|
poolclass=StaticPool,
|
|
connect_args={"check_same_thread": False},
|
|
)
|
|
ensure(engine)
|
|
focus = Store(engine)
|
|
# bib.Store holds one sqlite connection, and the endpoint runs on a
|
|
# worker thread. A file database lets each request open its own Store.
|
|
db_path = tmp_path / "bib.sqlite"
|
|
storage = tmp_path / "storage"
|
|
bib = BibStore(db_path, storage_dir=storage)
|
|
|
|
def override_bib():
|
|
request_store = BibStore(db_path, storage_dir=storage)
|
|
try:
|
|
yield request_store
|
|
finally:
|
|
request_store.close()
|
|
|
|
app.dependency_overrides[get_store] = lambda: focus
|
|
app.dependency_overrides[get_bib] = override_bib
|
|
try:
|
|
yield focus, bib, TestClient(app)
|
|
finally:
|
|
app.dependency_overrides.clear()
|
|
engine.dispose()
|
|
bib.close()
|
|
|
|
|
|
def _body(**overrides):
|
|
fields = {
|
|
"protocol": "comment-stance",
|
|
"label": "support",
|
|
"quote": "we support the proposal",
|
|
"prefix": "and ",
|
|
"suffix": " as",
|
|
"page": 1,
|
|
"rects": [{"x": 0, "y": 0, "w": 1, "h": 1}],
|
|
"note": "",
|
|
}
|
|
fields.update(overrides)
|
|
return fields
|
|
|
|
|
|
def test_actor_header_and_blank():
|
|
assert get_actor("ada") == "ada"
|
|
assert get_actor("") == "unknown"
|
|
assert get_actor(" ") == "unknown"
|
|
|
|
|
|
def test_protocols_and_bad_keys(ctx):
|
|
_focus, _bib, client = ctx
|
|
listed = client.get("/api/focus/protocols")
|
|
assert listed.status_code == 200
|
|
names = [row["name"] for row in listed.json()]
|
|
assert "comment-stance" in names
|
|
stance = next(row for row in listed.json() if row["name"] == "comment-stance")
|
|
assert stance["labels"][0]["key"] == "1"
|
|
assert client.get("/api/focus/nope/annotations").status_code == 404
|
|
assert client.get("/api/focus/nope/text").status_code == 404
|
|
assert client.post("/api/focus/nope/annotations", json=_body()).status_code == 404
|
|
|
|
|
|
def test_create_list_patch_delete_and_tags(ctx):
|
|
focus, bib, client = ctx
|
|
bib.create(
|
|
Item(key=KEY, item_type="report", title="A comment"),
|
|
tags=["llm:payment", "source:fr"],
|
|
)
|
|
created = client.post(
|
|
f"/api/focus/{KEY}/annotations",
|
|
json=_body(),
|
|
headers={"X-Auth-Request-User": "ada"},
|
|
)
|
|
assert created.status_code == 201, created.text
|
|
ann = created.json()
|
|
assert ann["provenance"] == "human"
|
|
assert ann["actor"] == "ada"
|
|
assert ann["model"] is None
|
|
assert ann["quote"] == "we support the proposal"
|
|
assert "hl:comment-stance:support" in bib.get(KEY).tags
|
|
assert "llm:payment" in bib.get(KEY).tags
|
|
|
|
listed = client.get(f"/api/focus/{KEY}/annotations")
|
|
assert listed.status_code == 200
|
|
assert [row["id"] for row in listed.json()] == [ann["id"]]
|
|
assert (
|
|
client.get(f"/api/focus/{KEY}/annotations?protocol=cms-disposition").json()
|
|
== []
|
|
)
|
|
|
|
noted = client.patch(
|
|
f"/api/focus/annotations/{ann['id']}",
|
|
json={"note": "see page 2"},
|
|
headers={"X-Auth-Request-User": "ada"},
|
|
)
|
|
assert noted.status_code == 200
|
|
assert noted.json()["note"] == "see page 2"
|
|
|
|
relabeled = client.patch(
|
|
f"/api/focus/annotations/{ann['id']}",
|
|
json={"label": "do-not-support", "note": "revised"},
|
|
)
|
|
assert relabeled.status_code == 200
|
|
assert relabeled.json()["label"] == "do-not-support"
|
|
# The annotation actor is who created it. A blank header is recorded
|
|
# on the relabel event as "unknown".
|
|
assert relabeled.json()["actor"] == "ada"
|
|
assert focus.history(ann["id"])[-1].actor == "unknown"
|
|
tags = set(bib.get(KEY).tags)
|
|
assert "hl:comment-stance:do-not-support" in tags
|
|
assert "hl:comment-stance:support" not in tags
|
|
|
|
removed = client.delete(f"/api/focus/annotations/{ann['id']}")
|
|
assert removed.status_code == 200
|
|
assert removed.json()["deleted_at"]
|
|
assert client.get(f"/api/focus/{KEY}/annotations").json() == []
|
|
assert not any(tag.startswith("hl:") for tag in bib.get(KEY).tags)
|
|
# the row is still in the store
|
|
assert focus.list(KEY, include_deleted=True)
|
|
|
|
|
|
def test_validation_status_codes(ctx):
|
|
_focus, bib, client = ctx
|
|
bib.create(Item(key=KEY, item_type="report", title="A comment"))
|
|
assert (
|
|
client.post(
|
|
f"/api/focus/{KEY}/annotations", json=_body(quote=" ")
|
|
).status_code
|
|
== 422
|
|
)
|
|
assert client.post(f"/api/focus/{KEY}/annotations", json={}).status_code == 422
|
|
bad_label = client.post(f"/api/focus/{KEY}/annotations", json=_body(label="nope"))
|
|
assert bad_label.status_code == 400
|
|
unknown = client.post(
|
|
f"/api/focus/{KEY}/annotations", json=_body(protocol="no-such-protocol")
|
|
)
|
|
assert unknown.status_code == 400
|
|
assert (
|
|
client.post(
|
|
f"/api/focus/{KEY}/annotations", json=_body(rects="nope")
|
|
).status_code
|
|
== 422
|
|
)
|
|
assert client.patch("/api/focus/annotations/missing", json={}).status_code == 422
|
|
assert (
|
|
client.patch("/api/focus/annotations/missing", json={"note": "x"}).status_code
|
|
== 404
|
|
)
|
|
assert client.delete("/api/focus/annotations/missing").status_code == 404
|
|
made = client.post(f"/api/focus/{KEY}/annotations", json=_body()).json()
|
|
assert (
|
|
client.patch(
|
|
f"/api/focus/annotations/{made['id']}",
|
|
json={"label": "finalized-as-proposed"},
|
|
).status_code
|
|
== 400
|
|
)
|
|
|
|
|
|
def test_annotation_without_a_bib_row_is_still_stored(ctx):
|
|
_focus, _bib, client = ctx
|
|
created = client.post(f"/api/focus/{KEY}/annotations", json=_body())
|
|
assert created.status_code == 201
|
|
assert (
|
|
client.get(f"/api/focus/{KEY}/annotations").json()[0]["id"]
|
|
== created.json()["id"]
|
|
)
|
|
|
|
|
|
def test_links(ctx):
|
|
_focus, _bib, client = ctx
|
|
left = client.post(f"/api/focus/{KEY}/annotations", json=_body()).json()
|
|
right = client.post(
|
|
f"/api/focus/{OTHER}/annotations", json=_body(quote="the response")
|
|
).json()
|
|
made = client.post(
|
|
"/api/focus/links",
|
|
json={"from_id": left["id"], "to_id": right["id"], "kind": "responds_to"},
|
|
headers={"X-Auth-Request-User": "ada"},
|
|
)
|
|
assert made.status_code == 201, made.text
|
|
assert made.json()["actor"] == "ada"
|
|
assert made.json()["deleted_at"] is None
|
|
removed = client.request(
|
|
"DELETE",
|
|
"/api/focus/links",
|
|
json={"from_id": left["id"], "to_id": right["id"], "kind": "responds_to"},
|
|
)
|
|
assert removed.status_code == 200
|
|
assert removed.json()["deleted_at"]
|
|
assert (
|
|
client.request(
|
|
"DELETE",
|
|
"/api/focus/links",
|
|
json={"from_id": left["id"], "to_id": right["id"], "kind": "responds_to"},
|
|
).status_code
|
|
== 404
|
|
)
|
|
assert (
|
|
client.post(
|
|
"/api/focus/links",
|
|
json={"from_id": left["id"], "to_id": right["id"], "kind": "cites"},
|
|
).status_code
|
|
== 422
|
|
)
|
|
assert (
|
|
client.post(
|
|
"/api/focus/links",
|
|
json={"from_id": left["id"], "to_id": "missing-id", "kind": "supports"},
|
|
).status_code
|
|
== 404
|
|
)
|
|
assert (
|
|
client.request(
|
|
"DELETE",
|
|
"/api/focus/links",
|
|
json={"from_id": left["id"], "to_id": right["id"], "kind": "cites"},
|
|
).status_code
|
|
== 422
|
|
)
|
|
|
|
|
|
def test_text_endpoint(ctx):
|
|
_focus, bib, client = ctx
|
|
assert client.get(f"/api/focus/{KEY}/text").status_code == 404
|
|
bib.create(
|
|
Item(
|
|
key=KEY, item_type="report", title="A report", abstract="Report body here."
|
|
)
|
|
)
|
|
got = client.get(f"/api/focus/{KEY}/text")
|
|
assert got.status_code == 200
|
|
assert got.json() == {"key": KEY, "attachment": "", "text": "Report body here."}
|
|
empty = client.get(f"/api/focus/{KEY}/text?attachment=missing.txt")
|
|
assert empty.status_code == 200
|
|
assert empty.json()["text"] == ""
|