|
1
|
|
|
from random import shuffle |
|
2
|
|
|
|
|
3
|
|
|
import pytest |
|
4
|
|
|
|
|
5
|
|
|
from flask_jsondash import schema, charts_builder, utils |
|
6
|
|
|
|
|
7
|
|
|
|
|
8
|
|
|
@pytest.mark.schema |
|
9
|
|
|
@pytest.mark.parametrize('lst', [ |
|
10
|
|
|
[1, 2, 3], |
|
11
|
|
|
[1, 1, 2, 2, 3], |
|
12
|
|
|
[], # Handles empty case as true |
|
13
|
|
|
]) |
|
14
|
|
|
def test_is_consecutive_rows_normal(lst): |
|
15
|
|
|
assert schema.is_consecutive_rows(lst) |
|
16
|
|
|
|
|
17
|
|
|
|
|
18
|
|
|
@pytest.mark.schema |
|
19
|
|
|
@pytest.mark.parametrize('lst', [ |
|
20
|
|
|
[1, 2, 3, 10], |
|
21
|
|
|
[1, 1, 2, 4], |
|
22
|
|
|
range(1, 100, 2), |
|
23
|
|
|
]) |
|
24
|
|
|
def test_is_consecutive_rows_invalid(lst): |
|
25
|
|
|
assert not schema.is_consecutive_rows(lst) |
|
26
|
|
|
|
|
27
|
|
|
|
|
28
|
|
|
@pytest.mark.schema |
|
29
|
|
|
@pytest.mark.parametrize('lst', [ |
|
30
|
|
|
[1, 0, 2], |
|
31
|
|
|
[0, 1], |
|
32
|
|
|
[1, 0], |
|
33
|
|
|
]) |
|
34
|
|
|
def test_is_consecutive_rows_invalid_no_row_zero_allowed(lst): |
|
35
|
|
|
with pytest.raises(AssertionError): |
|
36
|
|
|
schema.is_consecutive_rows(lst) |
|
37
|
|
|
|
|
38
|
|
|
|
|
39
|
|
|
@pytest.mark.utils |
|
40
|
|
|
def test_get_num_rows_none(): |
|
41
|
|
|
assert utils.get_num_rows(None) is None |
|
42
|
|
|
|
|
43
|
|
|
|
|
44
|
|
|
@pytest.mark.utils |
|
45
|
|
|
def test_get_num_rows_freeform(): |
|
46
|
|
|
assert utils.get_num_rows(dict(layout='freeform')) is None |
|
47
|
|
|
|
|
48
|
|
|
|
|
49
|
|
|
@pytest.mark.utils |
|
50
|
|
|
def test_get_num_rows_fixed(): |
|
51
|
|
|
conf = dict( |
|
52
|
|
|
layout='grid', |
|
53
|
|
|
modules=[dict(row=1), dict(row=2)], |
|
54
|
|
|
) |
|
55
|
|
|
assert utils.get_num_rows(conf) == 2 |
|
56
|
|
|
|
|
57
|
|
|
|
|
58
|
|
|
@pytest.mark.utils |
|
59
|
|
|
def test_order_sort_none(): |
|
60
|
|
|
assert utils.order_sort(None) == -1 |
|
61
|
|
|
|
|
62
|
|
|
|
|
63
|
|
|
@pytest.mark.utils |
|
64
|
|
|
def test_order_sort_force_valueerror(): |
|
65
|
|
|
item = dict(order='NaN') |
|
66
|
|
|
assert utils.order_sort(item) == -1 |
|
67
|
|
|
|
|
68
|
|
|
|
|
69
|
|
|
@pytest.mark.utils |
|
70
|
|
|
def test_order_sort_force_valueerror_func(): |
|
71
|
|
|
item = dict(order=lambda x: x) |
|
72
|
|
|
assert utils.order_sort(item) == -1 |
|
73
|
|
|
|
|
74
|
|
|
|
|
75
|
|
|
@pytest.mark.utils |
|
76
|
|
|
def test_order_sort_force_valueerror_none(): |
|
77
|
|
|
item = dict(order=None) |
|
78
|
|
|
assert utils.order_sort(item) == -1 |
|
79
|
|
|
|
|
80
|
|
|
|
|
81
|
|
|
@pytest.mark.utils |
|
82
|
|
|
def test_order_sort_invalid_key(): |
|
83
|
|
|
item = dict() |
|
84
|
|
|
assert utils.order_sort(item) == -1 |
|
85
|
|
|
|
|
86
|
|
|
|
|
87
|
|
|
@pytest.mark.utils |
|
88
|
|
|
def test_order_sort_valid_key(): |
|
89
|
|
|
item = dict(order=1) |
|
90
|
|
|
assert utils.order_sort(item) == 1 |
|
91
|
|
|
|
|
92
|
|
|
|
|
93
|
|
|
@pytest.mark.utils |
|
94
|
|
|
def test_order_shuffled_sort_multiple_valid_key(): |
|
95
|
|
|
orders = list(range(0, 10)) |
|
96
|
|
|
shuffle(orders) |
|
97
|
|
|
modules = [dict(order=i, foo='bar') for i in orders] |
|
98
|
|
|
res = sorted(modules, key=utils.order_sort) |
|
99
|
|
|
for i in range(0, 10): |
|
100
|
|
|
assert res[i]['order'] == i |
|
101
|
|
|
|
|
102
|
|
|
|
|
103
|
|
|
@pytest.mark.utils |
|
104
|
|
|
def test_order_shuffled_sort_multiple_valid_key_one_invalid_key(): |
|
105
|
|
|
orders = list(range(0, 10)) |
|
106
|
|
|
shuffle(orders) |
|
107
|
|
|
modules = [dict(order=i, foo='bar') for i in orders] |
|
108
|
|
|
# Add one w/o order key. |
|
109
|
|
|
modules.append(dict(foo='bar')) |
|
110
|
|
|
res = sorted(modules, key=utils.order_sort) |
|
111
|
|
|
# The invalid key goes first. |
|
112
|
|
|
assert 'order' not in res[0] |
|
113
|
|
|
# The remaining will be offset since the first one has no key. |
|
114
|
|
|
for i in range(0, 10): |
|
115
|
|
|
if 'order' in res[i]: |
|
116
|
|
|
assert res[i]['order'] == i - 1 |
|
117
|
|
|
|
|
118
|
|
|
|
|
119
|
|
|
def test_get_all_assets(): |
|
120
|
|
|
# Test that all assets are simply the right filetype and that they exist. |
|
121
|
|
|
res = charts_builder.get_all_assets() |
|
122
|
|
|
assert isinstance(res['css'], list) |
|
123
|
|
|
assert isinstance(res['js'], list) |
|
124
|
|
|
for url in res['js']: |
|
125
|
|
|
assert isinstance(url, str) |
|
126
|
|
|
assert url.endswith('.js') |
|
127
|
|
|
for url in res['css']: |
|
128
|
|
|
assert isinstance(url, str) |
|
129
|
|
|
assert url.endswith('.css') |
|
130
|
|
|
|
|
131
|
|
|
|
|
132
|
|
|
def test_get_active_assets(): |
|
133
|
|
|
all_res = charts_builder.get_all_assets() |
|
134
|
|
|
families = ['D3'] |
|
135
|
|
|
active_res = charts_builder.get_active_assets(families) |
|
136
|
|
|
assert all_res != active_res |
|
137
|
|
|
|
|
138
|
|
|
|
|
139
|
|
|
def test_get_active_assets_ensure_no_duplicates(): |
|
140
|
|
|
all_res = charts_builder.get_all_assets() |
|
141
|
|
|
families = ['D3', 'D3', 'C3', 'C3'] |
|
142
|
|
|
active_res = charts_builder.get_active_assets(families) |
|
143
|
|
|
assert all_res != active_res |
|
144
|
|
|
|
|
145
|
|
|
|
|
146
|
|
|
def test_get_active_assets_ensure_deps_loaded_first(): |
|
147
|
|
|
# Ensure that assets that require dependencies have the deps |
|
148
|
|
|
# loaded FIRST. |
|
149
|
|
|
active_res = charts_builder.get_active_assets(['D3', 'C3']) |
|
150
|
|
|
assert active_res['css'][0].endswith('c3.min.css') |
|
151
|
|
|
assert active_res['js'][0].endswith('d3.min.js') |
|
152
|
|
|
# c3 depends on d3. |
|
153
|
|
|
assert active_res['js'][1].endswith('c3.min.js') |
|
154
|
|
|
|