-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathconftest.py
95 lines (75 loc) · 2.53 KB
/
conftest.py
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
from collections.abc import Callable, Generator
import pytest
from zimscraperlib.rewriting.css import CssRewriter
from zimscraperlib.rewriting.js import JsRewriter
from zimscraperlib.rewriting.url_rewriting import (
ArticleUrlRewriter,
HttpUrl,
RewriteResult,
ZimPath,
)
class SimpleUrlRewriter(ArticleUrlRewriter):
"""Basic URL rewriter mocking most calls"""
def __init__(self, article_url: HttpUrl, suffix: str = ""):
self.article_url = article_url
self.suffix = suffix
def __call__(
self,
item_url: str,
base_href: str | None, # noqa: ARG002
*,
rewrite_all_url: bool = True, # noqa: ARG002
) -> RewriteResult:
return RewriteResult(
absolute_url=item_url + self.suffix,
rewriten_url=item_url + self.suffix,
zim_path=None,
)
def get_item_path(
self, item_url: str, base_href: str | None # noqa: ARG002
) -> ZimPath:
return ZimPath("")
def get_document_uri(
self, item_path: ZimPath, item_fragment: str # noqa: ARG002
) -> str:
return ""
@pytest.fixture(scope="module")
def simple_url_rewriter_gen() -> Generator[Callable[[str], ArticleUrlRewriter]]:
"""Fixture to create a basic url rewriter returning URLs as-is"""
def get_simple_url_rewriter(url: str, suffix: str = "") -> ArticleUrlRewriter:
return SimpleUrlRewriter(HttpUrl(url), suffix=suffix)
yield get_simple_url_rewriter
@pytest.fixture(scope="module")
def js_rewriter_gen() -> (
Generator[
Callable[
[ArticleUrlRewriter, str | None, Callable[[ZimPath], None]], JsRewriter
]
]
):
"""Fixture to create a basic url rewriter returning URLs as-is"""
def get_js_rewriter(
url_rewriter: ArticleUrlRewriter,
base_href: str | None,
notify_js_module: Callable[[ZimPath], None],
) -> JsRewriter:
return JsRewriter(
url_rewriter=url_rewriter,
base_href=base_href,
notify_js_module=notify_js_module,
)
yield get_js_rewriter
@pytest.fixture(scope="module")
def css_rewriter_gen() -> (
Generator[Callable[[ArticleUrlRewriter, str | None], CssRewriter]]
):
"""Fixture to create a basic url rewriter returning URLs as-is"""
def get_css_rewriter(
url_rewriter: ArticleUrlRewriter,
base_href: str | None,
) -> CssRewriter:
return CssRewriter(
url_rewriter=url_rewriter,
base_href=base_href,
)
yield get_css_rewriter