-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_plugin_internallinks.py
112 lines (89 loc) · 3.31 KB
/
test_plugin_internallinks.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
from unittest.mock import Mock, patch
from n2y.blocks import ChildPageBlock, HeadingOneBlock, ParagraphBlock
from n2y.notion import Client
from n2y.notion_mocks import (
mock_block,
mock_heading_block,
mock_id,
mock_page,
mock_rich_text,
mock_user,
)
from n2y.page import Page
from n2y.plugins.internallinks import (
find_target_block,
get_notion_id_from_href,
is_internal_link,
)
from n2y.user import User
from n2y.utils import header_id_from_text, pandoc_ast_to_markdown
def test_get_notion_id_from_href_simple():
uid = mock_id()
assert get_notion_id_from_href(f"/1234#{uid}") == uid
def test_get_notion_id_from_href_missing_fragment():
assert get_notion_id_from_href("/1234") is None
def test_get_notion_id_from_href_fragment_is_not_uid():
assert get_notion_id_from_href("/1234#foo") is None
def test_is_internal_link():
assert is_internal_link("/1234#5678", "1234")
def test_is_not_internal_link():
assert not is_internal_link("http://silverspoonscollectors.org/foo#bar", "1234")
def test_is_internal_link_missing_href():
assert not is_internal_link(None, "1234")
@patch("n2y.notion.Client.wrap_notion_user")
def mock_page_with_link_to_header(
wrap_notion_user, header_text: str = "nice header", link_text: str = "nice link"
) -> Page:
client = Client("", plugins=["n2y.plugins.internallinks"])
wrap_notion_user.return_value = User(client, mock_user())
page = Page(client, notion_data=mock_page())
page_block = ChildPageBlock(
client=client,
notion_data=mock_block("child_page", {"title": "Mock Page"}),
page=page,
get_children=False,
)
# HACK: should preferably mock Client.get_block, not set private attr
page._block = page_block
heading_block = HeadingOneBlock(
client=client,
notion_data=mock_heading_block(header_text, level=1),
page=page,
get_children=False,
)
paragraph_with_link_block = ParagraphBlock(
client=client,
notion_data=mock_block(
"paragraph",
{
"rich_text": [
mock_rich_text(text="some random text"),
mock_rich_text(
text=link_text,
href=(
f"/{page.notion_id.replace('-', '')}"
f"#{heading_block.notion_id.replace('-', '')}"
),
),
]
},
),
page=page,
get_children=False,
)
page.block.children = [heading_block, paragraph_with_link_block]
return page, heading_block, paragraph_with_link_block
def test_find_target_block():
page, heading, paragraph = mock_page_with_link_to_header()
assert find_target_block(page.block, target_id=heading.notion_id) is heading
assert find_target_block(page.block, target_id=paragraph.notion_id) is paragraph
def test_internal_link_to_pandoc():
header_text = "A Very Fine Title"
link_text = "A Very Poor Link Text"
page, _, _ = mock_page_with_link_to_header(
header_text=header_text,
link_text=link_text,
)
markdown = pandoc_ast_to_markdown(page.to_pandoc(), Mock())
assert f"# {header_text}" in markdown
assert f"[{link_text}](#{header_id_from_text(header_text)})" in markdown