forked from ChelseaKR/queer-the-stacks
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_unify.py
More file actions
136 lines (110 loc) · 4.52 KB
/
Copy pathtest_unify.py
File metadata and controls
136 lines (110 loc) · 4.52 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
"""Unify: cross-device progress, status classification, history completeness."""
from __future__ import annotations
import pytest
from ingest.models import Author, Book, DeviceProgress, ReadingStat, ReadingStatus
from ingest.unify import (
currently_reading,
finished,
normalize_key,
unify,
)
def test_currently_reading_and_finished(states: list) -> None:
reading = currently_reading(states)
done = finished(states)
reading_titles = {s.title for s in reading}
assert "Stone Butch Blues" in reading_titles
assert all(s.status is ReadingStatus.READING for s in reading)
assert all(s.status is ReadingStatus.FINISHED for s in done)
assert "Kindred" in {s.title for s in done}
def test_cross_device_progress_attached(states: list) -> None:
sbb = next(s for s in states if s.title == "Stone Butch Blues")
assert sbb.progress # device progress present
assert sbb.latest_device == "Kobo"
assert 0.4 < sbb.percent_complete < 0.5
def test_normalize_key_is_stable() -> None:
# Same title with case/spacing/punctuation noise resolves to one key, so a
# Calibre book and its KOReader stat join even when stored slightly differently.
assert normalize_key("The Handmaid's Tale", ("Margaret Atwood",)) == normalize_key(
" the handmaid's TALE ", ["Margaret Atwood"]
)
def test_status_classification() -> None:
book = Book(book_id="b1", title="Half Read", authors=(Author("X"),))
stat = ReadingStat(
key="k",
title="Half Read",
authors=("X",),
pages_read=50,
total_pages=100,
read_time_seconds=600,
last_read_ts=1_700_000_000,
sessions=2,
)
states = unify([book], [stat], None)
assert states[0].status is ReadingStatus.READING
def test_unread_book_with_no_stats() -> None:
book = Book(book_id="b1", title="Untouched", authors=(Author("X"),))
states = unify([book], [], None)
assert states[0].status is ReadingStatus.UNREAD
assert states[0].percent_complete == 0.0
def test_stat_without_calibre_book_is_surfaced() -> None:
"""A book read in KOReader but absent from Calibre still appears."""
stat = ReadingStat(
key="k",
title="Sideloaded Zine",
authors=("Zinester",),
pages_read=30,
total_pages=30,
read_time_seconds=900,
last_read_ts=1_700_000_000,
sessions=1,
)
states = unify([], [stat], None)
assert len(states) == 1
assert states[0].title == "Sideloaded Zine"
assert states[0].book is None
assert states[0].status is ReadingStatus.FINISHED
class _RaisingSource:
"""A ProgressSource stand-in for a live client — never given to unify() anymore."""
def progress_for(self, document: str) -> None:
raise RuntimeError("network is down")
class _MapSource:
"""The shape unify() now actually consumes: an already-resolved in-memory map."""
def __init__(self, progress: dict[str, DeviceProgress]) -> None:
self._progress = progress
def progress_for(self, document: str): # noqa: ANN201 - mirrors ProgressSource.progress_for
return self._progress.get(document)
def test_unify_no_longer_swallows_progress_source_errors() -> None:
"""FIX-08: fetching (and any error capture) happens upstream in fetch_progress now.
unify() must not catch a ProgressSource error itself — it should propagate,
proving the old blanket ``except Exception: return ()`` is gone.
"""
book = Book(book_id="b1", title="Half Read", authors=(Author("X"),))
stat = ReadingStat(
key="k",
title="Half Read",
authors=("X",),
pages_read=50,
total_pages=100,
read_time_seconds=600,
last_read_ts=1_700_000_000,
sessions=2,
)
with pytest.raises(RuntimeError, match="network is down"):
unify([book], [stat], _RaisingSource())
def test_unify_reads_resolved_progress_map_without_network() -> None:
"""unify() just does a lookup against an already-resolved map — no fetching."""
book = Book(book_id="b1", title="Half Read", authors=(Author("X"),))
stat = ReadingStat(
key="k",
title="Half Read",
authors=("X",),
pages_read=50,
total_pages=100,
read_time_seconds=600,
last_read_ts=1_700_000_000,
sessions=2,
)
dp = DeviceProgress(document="k", percentage=0.75, device="Kobo", timestamp=1_700_000_100)
states = unify([book], [stat], _MapSource({"k": dp}))
assert states[0].progress == (dp,)
assert states[0].percent_complete == 0.75