-
-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathtest_widgets.py
251 lines (224 loc) · 7.26 KB
/
test_widgets.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
from urllib.parse import parse_qsl
import pytest
from bs4 import BeautifulSoup
from django.core import signing
from django_sql_dashboard.models import Dashboard
from django_sql_dashboard.utils import unsign_sql
def test_default_widget(admin_client, dashboard_db):
response = admin_client.post(
"/dashboard/",
{
"sql": """
SELECT * FROM (
VALUES (1, 'one', 4.5), (2, 'two', 3.6), (3, 'three', 4.1)
) AS t (id, name, size)"""
},
follow=True,
)
html = response.content.decode("utf-8")
soup = BeautifulSoup(html, "html5lib")
assert soup.find("textarea").text == (
"SELECT * FROM (\n"
" VALUES (1, 'one', 4.5), (2, 'two', 3.6), (3, 'three', 4.1)\n"
" ) AS t (id, name, size)"
)
# Copyable area:
assert soup.select("textarea#copyable-0")[0].text == (
"id\tname\tsize\n" "1\tone\t4.5\n" "2\ttwo\t3.6\n" "3\tthree\t4.1"
)
def test_default_widget_pretty_prints_json(admin_client, dashboard_db):
response = admin_client.post(
"/dashboard/",
{
"sql": """
select json_build_object('hello', json_build_array(1, 2, 3)) as json
"""
},
follow=True,
)
html = response.content.decode("utf-8")
soup = BeautifulSoup(html, "html5lib")
trs = soup.select("table tbody tr")
assert str(trs[0].find("td")) == (
'<td><pre class="json">{\n'
' "hello": [\n'
" 1,\n"
" 2,\n"
" 3\n"
" ]\n"
"}</pre></td>"
)
@pytest.mark.parametrize(
"sql,expected",
(
("SELECT * FROM generate_series(0, 5)", "6 rows</p>"),
("SELECT 'hello'", "1 row</p>"),
("SELECT * FROM generate_series(0, 1000)", "Results were truncated"),
),
)
def test_default_widget_shows_row_count_or_truncated_message(
admin_client, dashboard_db, sql, expected
):
response = admin_client.post(
"/dashboard/",
{"sql": sql},
follow=True,
)
assert expected in response.content.decode("utf-8")
def test_default_widget_column_count_links(admin_client, dashboard_db):
response = admin_client.post(
"/dashboard/",
{
"sql": """
SELECT * FROM (
VALUES (1, %(label)s, 4.5), (2, 'two', 3.6), (3, 'three', 4.1)
) AS t (id, name, size)""",
"label": "LABEL",
},
follow=True,
)
soup = BeautifulSoup(response.content, "html5lib")
# Check that first link
th = soup.select("thead th")[0]
assert th["data-count-url"]
querystring = th["data-count-url"].split("?")[1]
bits = dict(parse_qsl(querystring))
assert unsign_sql(bits["sql"])[0] == (
'select "id", count(*) as n from (SELECT * FROM (\n'
" VALUES (1, %(label)s, 4.5), "
"(2, 'two', 3.6), (3, 'three', 4.1)\n"
" ) AS t (id, name, size))"
' as results group by "id" order by n desc'
)
assert bits["label"] == "LABEL"
@pytest.mark.parametrize(
"sql,should_have_count_links",
(
("SELECT 1 AS id, 2 AS id", False),
("SELECT 1 AS id, 2 AS id2", True),
),
)
def test_default_widget_no_count_links_for_ambiguous_columns(
admin_client, dashboard_db, sql, should_have_count_links
):
response = admin_client.post(
"/dashboard/",
{"sql": sql},
follow=True,
)
soup = BeautifulSoup(response.content, "html5lib")
ths_with_data_count_url = soup.select("th[data-count-url]")
if should_have_count_links:
assert len(ths_with_data_count_url)
else:
assert not len(ths_with_data_count_url)
def test_custom_query_widget(admin_client, dashboard_db):
response = admin_client.post(
"/dashboard/",
{
"sql": "select '<h1>Hi</h1>' as html, 1 as number",
"_save-title": "",
"_save-slug": "test-query-template",
"_save-description": "",
"_save-view_policy": "private",
"_save-view_group": "",
"_save-edit_policy": "private",
"_save-edit_group": "",
},
follow=True,
)
dashboard = Dashboard.objects.get(slug="test-query-template")
query = dashboard.queries.first()
query.template = "html"
query.save()
response = admin_client.get("/dashboard/test-query-template/")
html = response.content.decode("utf-8")
assert "<h1>Hi</h1>" in html
def test_big_number_widget(admin_client, dashboard_db):
response = admin_client.post(
"/dashboard/",
{"sql": "select 'Big' as label, 10801 * 5 as big_number"},
follow=True,
)
html = response.content.decode("utf-8")
assert (
' <div class="big-number">\n'
" <p><strong>Big</strong></p>\n"
" <h1>54005</h1>\n"
" </div>"
) in html
@pytest.mark.parametrize(
"sql,expected",
(
(
"select '# Foo\n\n## Bar [link](/)' as markdown",
'<h1>Foo</h1>\n<h2>Bar <a href="/" rel="nofollow">link</a></h2>',
),
("select null as markdown", ""),
),
)
def test_markdown_widget(admin_client, dashboard_db, sql, expected):
response = admin_client.post(
"/dashboard/",
{"sql": sql},
follow=True,
)
assert response.status_code == 200
html = response.content.decode("utf-8")
assert expected in html
def test_html_widget(admin_client, dashboard_db):
response = admin_client.post(
"/dashboard/",
{
"sql": "select '<h1>Hi</h1><script>alert(\"evil\")</script><p>There<br>And</p>' as html"
},
follow=True,
)
html = response.content.decode("utf-8")
assert (
"<h1>Hi</h1>" '<script>alert("evil")</script>' "<p>There<br>And</p>"
) in html
def test_bar_chart_widget(admin_client, dashboard_db):
sql = """
SELECT * FROM (
VALUES (1, 'one'), (2, 'two'), (3, 'three')
) AS t (bar_quantity, bar_label);
"""
response = admin_client.post(
"/dashboard/",
{"sql": sql},
follow=True,
)
html = response.content.decode("utf-8")
assert (
'<script id="vis-data-0" type="application/json">'
'[{"bar_quantity": 1, "bar_label": "one"}, '
'{"bar_quantity": 2, "bar_label": "two"}, '
'{"bar_quantity": 3, "bar_label": "three"}]</script>'
) in html
assert '$schema: "https://vega.github.io/schema/vega-lite/v5.json"' in html
def test_progress_bar_widget(admin_client, dashboard_db):
response = admin_client.post(
"/dashboard/",
{"sql": "select 100 as total_count, 72 as completed_count"},
follow=True,
)
html = response.content.decode("utf-8")
assert "<h2>72 / 100: 72%</h2>" in html
assert 'width: 72%"> </div>' in html
def test_word_cloud_widget(admin_client, dashboard_db):
sql = """
select * from (
values ('one', 1), ('two', 2), ('three', 3)
) as t (wordcloud_word, wordcloud_count);
"""
response = admin_client.post(
"/dashboard/",
{"sql": sql},
follow=True,
)
html = response.content.decode("utf-8")
assert (
'<script id="wordcloud-data-0" type="application/json">[{"wordcloud_word"'
in html
)