forked from ChelseaKR/queer-the-stacks
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_store_serde.py
More file actions
79 lines (62 loc) · 2.56 KB
/
Copy pathtest_store_serde.py
File metadata and controls
79 lines (62 loc) · 2.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
"""Serde round-trip fidelity + the persisted app-state store."""
from __future__ import annotations
from pathlib import Path
from ingest.models import Book, DailyActivity
from ingest.serde import (
_book_from_dict,
_book_to_dict,
activity_from_dict,
activity_to_dict,
state_from_dict,
state_to_dict,
)
from ingest.store import Store
def test_state_round_trips_with_full_fidelity(states: list) -> None:
for s in states:
assert state_from_dict(state_to_dict(s)) == s
def test_activity_round_trips() -> None:
a = DailyActivity(day_ordinal=19000, seconds=1234, pages=42)
assert activity_from_dict(activity_to_dict(a)) == a
def test_book_languages_and_publisher_round_trip() -> None:
"""FIX-11: sourced languages/publisher facts survive a to_dict/from_dict cycle."""
b = Book(
book_id="calibre:1",
title="Translated Novel",
languages=("eng", "fra"),
publisher="Small Press",
)
d = _book_to_dict(b)
assert d["languages"] == ["eng", "fra"]
assert d["publisher"] == "Small Press"
assert _book_from_dict(d) == b
def test_book_languages_and_publisher_backward_compat() -> None:
"""Old persisted snapshots lack the new keys; unknown stays first-class."""
d = {"book_id": "calibre:2", "title": "Old Snapshot", "authors": []}
b = _book_from_dict(d)
assert b.languages == ()
assert b.publisher is None
def test_store_save_and_load(states: list, daily_activity: list, tmp_path: Path) -> None:
store = Store(tmp_path / "state.sqlite")
try:
assert store.is_populated is False
store.save(states, daily_activity, refreshed_at=1_700_000_000, source_mtimes={"calibre": 5})
assert store.is_populated is True
assert store.refreshed_at() == 1_700_000_000
assert store.source_mtimes() == {"calibre": 5}
loaded = store.load_states()
assert loaded == states
assert store.load_daily_activity() == daily_activity
finally:
store.close()
def test_store_empty_reads(tmp_path: Path) -> None:
with Store(tmp_path / "empty.sqlite") as store:
assert store.load_states() == []
assert store.load_daily_activity() == []
assert store.refreshed_at() is None
assert store.source_mtimes() == {}
def test_store_overwrites_on_resave(states: list, tmp_path: Path) -> None:
with Store(tmp_path / "s.sqlite") as store:
store.save(states, [], refreshed_at=1)
store.save(states[:1], [], refreshed_at=2)
assert store.refreshed_at() == 2
assert len(store.load_states()) == 1