import unittest
from unittest.mock import patch
from pyecharts.charts import Bar
from pyecharts.render.engine import RenderEngine
from pyecharts.datasets import EXTRA, FILENAMES
from pyecharts.globals import CurrentConfig
class TestEngine(unittest.TestCase):
def test_generate_js_link(self):
FILENAMES.update({"existing_dep": ("file_path", "ext")})
EXTRA.update(
{
"https://extra_host.com": {
"dep": ("extra_file_path", "extra_ext"),
},
"https://extra_host.com/css": {
"dep1": ("extra_file_path", "css"),
},
}
)
chart = Bar()
chart.js_host = None
chart.js_dependencies.items = [
"https://api.map.baidu.com/test",
"existing_dep",
"dep",
"dep1",
]
RenderEngine.generate_js_link(chart)
assert chart.js_host == CurrentConfig.ONLINE_HOST
expected_links = [
"https://api.map.baidu.com/test",
"{}file_path.ext".format(CurrentConfig.ONLINE_HOST),
"https://extra_host.comextra_file_path.extra_ext",
]
assert chart.dependencies == expected_links
expected_css_links = [
"https://extra_host.com/cssextra_file_path.css",
]
assert chart.css_libs == expected_css_links
def test_import_iterable_new_location(self):
with patch("collections.abc", side_effect=ImportError):
import collections
try:
assert collections.Iterable.__module__ == "collections"
except (AttributeError, AssertionError):
pass
def test_import_iterable_old_location(self):
with patch.dict("sys.modules", {"collections.abc": object()}):
import collections.abc
assert collections.abc.Iterable.__module__ == "collections.abc"
def test_iterable_import(self):
try:
from collections.abc import Iterable
self.assertTrue(True)
except ImportError:
try:
from collections import Iterable
self.assertTrue(True)
except ImportError:
self.fail("Failed to import Iterable")