forked from pyecharts/pyecharts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_template_function.py
More file actions
126 lines (103 loc) · 3.6 KB
/
test_template_function.py
File metadata and controls
126 lines (103 loc) · 3.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# coding=utf8
"""
Test cases for jinja2 template functions
"""
from __future__ import unicode_literals
from nose.tools import raises
from pyecharts import Bar, Map
from pyecharts.conf import PyEchartsConfig
from pyecharts.engine import BaseEnvironment, EchartsEnvironment
from pyecharts.utils import get_resource_dir
ECHARTS_ENV = EchartsEnvironment()
def create_demo_bar(chart_id_demo=None):
attr = ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"]
v1 = [5, 20, 36, 10, 75, 90]
v2 = [10, 25, 8, 60, 20, 80]
bar = Bar("柱状图数据堆叠示例")
bar.add("商家A", attr, v1, is_stack=True)
bar.add("商家B", attr, v2, is_stack=True)
if chart_id_demo:
bar._chart_id = chart_id_demo
return bar
def test_echarts_js_dependencies():
env = EchartsEnvironment(
pyecharts_config=PyEchartsConfig(jshost="http://localhost/echarts")
)
tpl = env.from_string("{{ echarts_js_dependencies(bar) }}")
bar = create_demo_bar()
html = tpl.render(bar=bar)
assert (
'<script type="text/javascript" src="http://localhost/echarts/echarts.min.js"></script>'
== html
) # flake8: noqa
def test_echarts_js_dependencies_embed():
env = EchartsEnvironment(
pyecharts_config=PyEchartsConfig(
jshost=get_resource_dir("templates", "js", "echarts")
)
)
tpl = env.from_string('{{ echarts_js_dependencies_embed("echarts") }}')
bar = create_demo_bar()
html = tpl.render(bar=bar)
assert len(html) > 0
# no longer echarts_js_dependencies equals echarts_js_dependencies_
# embed when use local host.
# because the js files is either in python path or user path
# hence it could not be simply judged.
def test_echarts_js_container():
tpl = ECHARTS_ENV.from_string("{{ echarts_container(bar) }}")
bar = create_demo_bar("id_demo_chart")
html = tpl.render(bar=bar)
assert (
'<div id="id_demo_chart" style="width:800px;height:400px;"></div>'
== html
) # flake8: noqa
bar.width = 1024
bar.height = 768
html = tpl.render(bar=bar)
assert (
'<div id="id_demo_chart" style="width:1024px;height:768px;"></div>'
== html
) # flake8: noqa
bar.width = "1024px"
bar.height = "768px"
html = tpl.render(bar=bar)
assert (
'<div id="id_demo_chart" style="width:1024px;height:768px;"></div>'
== html
) # flake8: noqa
def test_echarts_js_content():
tpl = ECHARTS_ENV.from_string("{{ echarts_js_content(bar) }}")
bar = create_demo_bar()
html = tpl.render(bar=bar)
assert len(html) > 0
def test_echarts_js_content_wrap():
tpl = ECHARTS_ENV.from_string("{{ echarts_js_content_wrap(bar) }}")
bar = create_demo_bar()
html = tpl.render(bar=bar)
assert len(html) > 0
@raises(TypeError)
def test_create_environment_without_config():
be = BaseEnvironment()
def test_echarts_js_in_first():
value = [20, 190, 253, 77, 65]
attr = ["汕头市", "汕尾市", "揭阳市", "阳江市", "肇庆市"]
map = Map("广东地图示例", width=1200, height=600)
map.add(
"",
attr,
value,
maptype="广东",
is_visualmap=True,
visual_text_color="#000",
)
env = EchartsEnvironment(
pyecharts_config=PyEchartsConfig(jshost="http://localhost/echarts")
)
tpl = env.from_string("{{ echarts_js_dependencies(m) }}")
html = tpl.render(m=map)
echarts_js_pos = html.find("echarts.min.js")
guangdong_js_pos = html.find("guangdong.js")
assert echarts_js_pos > -1
assert guangdong_js_pos > -1
assert echarts_js_pos < guangdong_js_pos