Files
stack/tests/bib/test_store_updated_at.py

99 lines
2.6 KiB
Python

"""Tag/collection changes must stamp ``items.updated_at``.
The llm indexer fingerprints an item as ``updated_at`` + its file stats,
so a tag-only edit (``year:``, ``project:``, ``cms-rule:`` — all of which
land in chunk metadata) is invisible to a re-index unless the row is
stamped. An *unchanged* upsert must still write nothing.
"""
from __future__ import annotations
from bib.item import Source
from bib.store import Store
OLD = "2000-01-01T00:00:00Z"
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 _backdate(s: Store, key: str) -> None:
"""updated_at has 1s granularity — backdate so a bump is observable."""
s._con().execute("UPDATE items SET updated_at = ? WHERE key = ?", (OLD, key))
s._con().commit()
def _updated_at(s: Store, key: str) -> str:
return (
s._con()
.execute("SELECT updated_at FROM items WHERE key = ?", (key,))
.fetchone()["updated_at"]
)
def test_add_tag_bumps_updated_at():
s = _store()
key = s.create(_item())
_backdate(s, key)
s.add_tag(key, "year:2026")
assert _updated_at(s, key) != OLD
def test_re_adding_the_same_tag_does_not_bump():
s = _store()
key = s.create(_item())
s.add_tag(key, "year:2026")
_backdate(s, key)
s.add_tag(key, "year:2026") # already there — nothing changed
assert _updated_at(s, key) == OLD
def test_remove_tag_bumps_updated_at():
s = _store()
key = s.create(_item(tags=["year:2026"]))
_backdate(s, key)
s.remove_tag(key, "year:2026")
assert _updated_at(s, key) != OLD
def test_removing_an_absent_tag_does_not_bump():
s = _store()
key = s.create(_item())
_backdate(s, key)
s.remove_tag(key, "nope:1")
assert _updated_at(s, key) == OLD
def test_update_with_only_tags_bumps_updated_at():
s = _store()
key = s.create(_item())
_backdate(s, key)
s.update(key, tags=["year:2026", "project:pfs"])
assert _updated_at(s, key) != OLD
def test_update_with_only_collections_bumps_updated_at():
s = _store()
key = s.create(_item())
_backdate(s, key)
s.update(key, collections=[])
assert _updated_at(s, key) != OLD
def test_unchanged_upsert_still_does_not_bump():
s = _store()
key, _ = s.upsert_status(_item())
_backdate(s, key)
key2, status = s.upsert_status(_item())
assert (key2, status) == (key, "unchanged")
assert _updated_at(s, key) == OLD