1
|
|
|
from flask import url_for |
2
|
|
|
|
3
|
|
|
from pyquery import PyQuery as pq |
4
|
|
|
|
5
|
|
|
from flask_jsondash import charts_builder, utils |
6
|
|
|
|
7
|
|
|
from conftest import ( |
8
|
|
|
auth_valid, |
9
|
|
|
read, |
10
|
|
|
) |
11
|
|
|
|
12
|
|
|
|
13
|
|
|
def make_count(num=1000): |
14
|
|
|
def count(*args, **kwargs): |
15
|
|
|
return num |
16
|
|
|
return count |
17
|
|
|
|
18
|
|
|
|
19
|
|
|
def make_setting(num=30): |
20
|
|
|
def setting(*args, **kwargs): |
21
|
|
|
return num |
22
|
|
|
return setting |
23
|
|
|
|
24
|
|
|
|
25
|
|
|
def check_values(paginator, limit, perpage, currpage, skip, numpages, count): |
26
|
|
|
assert isinstance(paginator, utils.Paginator) |
27
|
|
|
assert paginator.limit == limit |
28
|
|
|
assert paginator.per_page == perpage |
29
|
|
|
assert paginator.curr_page == currpage |
30
|
|
|
assert paginator.skip == skip |
31
|
|
|
assert paginator.num_pages == list(numpages) |
32
|
|
|
assert paginator.count == count |
33
|
|
|
|
34
|
|
|
|
35
|
|
|
def test_paginator_default_usage(monkeypatch, client): |
36
|
|
|
app, test = client |
37
|
|
|
monkeypatch.setattr(utils, 'setting', make_setting(30)) |
38
|
|
|
monkeypatch.setattr(utils.adapter, 'count', make_count(1000)) |
39
|
|
|
paginator = utils.paginator(page=0) |
40
|
|
|
check_values(paginator, 30, 30, 0, 0, range(1, 35), 1000) |
41
|
|
|
|
42
|
|
|
|
43
|
|
|
def test_paginator_norecords(monkeypatch, client): |
44
|
|
|
app, test = client |
45
|
|
|
monkeypatch.setattr(utils, 'setting', make_setting(30)) |
46
|
|
|
monkeypatch.setattr(utils.adapter, 'count', make_count(0)) |
47
|
|
|
paginator = utils.paginator(page=0) |
48
|
|
|
check_values(paginator, 30, 30, 0, 0, [], 0) |
49
|
|
|
|
50
|
|
|
|
51
|
|
|
def test_paginator_default_fallback_data_lt_2(monkeypatch, client): |
52
|
|
|
app, test = client |
53
|
|
|
monkeypatch.setattr(utils, 'setting', make_setting(0)) |
54
|
|
|
monkeypatch.setattr(utils.adapter, 'count', make_count(0)) |
55
|
|
|
paginator = utils.paginator(page=None, per_page=1, count=None) |
56
|
|
|
check_values(paginator, 2, 2, 0, 0, [], 0) |
57
|
|
|
|
58
|
|
|
|
59
|
|
|
def test_paginator_bad_kwargs_fallback_data(monkeypatch, client): |
60
|
|
|
app, test = client |
61
|
|
|
monkeypatch.setattr(utils, 'setting', make_setting(0)) |
62
|
|
|
monkeypatch.setattr(utils.adapter, 'count', make_count(0)) |
63
|
|
|
# Ensure the paginator uses a minimum of 2 per page to prevent |
64
|
|
|
# division errors, even when there are no good values sent, |
65
|
|
|
# AND the app default setting is forcibly invalid (set to 0) |
66
|
|
|
paginator = utils.paginator( |
67
|
|
|
page=None, per_page=None, count=None) |
68
|
|
|
check_values(paginator, 2, 2, 0, 0, [], 0) |
69
|
|
|
|
70
|
|
|
|
71
|
|
|
def test_create_dashboards_check_paginator_html(monkeypatch, ctx, client): |
72
|
|
|
app, test = client |
73
|
|
|
monkeypatch.setattr(charts_builder, 'auth', auth_valid) |
74
|
|
|
for i in range(100): |
75
|
|
|
data = dict(name=i, modules=[]) |
76
|
|
|
res = test.post(url_for('jsondash.create'), data=data) |
77
|
|
|
res = test.get(url_for('jsondash.dashboard')) |
78
|
|
|
dom = pq(res.data) |
79
|
|
|
assert len(read()) == 100 |
80
|
|
|
assert dom.find('.paginator-status').text() == 'Showing 0-25 of 100 results' |
81
|
|
|
res = test.get(url_for('jsondash.dashboard') + '?page=2') |
82
|
|
|
dom = pq(res.data) |
83
|
|
|
assert dom.find( |
84
|
|
|
'.paginator-status').text() == 'Showing 25-50 of 100 results' |
85
|
|
|
res = test.get(url_for('jsondash.dashboard') + '?page=3') |
86
|
|
|
dom = pq(res.data) |
87
|
|
|
assert dom.find( |
88
|
|
|
'.paginator-status').text() == 'Showing 50-75 of 100 results' |
89
|
|
|
res = test.get(url_for('jsondash.dashboard') + '?page=4') |
90
|
|
|
dom = pq(res.data) |
91
|
|
|
assert dom.find( |
92
|
|
|
'.paginator-status').text() == 'Showing 75-100 of 100 results' |
93
|
|
|
|