105 lines
3.1 KiB
Python
105 lines
3.1 KiB
Python
"""Store.upsert must not rewrite rows/tags when nothing changed."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from bib.item import Source
|
|
from bib.store import Store
|
|
|
|
|
|
def _store() -> Store:
|
|
return Store(":memory:", storage_dir="/tmp/nope")
|
|
|
|
|
|
def _item(**kw) -> Source:
|
|
it = Source(title="T", url="https://www.regulations.gov/comment/CMS-2026-2377-1")
|
|
it.abstract = kw.get("abstract", "body")
|
|
for t in kw.get("tags", ["a:1", "b:2"]):
|
|
it.add_tag(t)
|
|
return it
|
|
|
|
|
|
def _snapshot(s: Store, key: str) -> tuple:
|
|
con = s._con()
|
|
row = con.execute(
|
|
"SELECT access_date, updated_at FROM items WHERE key=?", (key,)
|
|
).fetchone()
|
|
tags = con.execute(
|
|
"SELECT it.rowid FROM item_tags it JOIN items i ON i.id=it.item_id WHERE i.key=? ORDER BY 1",
|
|
(key,),
|
|
).fetchall()
|
|
return (row["access_date"], row["updated_at"], [t[0] for t in tags])
|
|
|
|
|
|
def test_first_upsert_is_created():
|
|
s = _store()
|
|
key, status = s.upsert_status(_item())
|
|
assert status == "created" and key
|
|
|
|
|
|
def test_identical_upsert_is_unchanged_and_writes_nothing():
|
|
s = _store()
|
|
key, _ = s.upsert_status(_item())
|
|
before = _snapshot(s, key)
|
|
# different Python object, same content
|
|
key2, status = s.upsert_status(_item())
|
|
assert key2 == key and status == "unchanged"
|
|
assert _snapshot(s, key) == before # no access/updated stamp, no tag row churn
|
|
|
|
|
|
def test_changed_abstract_is_updated():
|
|
s = _store()
|
|
key, _ = s.upsert_status(_item(abstract="v1"))
|
|
_, status = s.upsert_status(_item(abstract="v2"))
|
|
assert status == "updated"
|
|
assert s.get(key).abstract == "v2"
|
|
|
|
|
|
def test_new_tag_is_updated_and_merged():
|
|
s = _store()
|
|
key, _ = s.upsert_status(_item(tags=["a:1"]))
|
|
_, status = s.upsert_status(_item(tags=["c:3"]))
|
|
assert status == "updated"
|
|
assert set(s.get(key).tags) >= {"a:1", "c:3"}
|
|
|
|
|
|
def test_subset_of_existing_tags_is_unchanged():
|
|
"""Upsert never removes tags (#624); a subset therefore changes nothing."""
|
|
s = _store()
|
|
key, _ = s.upsert_status(_item(tags=["a:1", "b:2"]))
|
|
_, status = s.upsert_status(_item(tags=["a:1"]))
|
|
assert status == "unchanged"
|
|
|
|
|
|
def test_upsert_keeps_returning_key():
|
|
s = _store()
|
|
key = s.upsert(_item())
|
|
assert s.upsert(_item()) == key
|
|
|
|
|
|
def test_empty_incoming_abstract_keeps_stored_body():
|
|
"""A list-walk row (no body) must not blank an enriched comment."""
|
|
s = _store()
|
|
key, _ = s.upsert_status(_item(abstract="enriched body"))
|
|
_, status = s.upsert_status(_item(abstract=""))
|
|
assert status == "unchanged"
|
|
assert s.get(key).abstract == "enriched body"
|
|
|
|
|
|
def test_empty_incoming_title_keeps_stored_title():
|
|
s = _store()
|
|
it = _item()
|
|
it.title = "Org: CMS-2026-2377-1"
|
|
key, _ = s.upsert_status(it)
|
|
it2 = _item()
|
|
it2.title = ""
|
|
_, status = s.upsert_status(it2)
|
|
assert status == "unchanged"
|
|
assert s.get(key).title == "Org: CMS-2026-2377-1"
|
|
|
|
|
|
def test_non_empty_incoming_still_updates():
|
|
s = _store()
|
|
key, _ = s.upsert_status(_item(abstract="v1"))
|
|
_, status = s.upsert_status(_item(abstract="v2"))
|
|
assert status == "updated" and s.get(key).abstract == "v2"
|