Completed
Push — master ( 04c18b...a5a3aa )
by Chris
01:18
created

test_get_num_rows_fixed()   A

Complexity

Conditions 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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