1
|
|
|
import json |
2
|
|
|
|
3
|
|
|
from datetime import datetime as dt |
4
|
|
|
|
5
|
|
|
import pytest |
6
|
|
|
|
7
|
|
|
from flask_jsondash import charts_builder |
8
|
|
|
|
9
|
|
|
# Py2/3 compat. |
10
|
|
|
try: |
11
|
|
|
_unicode = unicode |
12
|
|
|
except NameError: |
13
|
|
|
_unicode = str |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
@pytest.mark.filters |
17
|
|
|
def test_getdims_normal(ctx, client): |
18
|
|
|
app, test = client |
19
|
|
|
data = dict(width=100, height=100, dataSource='...', type='sometype') |
20
|
|
|
expected = dict(width=100, height=100) |
21
|
|
|
assert charts_builder.get_dims(object, data) == expected |
22
|
|
|
|
23
|
|
|
|
24
|
|
|
@pytest.mark.parametrize('field', [ |
25
|
|
|
'width', |
26
|
|
|
'height', |
27
|
|
|
'dataSource', |
28
|
|
|
]) |
29
|
|
|
@pytest.mark.filters |
30
|
|
|
def test_getdims_missing_all_expected(ctx, client, field): |
31
|
|
|
app, test = client |
32
|
|
|
data = dict(width=100, height=100, dataSource='...', type='sometype') |
33
|
|
|
del data[field] |
34
|
|
|
with pytest.raises(ValueError): |
35
|
|
|
charts_builder.get_dims(object, data) |
36
|
|
|
|
37
|
|
|
|
38
|
|
|
@pytest.mark.filters |
39
|
|
|
def test_getdims_youtube_invalid_url(ctx, client): |
40
|
|
|
app, test = client |
41
|
|
|
data = dict(type='youtube', dataSource=None, width=100, height=100) |
42
|
|
|
with pytest.raises(ValueError): |
43
|
|
|
charts_builder.get_dims(object, data) |
44
|
|
|
|
45
|
|
|
|
46
|
|
|
@pytest.mark.filters |
47
|
|
|
def test_getdims_youtube(ctx, client): |
48
|
|
|
app, test = client |
49
|
|
|
yt = ('<iframe width="650" height="366" ' |
50
|
|
|
'src="https://www.youtube.com/embed/' |
51
|
|
|
'_hI0qMtdfng?list=RD_hI0qMtdfng&' |
52
|
|
|
'controls=0&showinfo=0" frameborder="0"' |
53
|
|
|
' allowfullscreen></iframe>') |
54
|
|
|
data = dict(type='youtube', dataSource=yt, width=100, height=100) |
55
|
|
|
expected = dict(width=650 + 20, height=366 + 60) |
56
|
|
|
assert charts_builder.get_dims(object, data) == expected |
57
|
|
|
|
58
|
|
|
|
59
|
|
|
@pytest.mark.filters |
60
|
|
|
def test_getdims_youtube_fixedgrid_width_adjusted_height_same(ctx, client): |
61
|
|
|
app, test = client |
62
|
|
|
yt = ('<iframe width="650" height="366" ' |
63
|
|
|
'src="https://www.youtube.com/embed/' |
64
|
|
|
'_hI0qMtdfng?list=RD_hI0qMtdfng&' |
65
|
|
|
'controls=0&showinfo=0" frameborder="0"' |
66
|
|
|
' allowfullscreen></iframe>') |
67
|
|
|
data = dict(type='youtube', dataSource=yt, width='col-6', height=100) |
68
|
|
|
expected = dict(width='6', height=100) |
69
|
|
|
assert charts_builder.get_dims(object, data) == expected |
70
|
|
|
|
71
|
|
|
|
72
|
|
|
@pytest.mark.filters |
73
|
|
|
def test_jsonstring(ctx, client): |
74
|
|
|
app, test = client |
75
|
|
|
now = dt.now() |
76
|
|
|
data = dict(date=now, foo='bar') |
77
|
|
|
res = charts_builder.jsonstring(object, data) |
78
|
|
|
assert 'foo' in res |
79
|
|
|
assert isinstance(res, str) |
80
|
|
|
d = json.loads(res) |
81
|
|
|
assert isinstance(d['date'], _unicode) |
82
|
|
|
|