-
-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Expand file tree
/
Copy pathtest_arxiv_retriever.py
More file actions
147 lines (107 loc) · 4.94 KB
/
test_arxiv_retriever.py
File metadata and controls
147 lines (107 loc) · 4.94 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
"""Tests for ArxivRetriever."""
import time
from types import SimpleNamespace
import feedparser
import pytest
from zotero_arxiv_daily.retriever.arxiv_retriever import ArxivRetriever, _run_with_hard_timeout
import zotero_arxiv_daily.retriever.arxiv_retriever as arxiv_retriever
def _sleep_and_return(value: str, delay_seconds: float) -> str:
time.sleep(delay_seconds)
return value
def _raise_runtime_error() -> None:
raise RuntimeError("boom")
def test_arxiv_retriever(config, mock_feedparser, monkeypatch):
monkeypatch.setattr("zotero_arxiv_daily.retriever.base.sleep", lambda _: None)
# The RSS fixture gives us paper IDs. After feedparser, the code calls
# arxiv.Client().results(search) which makes real HTTP requests. We mock
# the arxiv Client so the test stays offline.
new_entries = [
e for e in mock_feedparser.entries
if e.get("arxiv_announce_type", "new") == "new"
]
paper_ids = [e.id.removeprefix("oai:arXiv.org:") for e in new_entries]
# Build fake ArxivResult-like objects matching each RSS entry
fake_results = []
for entry in new_entries:
pid = entry.id.removeprefix("oai:arXiv.org:")
fake_results.append(SimpleNamespace(
title=entry.title,
authors=[SimpleNamespace(name="Test Author")],
summary="Test abstract",
pdf_url=f"https://arxiv.org/pdf/{pid}",
entry_id=f"https://arxiv.org/abs/{pid}",
source_url=lambda pid=pid: f"https://arxiv.org/e-print/{pid}",
))
class FakeClient:
def __init__(self, **kw):
pass
def results(self, search):
return iter(fake_results)
monkeypatch.setattr(arxiv_retriever.arxiv, "Client", FakeClient)
# Skip file downloads in convert_to_paper
monkeypatch.setattr(arxiv_retriever, "extract_text_from_html", lambda paper: None)
monkeypatch.setattr(arxiv_retriever, "extract_text_from_pdf", lambda paper: None)
monkeypatch.setattr(arxiv_retriever, "extract_text_from_tar", lambda paper: None)
retriever = ArxivRetriever(config)
papers = retriever.retrieve_papers()
assert len(papers) == len(new_entries)
assert set(p.title for p in papers) == set(e.title for e in new_entries)
def test_run_with_hard_timeout_returns_value():
result = _run_with_hard_timeout(
_sleep_and_return, ("done", 0.01), timeout=1, operation="test op", paper_title="paper"
)
assert result == "done"
def test_run_with_hard_timeout_returns_none_on_timeout(monkeypatch):
warnings: list[str] = []
monkeypatch.setattr(arxiv_retriever, "logger", SimpleNamespace(warning=warnings.append))
result = _run_with_hard_timeout(
_sleep_and_return, ("done", 1.0), timeout=0.01, operation="test op", paper_title="paper"
)
assert result is None
assert "timed out" in warnings[0]
def test_run_with_hard_timeout_returns_none_on_failure(monkeypatch):
warnings: list[str] = []
monkeypatch.setattr(arxiv_retriever, "logger", SimpleNamespace(warning=warnings.append))
result = _run_with_hard_timeout(
_raise_runtime_error, (), timeout=1, operation="test op", paper_title="paper"
)
assert result is None
assert "boom" in warnings[0]
def test_retrieve_raw_papers_skips_batch_after_retryable_http_error(config, mock_feedparser, monkeypatch):
from omegaconf import open_dict
with open_dict(config):
config.executor.debug = True
monkeypatch.setattr(arxiv_retriever, "sleep", lambda _: None)
warnings: list[str] = []
monkeypatch.setattr(arxiv_retriever, "logger", SimpleNamespace(warning=warnings.append))
class FakeHTTPError(Exception):
def __init__(self, status):
self.status = status
class FakeClient:
def __init__(self, **kw):
pass
def results(self, search):
raise FakeHTTPError(503)
monkeypatch.setattr(arxiv_retriever.arxiv, "HTTPError", FakeHTTPError)
monkeypatch.setattr(arxiv_retriever.arxiv, "Client", FakeClient)
retriever = ArxivRetriever(config)
assert retriever._retrieve_raw_papers() == []
assert any("Skipping batch 0 after 5 retries due to arXiv API 503" in warning for warning in warnings)
def test_retrieve_raw_papers_raises_non_retryable_http_error(config, mock_feedparser, monkeypatch):
from omegaconf import open_dict
with open_dict(config):
config.executor.debug = True
monkeypatch.setattr(arxiv_retriever, "sleep", lambda _: None)
class FakeHTTPError(Exception):
def __init__(self, status):
self.status = status
class FakeClient:
def __init__(self, **kw):
pass
def results(self, search):
raise FakeHTTPError(400)
monkeypatch.setattr(arxiv_retriever.arxiv, "HTTPError", FakeHTTPError)
monkeypatch.setattr(arxiv_retriever.arxiv, "Client", FakeClient)
retriever = ArxivRetriever(config)
with pytest.raises(FakeHTTPError):
retriever._retrieve_raw_papers()