Files
stack/tests/bib/test_store_attach_idempotent.py

31 lines
1.1 KiB
Python

from __future__ import annotations
from pathlib import Path
from bib.item import Source
from bib.store import Store
def test_second_attach_of_same_filename_returns_existing_key(tmp_path: Path):
s = Store(str(tmp_path / "bib.sqlite"), storage_dir=tmp_path / "storage")
key = s.create(Source(title="T", url="https://x/1"))
f = tmp_path / "attachment_1.pdf"
f.write_bytes(b"%PDF")
k1 = s.attach_file(key, f, title="attachment_1.pdf")
k2 = s.attach_file(key, f, title="attachment_1.pdf")
assert k1 == k2
n = s._con().execute("SELECT count(*) FROM attachments").fetchone()[0]
assert n == 1
# exactly one storage copy
assert len(list((tmp_path / "storage").iterdir())) == 1
def test_different_filename_creates_second_row(tmp_path: Path):
s = Store(str(tmp_path / "bib.sqlite"), storage_dir=tmp_path / "storage")
key = s.create(Source(title="T", url="https://x/1"))
f1 = tmp_path / "a.pdf"
f1.write_bytes(b"a")
f2 = tmp_path / "b.pdf"
f2.write_bytes(b"b")
assert s.attach_file(key, f1) != s.attach_file(key, f2)