Completed
Push — master ( 50c73f...50957d )
by Chris
01:24
created

test_is_consecutive_rows_invalid()   A

Complexity

Conditions 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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