-
Notifications
You must be signed in to change notification settings - Fork 306
Expand file tree
/
Copy pathtest_web.py
More file actions
43 lines (31 loc) · 1.31 KB
/
Copy pathtest_web.py
File metadata and controls
43 lines (31 loc) · 1.31 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
"""Tests for the web API wrapper."""
import importlib.util
from pathlib import Path
from types import SimpleNamespace
import unittest
from unittest.mock import Mock, patch
def load_web_api():
module_path = Path(__file__).resolve().parents[2] / "office" / "api" / "web.py"
spec = importlib.util.spec_from_file_location("web_api_under_test", module_path)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
return module
class TestWeb(unittest.TestCase):
def test_url2ebook_delegates_to_pospider(self):
web = load_web_api()
url2ebook = Mock()
pospider = SimpleNamespace(url=SimpleNamespace(url2ebook=url2ebook))
with patch.object(web, "_load_pospider", return_value=pospider):
web.url2ebook("https://example.com/article", tile="测试")
url2ebook.assert_called_once_with(
url="https://example.com/article",
tile="测试",
)
def test_loader_reports_incompatible_pospider(self):
web = load_web_api()
with patch("builtins.__import__", return_value=SimpleNamespace()):
with self.assertRaises(ImportError) as error:
web._load_pospider()
self.assertIn("不提供 url.url2ebook", str(error.exception))
if __name__ == "__main__":
unittest.main()