-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_rendering.py
More file actions
229 lines (193 loc) · 7.26 KB
/
test_rendering.py
File metadata and controls
229 lines (193 loc) · 7.26 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
"""Unit tests for bibliography rendering helpers."""
import pytest
from lit_agent.identifiers import (
CitationResolutionResult,
render_bibliography_to_strings,
)
@pytest.mark.unit
def test_render_bibliography_fallback(monkeypatch):
"""Render should fall back to compact mode when citeproc is unavailable."""
def raise_import_error():
raise ImportError("citeproc not installed")
monkeypatch.setattr(
"lit_agent.identifiers.api._import_citeproc",
raise_import_error,
)
result = CitationResolutionResult(
citations={
"1": {
"id": "1",
"title": "Example Paper",
"author": [{"family": "Doe"}],
"issued": {"date-parts": [[2024]]},
"DOI": "10.1234/example",
"resolution": {},
"URL": "https://example.com",
},
},
stats={},
failures=[],
)
rendered, meta = render_bibliography_to_strings(result, style="vancouver")
assert rendered[0].startswith("[1] Doe Example Paper 2024 10.1234/example")
assert meta["renderer"] == "fallback"
assert meta["style"] == "vancouver"
@pytest.mark.unit
def test_render_bibliography_vancouver_style():
"""Test proper bibliography rendering with vancouver style."""
result = CitationResolutionResult(
citations={
"1": {
"id": "1",
"title": "Example Paper on Glioblastoma",
"author": [{"family": "Doe", "given": "John"}],
"issued": {"date-parts": [[2024]]},
"container-title": "Nature",
"DOI": "10.1234/example",
"resolution": {"method": "doi"},
"URL": "https://example.com",
},
},
stats={"total": 1, "resolved": 1},
failures=[],
)
rendered, meta = render_bibliography_to_strings(result, style="vancouver")
# Should use citeproc renderer with citeproc-py-styles installed
assert meta["renderer"] == "citeproc-py", (
"Expected citeproc-py renderer but got fallback. "
"Ensure citeproc-py-styles is installed: pip install citeproc-py-styles"
)
assert meta["style"] == "vancouver"
assert meta["locale"] == "en-US"
# Bibliography should be properly formatted and non-empty
assert len(rendered) == 1, "Should have one bibliography entry"
assert rendered[0], "Bibliography entry should not be empty"
assert "Doe" in rendered[0], "Author name should appear in citation"
assert "2024" in rendered[0], "Publication year should appear"
@pytest.mark.unit
def test_render_bibliography_apa_style():
"""Test bibliography rendering with APA style."""
result = CitationResolutionResult(
citations={
"1": {
"id": "1",
"title": "Machine Learning in Cancer Research",
"author": [
{"family": "Smith", "given": "Jane"},
{"family": "Johnson", "given": "Bob"},
],
"issued": {"date-parts": [[2023]]},
"container-title": "Cell",
"DOI": "10.5678/test",
"resolution": {"method": "doi"},
},
},
stats={"total": 1, "resolved": 1},
failures=[],
)
rendered, meta = render_bibliography_to_strings(result, style="apa")
assert meta["renderer"] == "citeproc-py", (
"Expected citeproc-py renderer. Ensure citeproc-py-styles is installed."
)
assert meta["style"] == "apa"
assert len(rendered) == 1
assert "Smith" in rendered[0]
assert "2023" in rendered[0]
@pytest.mark.unit
def test_render_bibliography_chicago_style():
"""Test bibliography rendering with Chicago style."""
result = CitationResolutionResult(
citations={
"1": {
"id": "1",
"title": "The Role of Inflammation in Disease",
"author": [{"family": "Brown", "given": "Alice"}],
"issued": {"date-parts": [[2022]]},
"container-title": "Science",
"DOI": "10.9999/chicago-test",
"resolution": {"method": "doi"},
},
},
stats={"total": 1, "resolved": 1},
failures=[],
)
rendered, meta = render_bibliography_to_strings(result, style="chicago")
assert meta["renderer"] == "citeproc-py"
assert meta["style"] == "chicago"
assert len(rendered) == 1
assert rendered[0] # Non-empty
@pytest.mark.unit
def test_render_bibliography_multiple_citations():
"""Test rendering multiple citations in correct order."""
result = CitationResolutionResult(
citations={
"1": {
"id": "1",
"title": "First Paper",
"author": [{"family": "Alpha", "given": "A"}],
"issued": {"date-parts": [[2021]]},
"DOI": "10.1111/first",
},
"2": {
"id": "2",
"title": "Second Paper",
"author": [{"family": "Beta", "given": "B"}],
"issued": {"date-parts": [[2022]]},
"DOI": "10.2222/second",
},
"3": {
"id": "3",
"title": "Third Paper",
"author": [{"family": "Gamma", "given": "C"}],
"issued": {"date-parts": [[2023]]},
"DOI": "10.3333/third",
},
},
stats={"total": 3, "resolved": 3},
failures=[],
)
rendered, meta = render_bibliography_to_strings(result, style="vancouver")
assert meta["renderer"] == "citeproc-py"
assert len(rendered) == 3, "Should have three bibliography entries"
# Check all entries are non-empty
for entry in rendered:
assert entry, "Each bibliography entry should be non-empty"
# Check authors appear in their respective entries
assert "Alpha" in rendered[0]
assert "Beta" in rendered[1]
assert "Gamma" in rendered[2]
@pytest.mark.unit
def test_render_bibliography_empty_citations():
"""Test rendering with no citations."""
result = CitationResolutionResult(
citations={},
stats={"total": 0, "resolved": 0},
failures=[],
)
rendered, meta = render_bibliography_to_strings(result, style="vancouver")
# Even with no citations, should use citeproc if available
assert meta["renderer"] in ["citeproc-py", "fallback"]
assert meta["style"] == "vancouver"
assert len(rendered) == 0, "Should have no bibliography entries"
@pytest.mark.unit
def test_render_bibliography_with_locale():
"""Test bibliography rendering with different locale."""
result = CitationResolutionResult(
citations={
"1": {
"id": "1",
"title": "Example Paper",
"author": [{"family": "Doe", "given": "John"}],
"issued": {"date-parts": [[2024]]},
"DOI": "10.1234/example",
},
},
stats={"total": 1, "resolved": 1},
failures=[],
)
rendered, meta = render_bibliography_to_strings(
result, style="vancouver", locale="en-GB"
)
assert meta["renderer"] == "citeproc-py"
assert meta["locale"] == "en-GB"
assert len(rendered) == 1