forked from pyecharts/pyecharts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_utils.py
102 lines (80 loc) · 2.5 KB
/
test_utils.py
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
# coding=utf-8
from __future__ import unicode_literals
import codecs
import json
import os
from datetime import date
import numpy as np
from nose.tools import eq_
from pyecharts.utils import (
write_utf8_html_file,
get_resource_dir,
json_dumps,
merge_js_dependencies
)
def test_get_resource_dir():
path = get_resource_dir('templates')
expected = os.path.join(os.getcwd(), '..', 'pyecharts', 'templates')
eq_(path, os.path.abspath(expected))
def test_write_utf8_html_file():
content = "柱状图数据堆叠示例"
file_name = 'test.html'
write_utf8_html_file(file_name, content)
with codecs.open(file_name, 'r', 'utf-8') as f:
actual_content = f.read()
eq_(content, actual_content)
def test_json_encoder():
"""
Test json encoder.
:return:
"""
data = date(2017, 1, 1)
eq_(json.dumps({'date': '2017-01-01', 'a': '1'}, indent=0),
json_dumps({'date': data, 'a': '1'}))
data2 = {'np_list': np.array(['a', 'b', 'c'])}
data2_e = {'np_list': ['a', 'b', 'c']}
eq_(json.dumps(data2_e, indent=0), json_dumps(data2))
class MockChart(object):
"""
A mock class for a Chart and Page
"""
def __init__(self, js_dependencies):
self.js_dependencies = js_dependencies
def test_merge_js_dependencies_with_one_chart():
# Prepare some kinds of charts or page.
base_chart = MockChart(['echarts'])
# One chart or page
eq_(['echarts'], merge_js_dependencies(base_chart))
# A map chart
ch1 = MockChart(['echarts', 'fujian', 'zhengjiang', 'anhui'])
eq_(
['echarts', 'fujian', 'zhengjiang', 'anhui'],
merge_js_dependencies(ch1)
)
def test_merge_js_dependencies_with_multiple_charts():
base_chart = MockChart(['echarts'])
map_chart = MockChart(['echarts', 'fujian'])
three_d_chart = MockChart(['echarts', 'echartsgl'])
# Multiple charts
eq_(
['echarts', 'fujian'],
merge_js_dependencies(base_chart, map_chart)
)
eq_(
['echarts', 'echartsgl', 'fujian'],
merge_js_dependencies(base_chart, map_chart, three_d_chart)
)
def test_merge_js_dependencies_with_mixed_chart_and_string():
map_chart = MockChart(['echarts', 'fujian'])
eq_(
['echarts', 'zhejiang'],
merge_js_dependencies('echarts', 'zhejiang')
)
eq_(
['echarts', 'zhejiang'],
merge_js_dependencies(['echarts', 'zhejiang'])
)
eq_(
['echarts', 'fujian'],
merge_js_dependencies('echarts', map_chart)
)