Implement Catalogue class with BookRecord dataclass for managing the CSV-based book inventory. Provides operations to append records, read all rows, update specific books, and generate unique book IDs. 5 tests passing, all functionality working as specified.
49 lines
1.7 KiB
Python
49 lines
1.7 KiB
Python
import pytest
|
|
|
|
from bookmark.catalogue import Catalogue, BookRecord, CATALOGUE_COLUMNS
|
|
|
|
|
|
def test_creating_catalogue_writes_header(tmp_path):
|
|
csv_path = tmp_path / "catalogue.csv"
|
|
Catalogue(csv_path)
|
|
assert csv_path.exists()
|
|
header_line = csv_path.read_text().splitlines()[0]
|
|
assert header_line.split(",") == CATALOGUE_COLUMNS
|
|
|
|
|
|
def test_append_and_read_all_round_trip(tmp_path):
|
|
catalogue = Catalogue(tmp_path / "catalogue.csv")
|
|
record = BookRecord(book_id="abc123", timestamp="2026-07-13T00:00:00+00:00", size_bucket="medium")
|
|
catalogue.append(record)
|
|
|
|
rows = catalogue.read_all()
|
|
|
|
assert len(rows) == 1
|
|
assert rows[0]["book_id"] == "abc123"
|
|
assert rows[0]["size_bucket"] == "medium"
|
|
assert rows[0]["status"] == "captured"
|
|
|
|
|
|
def test_new_book_id_is_unique(tmp_path):
|
|
catalogue = Catalogue(tmp_path / "catalogue.csv")
|
|
ids = {catalogue.new_book_id() for _ in range(100)}
|
|
assert len(ids) == 100
|
|
|
|
|
|
def test_update_row_modifies_matching_row(tmp_path):
|
|
catalogue = Catalogue(tmp_path / "catalogue.csv")
|
|
catalogue.append(BookRecord(book_id="abc123", timestamp="t", size_bucket="small"))
|
|
catalogue.append(BookRecord(book_id="xyz789", timestamp="t", size_bucket="large"))
|
|
|
|
catalogue.update_row("abc123", title="Charlotte's Web", status="ready")
|
|
|
|
rows = {row["book_id"]: row for row in catalogue.read_all()}
|
|
assert rows["abc123"]["title"] == "Charlotte's Web"
|
|
assert rows["abc123"]["status"] == "ready"
|
|
assert rows["xyz789"]["title"] == ""
|
|
|
|
|
|
def test_update_row_raises_for_unknown_book_id(tmp_path):
|
|
catalogue = Catalogue(tmp_path / "catalogue.csv")
|
|
with pytest.raises(KeyError):
|
|
catalogue.update_row("does-not-exist", title="x")
|