Skip to content

Commit

Permalink
测试代码整理
Browse files Browse the repository at this point in the history
  • Loading branch information
chenjiandongx committed Oct 21, 2017
1 parent 488394b commit 70be5d1
Show file tree
Hide file tree
Showing 28 changed files with 565 additions and 533 deletions.
19 changes: 12 additions & 7 deletions test/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@
RANGE_COLOR = [
'#313695', '#4575b4', '#74add1', '#abd9e9', '#e0f3f8',
'#ffffbf', '#fee090', '#fdae61', '#f46d43', '#d73027', '#a50026'
]
]

X_TIME = [
"12a", "1a", "2a", "3a", "4a", "5a", "6a", "7a", "8a", "9a", "10a", "11a",
"12p", "1p", "2p", "3p", "4p", "5p", "6p", "7p", "8p", "9p", "10p", "11p"
]
"12a", "1a", "2a", "3a", "4a", "5a", "6a", "7a", "8a", "9a", "10a", "11a",
"12p", "1p", "2p", "3p", "4p", "5p", "6p", "7p", "8p", "9p", "10p", "11p"
]

Y_WEEK = [
"Saturday", "Friday", "Thursday", "Wednesday",
"Tuesday", "Monday", "Sunday"
]
"Saturday", "Friday", "Thursday", "Wednesday",
"Tuesday", "Monday", "Sunday"
]

CLOTHES = ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"]

WEEK = ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
86 changes: 46 additions & 40 deletions test/test_bar.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,97 +5,103 @@
import random

from pyecharts import Bar
from test.constants import CLOTHES


def test_bar():
# bar stack
attr = ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"]
v1 = [5, 20, 36, 10, 75, 90]
v2 = [10, 25, 8, 60, 20, 80]
clothes_v1 = [5, 20, 36, 10, 75, 90]
clothes_v2 = [10, 25, 8, 60, 20, 80]


def test_bar_stack():
bar = Bar("柱状图数据堆叠示例")
bar.add("商家A", attr, v1, is_stack=True)
bar.add("商家B", attr, v2, is_stack=True)
bar.add("商家A", CLOTHES, clothes_v1, is_stack=True)
bar.add("商家B", CLOTHES, clothes_v2, is_stack=True)
html_content = bar._repr_html_()
assert "dataZoom" not in html_content
assert "stack_" in html_content

# bar markPoint&markLine

def test_bar_marks():
bar = Bar("标记线和标记点示例")
bar.add("商家A", attr, v1, mark_point=["average"])
bar.add("商家B", attr, v2, mark_line=["min", "max"])
bar.add("商家A", CLOTHES, clothes_v1, mark_point=["average"])
bar.add("商家B", CLOTHES, clothes_v2, mark_line=["min", "max"])
assert '"average"' in bar._repr_html_()

# bar convert xAxis-yAxis

def test_bar_convert_xy_axis():
bar = Bar("x 轴和 y 轴交换")
bar.add("商家A", attr, v1)
bar.add("商家B", attr, v2, is_convert=True)
bar.add("商家A", CLOTHES, clothes_v1)
bar.add("商家B", CLOTHES, clothes_v2, is_convert=True)
assert "average" not in bar._repr_html_()

# histogram

def test_bar_histogram():
bar = Bar("直方图示例")
bar.add("", attr * 2, v1 + v2, bar_category_gap=0)
bar.add("", CLOTHES * 2, clothes_v1 + clothes_v2, bar_category_gap=0)
bar.render()

# bar rotate label
attr = ["{}天".format(i) for i in range(20)]
v1 = [random.randint(1, 20) for _ in range(20)]

def test_bar_rotate_label():
days = ["{}天".format(i) for i in range(20)]
days_v1 = [random.randint(1, 20) for _ in range(20)]
bar = Bar("坐标轴标签旋转示例")
bar.add("", attr, v1, xaxis_interval=0, xaxis_rotate=30,
bar.add("", days, days_v1, xaxis_interval=0, xaxis_rotate=30,
yaxis_rotate=30)
assert "stack_" not in bar._repr_html_()

# bar waterfall
attr = ["{}月".format(i) for i in range(1, 8)]
v1 = [0, 100, 200, 300, 400, 220, 250]
v2 = [1000, 800, 600, 500, 450, 400, 300]

def test_bar_waterfall():
months = ["{}月".format(i) for i in range(1, 8)]
months_v1 = [0, 100, 200, 300, 400, 220, 250]
months_v2 = [1000, 800, 600, 500, 450, 400, 300]
bar = Bar("瀑布图示例")
bar.add("", attr, v1, label_color=['rgba(0,0,0,0)'], is_stack=True)
bar.add("月份", attr, v2, is_label_show=True, is_stack=True,
bar.add("", months, months_v1, label_color=['rgba(0,0,0,0)'], is_stack=True)
bar.add("月份", months, months_v2, is_label_show=True, is_stack=True,
label_pos='inside')
bar.render()


def test_bar_datazoom_undefined():
attr = ["{}天".format(i) for i in range(30)]
v1 = [random.randint(1, 30) for _ in range(30)]
days = ["{}天".format(i) for i in range(30)]
days_v1 = [random.randint(1, 30) for _ in range(30)]
bar = Bar("Bar - datazoom 默认 示例")
bar.add("", attr, v1, is_label_show=True, is_datazoom_show=True)
bar.add("", days, days_v1, is_label_show=True, is_datazoom_show=True)
html_content = bar._repr_html_()
assert "dataZoom" in html_content
assert ': "slider"' in html_content
assert ': "inside"' not in html_content


def test_bar_datazoom_slider():
attr = ["{}天".format(i) for i in range(30)]
v1 = [random.randint(1, 30) for _ in range(30)]
days = ["{}天".format(i) for i in range(30)]
days_v1 = [random.randint(1, 30) for _ in range(30)]
bar = Bar("Bar - datazoom 示例")
bar.add("", attr, v1, is_datazoom_show=True, datazoom_type='slider',
datazoom_range=[10, 25])
bar.add("", days, days_v1, is_datazoom_show=True,
datazoom_type='slider', datazoom_range=[10, 25])
html_content = bar._repr_html_()
assert "dataZoom" in html_content
assert ': "slider"' in html_content
assert ': "inside"' not in html_content


def test_bar_datazoom_inside():
attr = ["{}天".format(i) for i in range(30)]
v1 = [random.randint(1, 30) for _ in range(30)]
days = ["{}天".format(i) for i in range(30)]
days_v1 = [random.randint(1, 30) for _ in range(30)]
bar = Bar("Bar - datazoom - inside 示例")
bar.add("", attr, v1, is_datazoom_show=True, datazoom_type='inside',
datazoom_range=[10, 25])
bar.add("", days, days_v1, is_datazoom_show=True,
datazoom_type='inside', datazoom_range=[10, 25])
html_content = bar._repr_html_()
assert "dataZoom" in html_content
assert ': "inside"' in html_content
assert ': "slider"' not in html_content


def test_bar_datazoom_both():
attr = ["{}天".format(i) for i in range(30)]
v1 = [random.randint(1, 30) for _ in range(30)]
days = ["{}天".format(i) for i in range(30)]
days_v1 = [random.randint(1, 30) for _ in range(30)]
bar = Bar("Bar - datazoom - both 示例")
bar.add("", attr, v1, is_datazoom_show=True, datazoom_type='both',
datazoom_range=[10, 25])
bar.add("", days, days_v1, is_datazoom_show=True,
datazoom_type='both', datazoom_range=[10, 25])
html_content = bar._repr_html_()
assert "dataZoom" in html_content
assert ': "inside"' in html_content
Expand Down
75 changes: 39 additions & 36 deletions test/test_bar3D.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,63 +6,66 @@
from test.constants import RANGE_COLOR, X_TIME, Y_WEEK


def test_bar3d():
# bar3D default
bar3d = Bar3D("3D 柱状图示例", width=1200, height=600)
data = [
[0, 0, 5], [0, 1, 1], [0, 2, 0], [0, 3, 0], [0, 4, 0], [0, 5, 0],
[0, 6, 0], [0, 7, 0], [0, 8, 0], [0, 9, 0], [0, 10, 0], [0, 11, 2],
[0, 12, 4], [0, 13, 1], [0, 14, 1], [0, 15, 3], [0, 16, 4], [0, 17, 6],
[0, 18, 4], [0, 19, 4], [0, 20, 3], [0, 21, 3], [0, 22, 2], [0, 23, 5],
[1, 0, 7], [1, 1, 0], [1, 2, 0], [1, 3, 0], [1, 4, 0], [1, 5, 0],
[1, 6, 0], [1, 7, 0], [1, 8, 0], [1, 9, 0], [1, 10, 5], [1, 11, 2],
[1, 12, 2], [1, 13, 6], [1, 14, 9], [1, 15, 11], [1, 16, 6], [1, 17, 7],
[1, 18, 8], [1, 19, 12], [1, 20, 5], [1, 21, 5], [1, 22, 7], [1, 23, 2],
[2, 0, 1], [2, 1, 1], [2, 2, 0], [2, 3, 0], [2, 4, 0], [2, 5, 0],
[2, 6, 0], [2, 7, 0], [2, 8, 0], [2, 9, 0], [2, 10, 3], [2, 11, 2],
[2, 12, 1], [2, 13, 9], [2, 14, 8], [2, 15, 10], [2, 16, 6], [2, 17, 5],
[2, 18, 5], [2, 19, 5], [2, 20, 7], [2, 21, 4], [2, 22, 2], [2, 23, 4],
[3, 0, 7], [3, 1, 3], [3, 2, 0], [3, 3, 0], [3, 4, 0], [3, 5, 0],
[3, 6, 0], [3, 7, 0], [3, 8, 1], [3, 9, 0], [3, 10, 5], [3, 11, 4],
[3, 12, 7], [3, 13, 14], [3, 14, 13], [3, 15, 12], [3, 16, 9], [3, 17, 5],
[3, 18, 5], [3, 19, 10], [3, 20, 6], [3, 21, 4], [3, 22, 4], [3, 23, 1],
[4, 0, 1], [4, 1, 3], [4, 2, 0], [4, 3, 0], [4, 4, 0], [4, 5, 1],
[4, 6, 0], [4, 7, 0], [4, 8, 0], [4, 9, 2], [4, 10, 4], [4, 11, 4],
[4, 12, 2], [4, 13, 4], [4, 14, 4], [4, 15, 14], [4, 16, 12], [4, 17, 1],
[4, 18, 8], [4, 19, 5], [4, 20, 3], [4, 21, 7], [4, 22, 3], [4, 23, 0],
[5, 0, 2], [5, 1, 1], [5, 2, 0], [5, 3, 3], [5, 4, 0], [5, 5, 0],
[5, 6, 0], [5, 7, 0], [5, 8, 2], [5, 9, 0], [5, 10, 4], [5, 11, 1],
[5, 12, 5], [5, 13, 10], [5, 14, 5], [5, 15, 7], [5, 16, 11], [5, 17, 6],
[5, 18, 0], [5, 19, 5], [5, 20, 3], [5, 21, 4], [5, 22, 2], [5, 23, 0],
[6, 0, 1], [6, 1, 0], [6, 2, 0], [6, 3, 0], [6, 4, 0], [6, 5, 0],
[6, 6, 0], [6, 7, 0], [6, 8, 0], [6, 9, 0], [6, 10, 1], [6, 11, 0],
[6, 12, 2], [6, 13, 1], [6, 14, 3], [6, 15, 4], [6, 16, 0], [6, 17, 0],
[6, 18, 0], [6, 19, 0], [6, 20, 1], [6, 21, 2], [6, 22, 2], [6, 23, 6]
]
data = [
[0, 0, 5], [0, 1, 1], [0, 2, 0], [0, 3, 0], [0, 4, 0], [0, 5, 0],
[0, 6, 0], [0, 7, 0], [0, 8, 0], [0, 9, 0], [0, 10, 0], [0, 11, 2],
[0, 12, 4], [0, 13, 1], [0, 14, 1], [0, 15, 3], [0, 16, 4], [0, 17, 6],
[0, 18, 4], [0, 19, 4], [0, 20, 3], [0, 21, 3], [0, 22, 2], [0, 23, 5],
[1, 0, 7], [1, 1, 0], [1, 2, 0], [1, 3, 0], [1, 4, 0], [1, 5, 0],
[1, 6, 0], [1, 7, 0], [1, 8, 0], [1, 9, 0], [1, 10, 5], [1, 11, 2],
[1, 12, 2], [1, 13, 6], [1, 14, 9], [1, 15, 11], [1, 16, 6], [1, 17, 7],
[1, 18, 8], [1, 19, 12], [1, 20, 5], [1, 21, 5], [1, 22, 7], [1, 23, 2],
[2, 0, 1], [2, 1, 1], [2, 2, 0], [2, 3, 0], [2, 4, 0], [2, 5, 0],
[2, 6, 0], [2, 7, 0], [2, 8, 0], [2, 9, 0], [2, 10, 3], [2, 11, 2],
[2, 12, 1], [2, 13, 9], [2, 14, 8], [2, 15, 10], [2, 16, 6], [2, 17, 5],
[2, 18, 5], [2, 19, 5], [2, 20, 7], [2, 21, 4], [2, 22, 2], [2, 23, 4],
[3, 0, 7], [3, 1, 3], [3, 2, 0], [3, 3, 0], [3, 4, 0], [3, 5, 0],
[3, 6, 0], [3, 7, 0], [3, 8, 1], [3, 9, 0], [3, 10, 5], [3, 11, 4],
[3, 12, 7], [3, 13, 14], [3, 14, 13], [3, 15, 12], [3, 16, 9], [3, 17, 5],
[3, 18, 5], [3, 19, 10], [3, 20, 6], [3, 21, 4], [3, 22, 4], [3, 23, 1],
[4, 0, 1], [4, 1, 3], [4, 2, 0], [4, 3, 0], [4, 4, 0], [4, 5, 1],
[4, 6, 0], [4, 7, 0], [4, 8, 0], [4, 9, 2], [4, 10, 4], [4, 11, 4],
[4, 12, 2], [4, 13, 4], [4, 14, 4], [4, 15, 14], [4, 16, 12], [4, 17, 1],
[4, 18, 8], [4, 19, 5], [4, 20, 3], [4, 21, 7], [4, 22, 3], [4, 23, 0],
[5, 0, 2], [5, 1, 1], [5, 2, 0], [5, 3, 3], [5, 4, 0], [5, 5, 0],
[5, 6, 0], [5, 7, 0], [5, 8, 2], [5, 9, 0], [5, 10, 4], [5, 11, 1],
[5, 12, 5], [5, 13, 10], [5, 14, 5], [5, 15, 7], [5, 16, 11], [5, 17, 6],
[5, 18, 0], [5, 19, 5], [5, 20, 3], [5, 21, 4], [5, 22, 2], [5, 23, 0],
[6, 0, 1], [6, 1, 0], [6, 2, 0], [6, 3, 0], [6, 4, 0], [6, 5, 0],
[6, 6, 0], [6, 7, 0], [6, 8, 0], [6, 9, 0], [6, 10, 1], [6, 11, 0],
[6, 12, 2], [6, 13, 1], [6, 14, 3], [6, 15, 4], [6, 16, 0], [6, 17, 0],
[6, 18, 0], [6, 19, 0], [6, 20, 1], [6, 21, 2], [6, 22, 2], [6, 23, 6]
]


def test_bar3d_default():
bar3d = Bar3D("3D 柱状图示例", width=1200, height=600)
bar3d.add("", X_TIME, Y_WEEK, [[d[1], d[0], d[2]] for d in data],
is_visualmap=True, visual_range=[0, 20],
visual_range_color=RANGE_COLOR,
grid3d_width=200, grid3d_depth=80)
assert "lambert" not in bar3d._repr_html_()

# bar3D shading lambert

def test_bar3d_shading_lambert():
bar3d = Bar3D("3D 柱状图示例", width=1200, height=600)
bar3d.add("", X_TIME, Y_WEEK, [[d[1], d[0], d[2]] for d in data],
is_visualmap=True, visual_range=[0, 20],
visual_range_color=RANGE_COLOR, grid3d_width=200,
grid3d_depth=80, grid3d_shading='lambert')
assert "lambert" in bar3d._repr_html_()

# bar3D rotate automatically

def test_bar3d_rotate_automatically():
bar3d = Bar3D("3D 柱状图示例", width=1200, height=600)
bar3d.add("", X_TIME, Y_WEEK, [[d[1], d[0], d[2]] for d in data],
is_visualmap=True, visual_range=[0, 20],
visual_range_color=RANGE_COLOR, grid3d_width=200,
grid3d_depth=80, is_grid3d_rotate=True)
bar3d.render()

# bar3D rotate automatically speedup

def test_bar3d_rotate_automatically_speedup():
bar3d = Bar3D("3D 柱状图示例", width=1200, height=600)
bar3d.add("", X_TIME, Y_WEEK, [[d[1], d[0], d[2]] for d in data],
is_visualmap=True, visual_range=[0, 20],
Expand Down
9 changes: 4 additions & 5 deletions test/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@

from nose.tools import eq_
from pyecharts import Bar, Map
from test.constants import CLOTHES


TITLE = "柱状图数据堆叠示例"


def create_a_bar(title):
attr = ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"]
v1 = [5, 20, 36, 10, 75, 90]
v2 = [10, 25, 8, 60, 20, 80]
bar = Bar(title)
bar.add("商家A", attr, v1, is_stack=True)
bar.add("商家B", attr, v2, is_stack=True)
bar.add("商家A", CLOTHES, v1, is_stack=True)
bar.add("商家B", CLOTHES, v2, is_stack=True)
return bar


Expand Down Expand Up @@ -71,10 +71,9 @@ def test_base_get_js_dependencies():


def test_numpy_array():
attr = ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"]
v1 = np.array([5, 20, 36, 10, 75, 90])
bar = Bar(TITLE)
bar.add("商家A", attr, v1, is_stack=True)
bar.add("商家A", CLOTHES, v1, is_stack=True)
html = bar.render_embed()
json_encoded_title = json.dumps(TITLE)
assert json_encoded_title in html
Expand Down
18 changes: 8 additions & 10 deletions test/test_chart_properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from pyecharts.base import Base
from pyecharts import Bar, Line, Grid
from test.constants import CLOTHES, WEEK
from nose.tools import eq_

UUID_HEX_LENGTH = 32
Expand All @@ -21,12 +22,11 @@ def test_base_properties():


def test_chart_properties():
attr = ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"]
v1 = [5, 20, 36, 10, 75, 90]
v2 = [10, 25, 8, 60, 20, 80]
bar = Bar("柱状图数据堆叠示例", width=900, height=500)
bar.add("商家A", attr, v1, is_stack=True)
bar.add("商家B", attr, v2, is_stack=True)
bar = Bar("柱状图-数据堆叠示例", width=900, height=500)
bar.add("商家A", CLOTHES, v1, is_stack=True)
bar.add("商家B", CLOTHES, v2, is_stack=True)

eq_(len(bar.chart_id), UUID_HEX_LENGTH)
eq_(bar.width, 900)
Expand All @@ -36,17 +36,15 @@ def test_chart_properties():


def test_grid_properties():
attr = ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"]
v1 = [5, 20, 36, 10, 75, 90]
v2 = [10, 25, 8, 60, 20, 80]
bar = Bar("柱状图示例", height=720)
bar.add("商家A", attr, v1, is_stack=True)
bar.add("商家B", attr, v2, is_stack=True)
bar.add("商家A", CLOTHES, v1, is_stack=True)
bar.add("商家B", CLOTHES, v2, is_stack=True)
line = Line("折线图示例", title_top="50%")
attr = ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
line.add("最高气温", attr, [11, 11, 15, 13, 12, 13, 10],
line.add("最高气温", WEEK, [11, 11, 15, 13, 12, 13, 10],
mark_point=["max", "min"], mark_line=["average"])
line.add("最低气温", attr, [1, -2, 2, 5, 3, 2, 0],
line.add("最低气温", WEEK, [1, -2, 2, 5, 3, 2, 0],
mark_point=["max", "min"], mark_line=["average"],
legend_top="50%")

Expand Down
Loading

0 comments on commit 70be5d1

Please sign in to comment.