|
1
|
|
|
from random import shuffle |
|
2
|
|
|
|
|
3
|
|
|
import pytest |
|
4
|
|
|
|
|
5
|
|
|
from flask_jsondash import charts_builder |
|
6
|
|
|
|
|
7
|
|
|
|
|
8
|
|
|
@pytest.mark.utils |
|
9
|
|
|
def test_order_sort_force_valueerror(): |
|
10
|
|
|
item = dict(order='NaN') |
|
11
|
|
|
assert charts_builder.order_sort(item) == -1 |
|
12
|
|
|
|
|
13
|
|
|
|
|
14
|
|
|
@pytest.mark.utils |
|
15
|
|
|
def test_order_sort_invalid_key(): |
|
16
|
|
|
item = dict() |
|
17
|
|
|
assert charts_builder.order_sort(item) == -1 |
|
18
|
|
|
|
|
19
|
|
|
|
|
20
|
|
|
@pytest.mark.utils |
|
21
|
|
|
def test_order_sort_valid_key(): |
|
22
|
|
|
item = dict(order=1) |
|
23
|
|
|
assert charts_builder.order_sort(item) == 1 |
|
24
|
|
|
|
|
25
|
|
|
|
|
26
|
|
|
@pytest.mark.utils |
|
27
|
|
|
def test_order_shuffled_sort_multiple_valid_key(): |
|
28
|
|
|
orders = list(range(0, 10)) |
|
29
|
|
|
shuffle(orders) |
|
30
|
|
|
modules = [dict(order=i, foo='bar') for i in orders] |
|
31
|
|
|
res = sorted(modules, key=charts_builder.order_sort) |
|
32
|
|
|
for i in range(0, 10): |
|
33
|
|
|
assert res[i]['order'] == i |
|
34
|
|
|
|
|
35
|
|
|
|
|
36
|
|
|
@pytest.mark.utils |
|
37
|
|
|
def test_order_shuffled_sort_multiple_valid_key_one_invalid_key(): |
|
38
|
|
|
orders = list(range(0, 10)) |
|
39
|
|
|
shuffle(orders) |
|
40
|
|
|
modules = [dict(order=i, foo='bar') for i in orders] |
|
41
|
|
|
# Add one w/o order key. |
|
42
|
|
|
modules.append(dict(foo='bar')) |
|
43
|
|
|
res = sorted(modules, key=charts_builder.order_sort) |
|
44
|
|
|
# The invalid key goes first. |
|
45
|
|
|
assert 'order' not in res[0] |
|
46
|
|
|
# The remaining will be offset since the first one has no key. |
|
47
|
|
|
for i in range(0, 10): |
|
48
|
|
|
if 'order' in res[i]: |
|
49
|
|
|
assert res[i]['order'] == i - 1 |
|
50
|
|
|
|
|
51
|
|
|
|
|
52
|
|
|
def test_get_all_assets(): |
|
53
|
|
|
res = charts_builder.get_all_assets() |
|
54
|
|
|
assert isinstance(res['css'], list) |
|
55
|
|
|
assert isinstance(res['js'], list) |
|
56
|
|
|
for url in res['js']: |
|
57
|
|
|
assert isinstance(url, str) |
|
58
|
|
|
assert url.endswith('.js') |
|
59
|
|
|
for url in res['css']: |
|
60
|
|
|
assert isinstance(url, str) |
|
61
|
|
|
assert url.endswith('.css') |
|
62
|
|
|
|
|
63
|
|
|
|
|
64
|
|
|
def test_get_active_assets_empty(): |
|
65
|
|
|
all_res = charts_builder.get_all_assets() |
|
66
|
|
|
active_res = charts_builder.get_active_assets([]) |
|
67
|
|
|
assert all_res == active_res |
|
68
|
|
|
|
|
69
|
|
|
|
|
70
|
|
|
def test_get_active_assets(): |
|
71
|
|
|
all_res = charts_builder.get_all_assets() |
|
72
|
|
|
families = ['D3'] |
|
73
|
|
|
active_res = charts_builder.get_active_assets(families) |
|
74
|
|
|
assert all_res != active_res |
|
75
|
|
|
|
|
76
|
|
|
|
|
77
|
|
|
def test_get_active_assets_ensure_no_duplicates(): |
|
78
|
|
|
all_res = charts_builder.get_all_assets() |
|
79
|
|
|
families = ['D3', 'D3', 'C3', 'C3'] |
|
80
|
|
|
active_res = charts_builder.get_active_assets(families) |
|
81
|
|
|
assert all_res != active_res |
|
82
|
|
|
|