pyecharts/test/test_graph.py
2023-03-16 17:01:03 +08:00

139 lines
4.5 KiB
Python

import os
import json
from unittest.mock import patch
from nose.tools import assert_equal, assert_in
from pyecharts import options as opts
from pyecharts.charts import Graph
@patch("pyecharts.render.engine.write_utf8_html_file")
def test_graph_base(fake_writer):
nodes = [
{"name": "结点1", "symbolSize": 10},
{"name": "结点2", "symbolSize": 20},
{"name": "结点3", "symbolSize": 30},
{"name": "结点4", "symbolSize": 40},
]
links = []
for i in nodes:
for j in nodes:
links.append({"source": i.get("name"), "target": j.get("name")})
c = Graph().add("", nodes, links, repulsion=8000)
c.render()
_, content = fake_writer.call_args[0]
assert_equal(c.theme, "white")
assert_equal(c.renderer, "canvas")
@patch("pyecharts.render.engine.write_utf8_html_file")
def test_graph_base_v1(fake_writer):
nodes = [
opts.GraphNode(name="结点1", symbol_size=10),
opts.GraphNode(name="结点2", symbol_size=20),
opts.GraphNode(name="结点3", symbol_size=30),
opts.GraphNode(name="结点4", symbol_size=40),
opts.GraphNode(name="结点5", symbol_size=50),
]
links = [
opts.GraphLink(source="结点1", target="结点2"),
opts.GraphLink(source="结点2", target="结点3"),
opts.GraphLink(source="结点3", target="结点4"),
opts.GraphLink(source="结点4", target="结点5"),
opts.GraphLink(source="结点5", target="结点1"),
]
c = (
Graph()
.add("", nodes, links, repulsion=4000)
.set_global_opts(title_opts=opts.TitleOpts(title="Graph-GraphNode-GraphLink"))
)
c.render()
_, content = fake_writer.call_args[0]
assert_equal(c.theme, "white")
assert_equal(c.renderer, "canvas")
@patch("pyecharts.render.engine.write_utf8_html_file")
def test_graph_base_v2(fake_writer):
with open(
os.path.join("test/fixtures", "les-miserables.json"), "r", encoding="utf-8"
) as f:
j = json.load(f)
nodes = j["nodes"]
links = j["links"]
categories = j["categories"]
categories = [opts.GraphCategory(name=d.get("name")) for d in categories]
c = (
Graph(init_opts=opts.InitOpts(width="1000px", height="600px"))
.add(
"",
nodes=nodes,
links=links,
categories=categories,
layout="circular",
is_rotate_label=True,
linestyle_opts=opts.LineStyleOpts(color="source", curve=0.3),
label_opts=opts.LabelOpts(position="right"),
)
.set_global_opts(
title_opts=opts.TitleOpts(title="Graph-Les Miserables"),
legend_opts=opts.LegendOpts(
orient="vertical", pos_left="2%", pos_top="20%"
),
)
)
c.render()
_, content = fake_writer.call_args[0]
assert_equal(c.theme, "white")
assert_equal(c.renderer, "canvas")
@patch("pyecharts.render.engine.write_utf8_html_file")
def test_graph_draggable_and_symbol_size(fake_writer):
nodes = [
{"name": "结点1", "symbolSize": 10},
{"name": "结点2", "symbolSize": 20},
{"name": "结点3", "symbolSize": 30},
{"name": "结点4", "symbolSize": 40},
]
links = []
for i in nodes:
for j in nodes:
links.append({"source": i.get("name"), "target": j.get("name")})
c = Graph().add("", nodes, links, repulsion=8000, is_draggable=True, symbol_size=50)
c.render()
_, content = fake_writer.call_args[0]
assert_in("draggable", content)
assert_in("symbolSize", content)
@patch("pyecharts.render.engine.write_utf8_html_file")
def test_graph_edge_label_opts(fake_writer):
nodes = [
{"name": "结点1", "symbolSize": 10},
{"name": "结点2", "symbolSize": 20},
{"name": "结点3", "symbolSize": 30},
{"name": "结点4", "symbolSize": 40},
]
links = []
for i in nodes:
for j in nodes:
links.append({"source": i.get("name"), "target": j.get("name")})
c = Graph().add(
"", nodes, links, repulsion=4000, edge_label=opts.LabelOpts(is_show=True)
)
c.render()
_, content = fake_writer.call_args[0]
assert_in("edgeLabel", content)
def test_graph_item():
node_name, link_source = "test_node_name", "test_link_source"
node = opts.GraphNode(name=node_name)
link = opts.GraphLink(source=link_source)
assert_equal(node_name, node.opts.get("name"))
assert_equal(link_source, link.opts.get("source"))