Completed
Push — master ( 4244aa...92b6ae )
by Chris
01:15
created

test_getdims_missing_all_expected()   A

Complexity

Conditions 2

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
c 0
b 0
f 0
dl 0
loc 12
rs 9.4285
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&amp;'
52
          'controls=0&amp;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&amp;'
65
          'controls=0&amp;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