pyecharts/test/test_base.py
LeoSun 64ba867342
🚀 Prepare for version 2.0.9【Last support version for Echarts 5.x】 (#2420)
* add support with google map(GMap)

* add support with leaflet map(LMap);

* fix blank

* add api in class Base for echarts stats extension

* support multiple calendar (#2414)

* support multiple calendar

* Fix PrettyTable class not expose parameters (#2416)

* Fix/grid and table (#2419)

fix grid chart_id not working and format table.py

* update version to 2.0.9

* fix: issue#2421 (#2422)

fix: issue#2421

* Fix grid index (#2426)

fix some bugs in Grid component

* 更新 MarkPointOpts、MarkPointItemOpts 以及 LabelOpts (#2427)

* update README.md

* [hotfix]fix page missing css_libs

* fix workflows

* fix workflows

* update engine.py

* FIX enable changing locale (#2442)

* FIX enable changing locale

* FIX add locale two more templates

* FIX add tests and defaultLocale

* FIX add tests and defaultLocale

* FIX add locale render in HTML test

---------

Co-authored-by: Feiko Ritsema <fritsema@netflix.com>

* update line.py and update workflows

* update python-app.yml

---------

Co-authored-by: sunhailin <sunhailin@jtexpress.com>
Co-authored-by: jiangyang <jiangyangcreate@gmail.com>
Co-authored-by: GokoRuri <1249736473@qq.com>
Co-authored-by: Feiko <feiko.ritsema@hotmail.com>
Co-authored-by: Feiko Ritsema <fritsema@netflix.com>
2025-10-10 15:24:18 +08:00

126 lines
4.5 KiB
Python

import unittest
from datetime import datetime
from unittest.mock import patch
from pyecharts.charts import Bar
from pyecharts.options import InitOpts, RenderOpts
from pyecharts.globals import CurrentConfig, Locale
from pyecharts.charts.base import Base, default
from pyecharts.options.series_options import AnimationOpts
class TestBaseClass(unittest.TestCase):
def test_base_add_functions(self):
c = Base()
c.add_js_funcs("console.log('hello')", "console.log('hello')")
self.assertEqual(1, len(c.js_functions.items))
self.assertEqual(["console.log('hello')"], c.js_functions.items)
def test_base_add_events(self):
c = Base()
c.add_js_events("console.log('hello')", "console.log('hello')")
self.assertEqual(1, len(c.js_events.items))
self.assertEqual(["console.log('hello')"], c.js_events.items)
def test_base_init_funcs(self):
c0 = Base({"width": "100px", "height": "200px"})
self.assertEqual(c0.width, "100px")
self.assertEqual(c0.height, "200px")
c1 = Base(dict(width="110px", height="210px"))
self.assertEqual(c1.width, "110px")
self.assertEqual(c1.height, "210px")
self.assertNotIn(c1.js_host, ["", None])
@patch("pyecharts.render.engine.write_utf8_html_file")
def test_render(self, fake_writer):
my_render_content = "my_render_content"
bar = Bar()
bar.add_xaxis(["1"]).add_yaxis("", [1]).render(
my_render_content=my_render_content
)
assert "test ok" == "test ok"
@patch("pyecharts.render.engine.write_utf8_html_file")
def test_render_js_host_none(self, fake_writer):
my_render_content = "my_render_content"
bar = Bar()
bar.add_xaxis(["1"]).add_yaxis("", [1])
# Hack to test
bar.js_host = None
# Render
bar.render(my_render_content=my_render_content)
self.assertEqual(bar.js_host, CurrentConfig.ONLINE_HOST)
@patch("pyecharts.render.engine.write_utf8_html_file")
def test_render_embed_js(self, _):
c = Base(render_opts=RenderOpts(is_embed_js=True))
# Embedded JavaScript
content = c.render_embed()
self.assertNotIn(
CurrentConfig.ONLINE_HOST, content, "Embedding JavaScript fails"
)
# No embedded JavaScript
c.render_options.update(embed_js=False)
content = c.render_embed()
self.assertIn(
CurrentConfig.ONLINE_HOST, content, "Embedded JavaScript cannot be closed"
)
def test_base_render_options(self):
c0 = Base(render_opts=RenderOpts(is_embed_js=True))
self.assertEqual(c0.render_options.get("embed_js"), True)
def test_base_iso_format(self):
mock_time_str = "2022-04-14 14:42:00"
mock_time = datetime.strptime(mock_time_str, "%Y-%m-%d %H:%M:%S")
assert default(mock_time) == "2022-04-14T14:42:00"
def test_base_animation_option(self):
c0 = Base(init_opts=InitOpts(animation_opts=AnimationOpts(animation=False)))
self.assertEqual(c0.options.get("animation"), False)
c1 = Base({"animationOpts": {"animation": False}})
self.assertEqual(c1.options.get("animation"), False)
def test_base_chart_id(self):
c0 = Base(init_opts=InitOpts(chart_id="1234567"))
self.assertEqual(c0.chart_id, "1234567")
c1 = Base(init_opts=InitOpts(chart_id="1234567"))
self.assertEqual(c1.get_chart_id(), "1234567")
def test_use_echarts_stat(self):
c0 = Base().use_echarts_stat()
self.assertEqual(c0.js_dependencies.items, ["echarts", "echarts-stat"])
def test_default_locale_zh(self):
# Default locale should be Chinese
c0 = Base()
self.assertEqual(c0.locale, Locale.ZH)
def test_set_locale_en(self):
# Set to English locale
CurrentConfig.LOCALE = Locale.EN
c0 = Base()
self.assertEqual(c0.locale, Locale.EN)
def test_set_locale_wrong(self):
# Set to a wrong locale, should fallback to default
CurrentConfig.LOCALE = "wrong_locale"
c0 = Base()
self.assertEqual(c0.locale, Locale.ZH)
@patch("pyecharts.render.engine.write_utf8_html_file")
def test_locale_renders_in_html(self, fake_writer):
CurrentConfig.LOCALE = Locale.ZH
c = (
Bar()
.add_xaxis(["A", "B", "C"])
.add_yaxis("series0", [1, 2, 4])
)
c.render()
_, content = fake_writer.call_args[0]
self.assertIn("locale: 'ZH'", content)