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
|
import pygraphviz as pgv
stringify = pgv.testing.stringify
def test_edge_attributes():
A = pgv.AGraph()
A.add_edge(1, 2, label="test", spam="eggs")
ans = """strict graph { 1 -- 2 [label=test, spam=eggs]; }"""
assert stringify(A) == " ".join(ans.split())
def test_edge_attributes2():
A = pgv.AGraph()
A.add_edge(1, 2)
one = A.get_edge(1, 2)
one.attr["label"] = "test"
one.attr["spam"] = "eggs"
assert "label" in one.attr
assert one.attr["label"] == "test"
assert sorted(one.attr.keys()) == ["label", "spam"]
ans = """strict graph { 1 -- 2 [label=test, spam=eggs]; }"""
assert stringify(A) == " ".join(ans.split())
one.attr["label"] = ""
one.attr["spam"] = ""
ans = """strict graph { 1 -- 2; }"""
assert stringify(A) == " ".join(ans.split())
one.attr["label"] = "test"
del one.attr["label"]
ans = """strict graph { 1 -- 2; }"""
assert stringify(A) == " ".join(ans.split())
def test_anonymous_edges():
text_graph = (
b"""graph test {\n a -- b [label="edge1"];\n a -- b [label="edge2"];\n }"""
)
import os
import tempfile
fd, fname = tempfile.mkstemp()
os.write(fd, text_graph)
os.close(fd)
A = pgv.AGraph(fname)
ans = """graph test { a -- b [label=edge1]; a -- b [label=edge2]; }"""
assert stringify(A) == " ".join(ans.split())
os.unlink(fname)
def test_edge_attribute_update():
A = pgv.AGraph(strict=True)
A.add_edge(1, 2, label="test", spam="eggs")
A.add_edge(1, 2, label="update", spam="")
ans = """strict graph { 1 -- 2 [label=update]; }"""
assert stringify(A) == " ".join(ans.split())
def test_edge_attribute_update_nonstrict():
A = pgv.AGraph(strict=False)
A.add_edge(1, 2, label="test", spam="eggs", key="one")
A.add_edge(1, 2, label="update", spam="", key="one")
ans = """graph { 1 -- 2 [key=one, label=update]; }"""
assert stringify(A) == " ".join(ans.split())
|