Completed
Push — master ( f3ed3a...684a71 )
by Chris
01:16
created

test_get_active_assets_empty()   A

Complexity

Conditions 2

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
c 1
b 0
f 1
dl 0
loc 4
rs 10
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_force_valueerror_func():
16
    item = dict(order=lambda x: x)
17
    assert charts_builder.order_sort(item) == -1
18
19
20
@pytest.mark.utils
21
def test_order_sort_force_valueerror_none():
22
    item = dict(order=None)
23
    assert charts_builder.order_sort(item) == -1
24
25
26
@pytest.mark.utils
27
def test_order_sort_invalid_key():
28
    item = dict()
29
    assert charts_builder.order_sort(item) == -1
30
31
32
@pytest.mark.utils
33
def test_order_sort_valid_key():
34
    item = dict(order=1)
35
    assert charts_builder.order_sort(item) == 1
36
37
38
@pytest.mark.utils
39
def test_order_shuffled_sort_multiple_valid_key():
40
    orders = list(range(0, 10))
41
    shuffle(orders)
42
    modules = [dict(order=i, foo='bar') for i in orders]
43
    res = sorted(modules, key=charts_builder.order_sort)
44
    for i in range(0, 10):
45
        assert res[i]['order'] == i
46
47
48
@pytest.mark.utils
49
def test_order_shuffled_sort_multiple_valid_key_one_invalid_key():
50
    orders = list(range(0, 10))
51
    shuffle(orders)
52
    modules = [dict(order=i, foo='bar') for i in orders]
53
    # Add one w/o order key.
54
    modules.append(dict(foo='bar'))
55
    res = sorted(modules, key=charts_builder.order_sort)
56
    # The invalid key goes first.
57
    assert 'order' not in res[0]
58
    # The remaining will be offset since the first one has no key.
59
    for i in range(0, 10):
60
        if 'order' in res[i]:
61
            assert res[i]['order'] == i - 1
62
63
64
def test_get_all_assets():
65
    # Test that all assets are simply the right filetype and that they exist.
66
    res = charts_builder.get_all_assets()
67
    assert isinstance(res['css'], list)
68
    assert isinstance(res['js'], list)
69
    for url in res['js']:
70
        assert isinstance(url, str)
71
        assert url.endswith('.js')
72
    for url in res['css']:
73
        assert isinstance(url, str)
74
        assert url.endswith('.css')
75
76
77
def test_get_active_assets():
78
    all_res = charts_builder.get_all_assets()
79
    families = ['D3']
80
    active_res = charts_builder.get_active_assets(families)
81
    assert all_res != active_res
82
83
84
def test_get_active_assets_ensure_no_duplicates():
85
    all_res = charts_builder.get_all_assets()
86
    families = ['D3', 'D3', 'C3', 'C3']
87
    active_res = charts_builder.get_active_assets(families)
88
    assert all_res != active_res
89
90
91
def test_get_active_assets_ensure_deps_loaded_first():
92
    # Ensure that assets that require dependencies have the deps
93
    # loaded FIRST.
94
    active_res = charts_builder.get_active_assets(['D3', 'C3'])
95
    assert active_res['css'][0].endswith('c3.min.css')
96
    assert active_res['js'][0].endswith('d3.min.js')
97
    # c3 depends on d3.
98
    assert active_res['js'][1].endswith('c3.min.js')
99