-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_plugins.py
113 lines (84 loc) · 3.08 KB
/
test_plugins.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
113
"""
Unit tests of the builtin plugins.
"""
import pytest
from n2y.blocks import ParagraphBlock, FencedCodeBlock
from n2y.notion import Client
from n2y.page import Page
from n2y.errors import PluginError, UseNextClass
from n2y.notion_mocks import mock_paragraph_block
from n2y.plugins.mermaid import MermaidFencedCodeBlock
from n2y.plugins.rawcodeblocks import RawFencedCodeBlock
def test_load_plugin_invalid_notion_object():
client = Client("")
with pytest.raises(PluginError) as err:
client.load_plugin({"puppy": str})
assert "puppy" in str(err)
def test_load_plugin_invalid_object_type():
client = Client("")
with pytest.raises(PluginError) as err:
client.load_plugin({"blocks": {"puppy": str}})
assert "block" in str(err)
assert "puppy" in str(err)
def test_load_plugin_valid_page():
client = Client("")
class MyPage(Page):
pass
client.load_plugin({"page": MyPage})
assert client.get_class_list("page") == [Page, MyPage]
def test_get_class_fallthrough():
client = Client("")
class SometimesParagraph(ParagraphBlock):
def __init__(self, client, notion_data, page=None, get_children=False):
super().__init__(client, notion_data, page, get_children)
if self.rich_text.to_plain_text() != "sometimes":
raise UseNextClass()
client.load_plugin({"blocks": {"paragraph": SometimesParagraph}})
sometimes = client.wrap_notion_block(
mock_paragraph_block([("sometimes", {})]), None, False
)
othertimes = client.wrap_notion_block(
mock_paragraph_block([("othertimes", {})]), None, False
)
assert type(sometimes) is SometimesParagraph
assert type(othertimes) is ParagraphBlock
def test_load_plugin_invalid_page_class():
client = Client("")
class MyPage:
pass
with pytest.raises(PluginError) as err:
client.load_plugin({"page": MyPage})
assert "MyPage" in str(err)
def test_load_plugin_valid_block():
client = Client("")
class MyParagraphBlock(ParagraphBlock):
pass
client.load_plugin({"blocks": {"paragraph": MyParagraphBlock}})
assert client.get_class_list("blocks", "paragraph") == [
ParagraphBlock,
MyParagraphBlock,
]
def test_load_plugin_invalid_block_mapping():
client = Client("")
with pytest.raises(PluginError) as err:
client.load_plugin({"blocks": str})
assert "block" in str(err)
def test_load_plugin_invalid_block_class():
client = Client("")
class MyParagraphBlock:
pass
with pytest.raises(PluginError) as err:
client.load_plugin({"blocks": {"paragraph": MyParagraphBlock}})
assert "MyParagraphBlock" in str(err)
def test_load_plugins_overrides_old_plugins():
client = Client("")
client.load_plugins(["n2y.plugins.mermaid"])
assert client.get_class_list("blocks", "code") == [
FencedCodeBlock,
MermaidFencedCodeBlock,
]
client.load_plugins(["n2y.plugins.rawcodeblocks"])
assert client.get_class_list("blocks", "code") == [
FencedCodeBlock,
RawFencedCodeBlock,
]