forked from asg017/sqlite-html
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
176 lines (150 loc) · 5.36 KB
/
test.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import sqlite3
import unittest
import os
EXT_PATH = "dist/html0-{OS}".format(OS=os.environ.get("OS"))
db = sqlite3.connect(":memory:")
db.execute("create table fbefore as select name from pragma_function_list")
db.execute("create table mbefore as select name from pragma_module_list")
db.enable_load_extension(True)
db.load_extension(EXT_PATH)
db.execute("create temp table fafter as select name from pragma_function_list")
db.execute("create temp table mafter as select name from pragma_module_list")
class TestHtml(unittest.TestCase):
def test_funcs(self):
funcs = list(map(lambda a: a[0], db.execute("select name from fafter where name not in (select name from fbefore) order by name").fetchall()))
self.assertEqual(funcs, [
"html",
"html_attr_get",
"html_attr_has",
"html_attribute_get",
"html_attribute_has",
"html_count",
"html_debug",
"html_element",
"html_escape",
"html_extract",
"html_table",
"html_text",
"html_trim",
"html_unescape",
"html_version",
])
def test_modules(self):
funcs = list(map(lambda a: a[0], db.execute("select name from mafter where name not in (select name from mbefore) order by name").fetchall()))
self.assertEqual(funcs, [
"html_each",
])
def test_http_version(self):
v, = db.execute("select html_version()").fetchone()
self.assertEqual(v, "v0.0.0")
def test_html_debug(self):
d, = db.execute("select html_debug()").fetchone()
lines = d.splitlines()
self.assertEqual(len(lines), 4)
self.assertTrue(lines[0].startswith("Version"))
self.assertTrue(lines[1].startswith("Commit"))
self.assertTrue(lines[2].startswith("Runtime"))
self.assertTrue(lines[3].startswith("Date"))
def test_html_table(self):
d, = db.execute("select html_table('a')").fetchone()
self.assertEqual(d, "<table>a")
def test_html_escape(self):
d, = db.execute("select html_escape('<a>')").fetchone()
self.assertEqual(d, "<a>")
def test_html_unescape(self):
d, = db.execute("select html_unescape('<a')").fetchone()
self.assertEqual(d, "<a")
def test_html_trim(self):
a,b = db.execute("""select html_trim(' a '), html_trim('
bb
')""").fetchone()
self.assertEqual(a, "a")
self.assertEqual(b, "bb")
def test_html(self):
a, b, c = db.execute("select html('a'), html('<p>b'), html('<ohno');").fetchone()
self.assertEqual(a, "a")
self.assertEqual(b, "<p>b</p>")
# TODO what should this do
self.assertEqual(c, None)
def test_html_element(self):
a, b, c = db.execute("""select
html_element('p', null, "ayoo"),
html_element('p', json_object("class", "cool"), "ayoo"),
html_element(
'p',
null,
html_element("span", null, "My name is "),
html_element("b", null, "Alex Garcia"),
"."
)
""").fetchone()
self.assertEqual(a, "<p>ayoo</p>")
self.assertEqual(b, "<p class=\"cool\">ayoo</p>")
# TODO what should this do
self.assertEqual(c, "<p><span>My name is </span><b>Alex Garcia</b>.</p>")
def test_html_attribute_has(self):
a, b, c = db.execute("""select
html_attribute_has('<p x>', 'p', 'x'),
html_attribute_has('<p>abc', 'p', 'x'),
html_attr_has('<p> <span z>yo', 'span', 'z' )
""").fetchone()
self.assertEqual(a, 1)
self.assertEqual(b, 0)
self.assertEqual(c, 1)
def test_html_attribute_get(self):
a, b, c = db.execute("""select
html_attribute_get('<p x>', 'p', 'x'),
html_attribute_get('<p>abc', 'p', 'x'),
html_attr_get('<p> <span x=z>yo', 'span', 'x' )
""").fetchone()
self.assertEqual(a, None)
self.assertEqual(b, None)
self.assertEqual(c, "z")
def test_html_extract(self):
a, b, c = db.execute("""select
html_extract('<div> asdfasdf <p a=b>abc</p> asdfasdf </div>', 'p'),
html_extract('<p>abc', 'p'),
html_extract('<p> <b>asdf</b></p>', 'x')
""").fetchone()
self.assertEqual(a, "<p a=\"b\">abc</p>")
self.assertEqual(b, "<p>abc</p>")
self.assertEqual(c, None)
def test_html_text(self):
a, b, c = db.execute("""select
html_text('<div> asdfasdf <p a=b>abc</p> asdfasdf </div>', 'p'),
html_text('<p>abc', 'p'),
html_text('<p> <b>asdf</b></p>', 'x')
""").fetchone()
self.assertEqual(a, "abc")
self.assertEqual(b, "abc")
self.assertEqual(c, None)
def test_html_count(self):
a, b, c = db.execute("""select
html_count('<div> ', 'p'),
html_count('<div> <p>abc', 'p'),
html_count('<div> <p class=x></p> <p>a</p> <p class=x>', 'p.x')
""").fetchone()
self.assertEqual(a, 0)
self.assertEqual(b, 1)
self.assertEqual(c, 2)
def test_html_each(self):
rows = db.execute("""select *
from html_each('<div>
<p>a</p>
<p id=x>b</p>
<p>c1<span>c2</span></p>
', 'p')
""").fetchall()
self.assertEqual(len(rows), 3)
self.assertEqual(len(rows[0]), 3)
self.assertEqual(rows[0][0], 0)
self.assertEqual(rows[0][1], "<p>a</p>")
self.assertEqual(rows[0][2], "a")
self.assertEqual(rows[1][0], 1)
self.assertEqual(rows[1][1], "<p id=\"x\">b</p>")
self.assertEqual(rows[1][2], "b")
self.assertEqual(rows[2][0], 2)
self.assertEqual(rows[2][1], "<p>c1<span>c2</span></p>")
self.assertEqual(rows[2][2], "c1c2")
if __name__ == '__main__':
unittest.main()