forked from ChelseaKR/queer-the-stacks
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_diversity.py
More file actions
330 lines (265 loc) · 12.3 KB
/
Copy pathtest_diversity.py
File metadata and controls
330 lines (265 loc) · 12.3 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
"""Diverse-shelf analytics — sourced-only, honest coverage, no author labels."""
from __future__ import annotations
from pathlib import Path
import pytest
from app.diversity import (
BUILTIN_LENS_SOURCE,
DEFAULT_DIMENSIONS,
DIMENSIONS,
SENSITIVE_DESCRIPTORS,
SENSITIVE_DIMENSIONS,
LensValidationError,
compute_diversity,
load_dimensions,
load_lens_config,
validate_dimensions,
)
from ingest.models import (
Author,
Book,
ReadingStat,
ReadingState,
ReadingStatus,
Source,
SourceKind,
ThemeTag,
)
def _tag(label: str, kind: SourceKind = SourceKind.CALIBRE_TAG) -> ThemeTag:
return ThemeTag(label, Source(kind, "calibre:local", "2026-06-05", label))
def _state(
title: str,
status: ReadingStatus,
tags: tuple[ThemeTag, ...],
) -> ReadingState:
book = Book(book_id=title, title=title, authors=(Author("A"),), theme_tags=tags)
stat = ReadingStat(title, title, ("A",), 100, 100, 3600, 1_700_000_000, 3)
return ReadingState(title=title, authors=("A",), status=status, book=book, stat=stat)
def test_excludes_unread_and_counts_described() -> None:
states = [
_state("Read trans", ReadingStatus.FINISHED, (_tag("trans"), _tag("literary"))),
_state("Reading queer", ReadingStatus.READING, (_tag("queer"),)),
_state("No tags", ReadingStatus.FINISHED, ()),
_state("Unread queer", ReadingStatus.UNREAD, (_tag("queer"),)), # excluded
]
report = compute_diversity(states)
assert report.total_books == 3 # the unread one is not considered
assert report.described_books == 2 # "No tags" carries no sourced descriptor
assert report.undescribed_books == 1
assert round(report.coverage_pct, 3) == round(2 / 3, 3)
def test_dimensions_group_sourced_descriptors_only() -> None:
states = [
_state("A", ReadingStatus.FINISHED, (_tag("trans"),)),
_state("B", ReadingStatus.FINISHED, (_tag("queer"),)),
_state("C", ReadingStatus.FINISHED, (_tag("speculative"),)),
]
report = compute_diversity(states)
by_name = {d.name: d for d in report.dimensions}
assert by_name["Trans & nonbinary"].books == 1
assert by_name["Queer / LGBTQ+"].books == 1
assert by_name["Speculative / SFF"].books == 1
# % is a share of *described* books, never the whole shelf.
assert round(by_name["Trans & nonbinary"].pct, 3) == round(1 / 3, 3)
# The concrete sourced labels are surfaced for transparency.
assert by_name["Trans & nonbinary"].matched_labels == ("trans",)
def test_empty_lenses_are_omitted() -> None:
report = compute_diversity([_state("A", ReadingStatus.FINISHED, (_tag("trans"),))])
names = {d.name for d in report.dimensions}
assert names == {"Trans & nonbinary"} # only populated lenses surface
def test_provenance_counts_by_source_kind() -> None:
states = [
_state("A", ReadingStatus.FINISHED, (_tag("trans", SourceKind.CALIBRE_TAG),)),
_state(
"B",
ReadingStatus.FINISHED,
(_tag("queer", SourceKind.OPENLIBRARY_SUBJECT),),
),
]
report = compute_diversity(states)
prov = dict(report.source_provenance)
assert prov["calibre-tag"] == 1
assert prov["openlibrary-subject"] == 1
def test_empty_shelf_is_safe() -> None:
report = compute_diversity([])
assert report.total_books == 0
assert report.coverage_pct == 0.0
assert report.dimensions == ()
def test_dimensions_constant_has_no_author_identity_intent() -> None:
"""The lens grouping describes books; its names must not label a person."""
for name, labels in DIMENSIONS:
assert name and labels
# Lenses are descriptors of works, never claims about an author.
assert "author" not in name.lower()
def test_demo_diversity_reflects_the_canon(states: list) -> None:
report = compute_diversity(states)
assert report.described_books >= 7
by_name = {d.name: d for d in report.dimensions}
assert by_name["Trans & nonbinary"].books >= 3
assert by_name["Speculative / SFF"].books >= 3
# --- R4: per-descriptor provenance + the privacy (hide-sensitive) toggle -------
def test_descriptor_provenance_carries_source_and_retrieved_at() -> None:
"""Every diverse-shelf tag exposes its Source kind, citation, and fetch date."""
states = [_state("A", ReadingStatus.FINISHED, (_tag("literary"), _tag("trans")))]
report = compute_diversity(states)
by_label = {d.label: d for d in report.descriptor_provenance}
lit = by_label["literary"]
assert lit.source_kinds == ("calibre-tag",)
assert lit.latest_retrieved_at == "2026-06-05"
assert lit.sources[0].citation == "calibre:local"
assert lit.sensitive is False
# "trans" is identity-adjacent and flagged sensitive (but still shown by default).
assert by_label["trans"].sensitive is True
assert report.hide_sensitive is False
def test_descriptor_provenance_unions_multiple_sources() -> None:
states = [
_state("A", ReadingStatus.FINISHED, (_tag("queer", SourceKind.CALIBRE_TAG),)),
_state("B", ReadingStatus.FINISHED, (_tag("queer", SourceKind.OPENLIBRARY_SUBJECT),)),
]
report = compute_diversity(states)
queer = next(d for d in report.descriptor_provenance if d.label == "queer")
assert queer.books == 2
assert queer.source_kinds == ("calibre-tag", "openlibrary-subject")
def test_hide_sensitive_aggregates_identity_descriptors() -> None:
states = [
_state("A", ReadingStatus.FINISHED, (_tag("trans"), _tag("literary"))),
_state("B", ReadingStatus.FINISHED, (_tag("queer"),)),
]
report = compute_diversity(states, hide_sensitive=True)
labels = {d.label for d in report.descriptor_provenance}
# Granular identity labels are gone; the non-sensitive one stays.
assert "trans" not in labels and "queer" not in labels
assert "literary" in labels
# Exactly one aggregated stand-in row, counting distinct books, keeping provenance.
agg = [d for d in report.descriptor_provenance if d.aggregated]
assert len(agg) == 1
assert agg[0].sensitive and agg[0].books == 2
assert agg[0].source_kinds == ("calibre-tag",)
# Coarse lens counts remain, but their concrete labels are masked.
by_name = {d.name: d for d in report.dimensions}
assert by_name["Trans & nonbinary"].books == 1
assert by_name["Trans & nonbinary"].matched_labels == ("(hidden for privacy)",)
# The flat theme breakdown also redacts the granular sensitive labels.
tb = dict(report.theme_breakdown)
assert "trans" not in tb and "queer" not in tb
assert report.hide_sensitive is True
def test_hide_sensitive_keeps_nonsensitive_detail() -> None:
states = [_state("A", ReadingStatus.FINISHED, (_tag("speculative"), _tag("literary")))]
report = compute_diversity(states, hide_sensitive=True)
labels = {d.label for d in report.descriptor_provenance}
assert {"speculative", "literary"} <= labels
# No sensitive descriptors present, so no aggregated row is synthesised.
assert not any(d.aggregated for d in report.descriptor_provenance)
def test_sensitive_descriptors_are_identity_adjacent() -> None:
assert {"trans", "queer"} <= SENSITIVE_DESCRIPTORS
# Descriptors of works (not outing identity labels) are never sensitive.
assert "speculative" not in SENSITIVE_DESCRIPTORS
assert "literary" not in SENSITIVE_DESCRIPTORS
# The sensitive lenses are a subset of the published, auditable dimensions.
dimension_names = {name for name, _ in DIMENSIONS}
assert SENSITIVE_DIMENSIONS.issubset(dimension_names)
def test_dimensions_alias_matches_default() -> None:
assert DIMENSIONS is DEFAULT_DIMENSIONS
def test_default_lens_source_is_builtin() -> None:
report = compute_diversity([_state("A", ReadingStatus.FINISHED, (_tag("trans"),))])
assert report.lens_source == BUILTIN_LENS_SOURCE
assert report.lens_warning is None
def test_custom_dimensions_reflect_renamed_lens_labels() -> None:
"""A caller-supplied lens grouping is used verbatim — a renamed label shows up."""
custom = (("Trans Futures", frozenset({"trans"})),)
states = [_state("A", ReadingStatus.FINISHED, (_tag("trans"),))]
report = compute_diversity(states, custom, lens_source="data/lenses.toml")
names = {d.name for d in report.dimensions}
assert names == {"Trans Futures"}
assert report.lens_source == "data/lenses.toml"
def test_validate_dimensions_rejects_duplicate_labels() -> None:
dims = (
("Queer", frozenset({"queer"})),
("queer", frozenset({"lgbtq"})), # case-insensitive duplicate
)
with pytest.raises(LensValidationError, match="duplicate"):
validate_dimensions(dims)
def test_validate_dimensions_rejects_empty_descriptors() -> None:
dims = (("Empty Lens", frozenset()),)
with pytest.raises(LensValidationError, match="no descriptors"):
validate_dimensions(dims)
def test_validate_dimensions_rejects_empty_name() -> None:
dims = ((" ", frozenset({"trans"})),)
with pytest.raises(LensValidationError, match="name"):
validate_dimensions(dims)
def test_load_dimensions_from_records() -> None:
records: list[dict[str, object]] = [
{"name": "Trans & nonbinary", "descriptors": ["Trans", "NONBINARY"]},
{"name": "Queer / LGBTQ+", "descriptors": ["queer", "lesbian"]},
]
dims = load_dimensions(records)
by_name = dict(dims)
# Descriptors are normalized to lowercase to match ThemeTag.normalized.
assert by_name["Trans & nonbinary"] == frozenset({"trans", "nonbinary"})
def test_load_dimensions_rejects_duplicate_labels() -> None:
records: list[dict[str, object]] = [
{"name": "Queer", "descriptors": ["queer"]},
{"name": "queer", "descriptors": ["lgbtq"]},
]
with pytest.raises(LensValidationError, match="duplicate"):
load_dimensions(records)
def test_load_dimensions_rejects_empty_descriptors() -> None:
with pytest.raises(LensValidationError, match="no descriptors"):
load_dimensions([{"name": "Empty", "descriptors": []}])
def test_load_lens_config_none_uses_defaults_with_no_warning() -> None:
cfg = load_lens_config(None)
assert cfg.dimensions == DEFAULT_DIMENSIONS
assert cfg.source == BUILTIN_LENS_SOURCE
assert cfg.warning is None
def test_load_lens_config_valid_file(tmp_path: Path) -> None:
toml = tmp_path / "lenses.toml"
toml.write_text(
"""
[[lenses]]
name = "Trans Futures"
descriptors = ["trans", "nonbinary"]
"""
)
cfg = load_lens_config(toml)
assert cfg.warning is None
assert cfg.source == str(toml)
assert dict(cfg.dimensions)["Trans Futures"] == frozenset({"trans", "nonbinary"})
def test_load_lens_config_missing_file_degrades(tmp_path: Path) -> None:
cfg = load_lens_config(tmp_path / "absent.toml")
assert cfg.dimensions == DEFAULT_DIMENSIONS
assert cfg.source == BUILTIN_LENS_SOURCE
assert cfg.warning is not None # visible, never a silent fallback
def test_load_lens_config_malformed_toml_degrades(tmp_path: Path) -> None:
toml = tmp_path / "lenses.toml"
toml.write_text("this is not [valid toml")
cfg = load_lens_config(toml)
assert cfg.dimensions == DEFAULT_DIMENSIONS
assert cfg.warning is not None
def test_load_lens_config_duplicate_labels_degrade_with_warning(tmp_path: Path) -> None:
toml = tmp_path / "lenses.toml"
toml.write_text(
"""
[[lenses]]
name = "Queer"
descriptors = ["queer"]
[[lenses]]
name = "queer"
descriptors = ["lgbtq"]
"""
)
cfg = load_lens_config(toml)
# Invalid config never blocks the view: it degrades to defaults, named.
assert cfg.dimensions == DEFAULT_DIMENSIONS
assert cfg.source == BUILTIN_LENS_SOURCE
assert cfg.warning is not None
assert "duplicate" in cfg.warning.lower()
def test_load_lens_config_empty_lenses_array_degrades(tmp_path: Path) -> None:
toml = tmp_path / "lenses.toml"
toml.write_text("lenses = []\n")
cfg = load_lens_config(toml)
assert cfg.dimensions == DEFAULT_DIMENSIONS
assert cfg.warning is not None
def test_the_committed_lenses_toml_template_is_valid() -> None:
"""The shipped data/lenses.toml must load cleanly to defaults' equivalent."""
repo_root = Path(__file__).resolve().parent.parent
cfg = load_lens_config(repo_root / "data" / "lenses.toml")
assert cfg.warning is None
assert cfg.dimensions == DEFAULT_DIMENSIONS