Repository navigation
Expand file tree
/
Copy pathtest_install_html_meta_data.py
More file actions
85 lines (72 loc) · 2.99 KB
/
Copy pathtest_install_html_meta_data.py
File metadata and controls
85 lines (72 loc) · 2.99 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
import io
import json
from unittest.mock import Mock
from urllib.error import URLError
import pytest
from htmlbook import http_retry
from htmlbook import install_html_meta_data as metadata
from htmlbook.install_html_meta_data import install_html_meta_data
def test_install_html_meta_data_check() -> None:
assert not install_html_meta_data(check=True)
def test_batched_bibliography_and_repeated_rendering(monkeypatch):
entry = {
"bibtag": "A",
"bibtype": "article",
"title": "Title",
"year": "2025",
"url": "paper.pdf",
"isPublic": 1,
}
response = io.BytesIO(json.dumps({"entries": {"A": entry}, "missing": []}).encode())
fetch = Mock(return_value=response)
monkeypatch.setattr(metadata, "urlopen", fetch)
entries = metadata.fetch_bibliography("https://example.org/elib.cgi", ["A", "A"])
assert json.loads(fetch.call_args.args[0].data) == ["A"]
html = '<html><elib>A+A</elib><div id="references"></div></html>'
first = metadata.write_references(entries, html, "chapter.html")
assert first == metadata.write_references(entries, html, "another.html")
assert entries["A"] == entry
assert first.count("<li id=A>") == 1
@pytest.mark.parametrize(
"payload",
[
[],
{"entries": {}, "missing": []},
{"entries": {}, "missing": ["A"]},
{"entries": {"A": {"bibtag": "B"}}, "missing": []},
{"entries": {"A": {}}, "missing": ["A"]},
],
)
def test_invalid_or_missing_bibliography(monkeypatch, payload):
monkeypatch.setattr(
metadata,
"urlopen",
lambda *args, **kwargs: io.BytesIO(json.dumps(payload).encode()),
)
with pytest.raises(RuntimeError):
metadata.fetch_bibliography("https://example.org/elib.cgi", ["A"])
def test_network_failure_precedes_any_writes(monkeypatch):
monkeypatch.setattr(http_retry.time, "sleep", Mock())
def fail(*args, **kwargs):
raise URLError("unavailable")
monkeypatch.setattr(metadata, "urlopen", fail)
write = Mock()
monkeypatch.setattr(metadata, "write_file_as_string", write)
with pytest.raises(RuntimeError, match="Failed to fetch"):
install_html_meta_data()
write.assert_not_called()
def test_bibliography_recovers_from_connection_timeout(monkeypatch):
monkeypatch.setattr(http_retry.time, "sleep", Mock())
entry = {"bibtag": "A", "bibtype": "article", "title": "Title", "year": "2025"}
response = io.BytesIO(json.dumps({"entries": {"A": entry}, "missing": []}).encode())
fetch = Mock(side_effect=[URLError(TimeoutError("timed out")), response])
monkeypatch.setattr(metadata, "urlopen", fetch)
assert metadata.fetch_bibliography("https://example.org/elib.cgi", ["A"]) == {
"A": entry
}
assert fetch.call_count == 2
for invocation in fetch.call_args_list:
assert invocation.kwargs == {"timeout": 30}
assert invocation.args[0].get_method() == "POST"
assert json.loads(invocation.args[0].data) == ["A"]
assert response.closed