forked from getredash/redash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_utils.py
102 lines (83 loc) · 3.17 KB
/
test_utils.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
from collections import namedtuple
from unittest import TestCase
from redash import create_app
from redash.utils import (
build_url,
collect_parameters_from_request,
filter_none,
generate_token,
json_dumps,
render_template,
)
DummyRequest = namedtuple("DummyRequest", ["host", "scheme"])
class TestBuildUrl(TestCase):
def test_simple_case(self):
self.assertEqual(
"http://example.com/test",
build_url(DummyRequest("", "http"), "example.com", "/test"),
)
def test_uses_current_request_port(self):
self.assertEqual(
"http://example.com:5000/test",
build_url(DummyRequest("example.com:5000", "http"), "example.com", "/test"),
)
def test_uses_current_request_schema(self):
self.assertEqual(
"https://example.com/test",
build_url(DummyRequest("example.com", "https"), "example.com", "/test"),
)
def test_skips_port_for_default_ports(self):
self.assertEqual(
"https://example.com/test",
build_url(DummyRequest("example.com:443", "https"), "example.com", "/test"),
)
self.assertEqual(
"http://example.com/test",
build_url(DummyRequest("example.com:80", "http"), "example.com", "/test"),
)
self.assertEqual(
"https://example.com:80/test",
build_url(DummyRequest("example.com:80", "https"), "example.com", "/test"),
)
self.assertEqual(
"http://example.com:443/test",
build_url(DummyRequest("example.com:443", "http"), "example.com", "/test"),
)
class TestCollectParametersFromRequest(TestCase):
def test_ignores_non_prefixed_values(self):
self.assertEqual({}, collect_parameters_from_request({"test": 1}))
def test_takes_prefixed_values(self):
self.assertDictEqual(
{"test": 1, "something_else": "test"},
collect_parameters_from_request({"p_test": 1, "p_something_else": "test"}),
)
class TestSkipNones(TestCase):
def test_skips_nones(self):
d = {"a": 1, "b": None}
self.assertDictEqual(filter_none(d), {"a": 1})
class TestJsonDumps(TestCase):
def test_handles_binary(self):
self.assertEqual(json_dumps(memoryview(b"test")), '"74657374"')
class TestGenerateToken(TestCase):
def test_format(self):
token = generate_token(40)
self.assertRegex(token, r"[a-zA-Z0-9]{40}")
class TestRenderTemplate(TestCase):
def test_render(self):
app = create_app()
with app.app_context():
d = {
"failures": [
{
"id": 1,
"name": "Failure Unit Test",
"failed_at": "May 04, 2021 02:07PM UTC",
"failure_reason": "",
"failure_count": 1,
"comment": None,
}
]
}
html, text = [render_template("emails/failures.{}".format(f), d) for f in ["html", "txt"]]
self.assertIn("Failure Unit Test", html)
self.assertIn("Failure Unit Test", text)