def test_specials(self): from math import isnan inf, nan = float("inf"), float("nan") G = eg.Graph() G.add_node(1, testattr=inf, strdata="inf", key="a") G.add_node(2, testattr=nan, strdata="nan", key="b") G.add_node(3, testattr=-inf, strdata="-inf", key="c") fh = io.BytesIO() eg.write_gexf(G, fh) fh.seek(0) filetext = fh.read() fh.seek(0) H = eg.read_gexf(fh, node_type=int) assert b"INF" in filetext assert b"NaN" in filetext assert b"-INF" in filetext assert H.nodes[1]["testattr"] == inf assert isnan(H.nodes[2]["testattr"]) assert H.nodes[3]["testattr"] == -inf assert H.nodes[1]["strdata"] == "inf" assert H.nodes[2]["strdata"] == "nan" assert H.nodes[3]["strdata"] == "-inf" assert H.nodes[1]["easygraph_key"] == "a" assert H.nodes[2]["easygraph_key"] == "b" assert H.nodes[3]["easygraph_key"] == "c"
def test_bool(self): G = eg.Graph() G.add_node(1, testattr=True) fh = io.BytesIO() eg.write_gexf(G, fh) fh.seek(0) H = eg.read_gexf(fh, node_type=int) assert H.nodes[1]["testattr"]
def test_simple_list(self): G = eg.Graph() list_value = [(1, 2, 3), (9, 1, 2)] G.add_node(1, key=list_value) fh = io.BytesIO() eg.write_gexf(G, fh) fh.seek(0) H = eg.read_gexf(fh, node_type=int) assert H.nodes[1]["easygraph_key"] == list_value
def test_serialize_ints_to_strings(self): G = eg.Graph() G.add_node(1, id=7, label=77) fh = io.BytesIO() eg.write_gexf(G, fh) fh.seek(0) H = eg.read_gexf(fh, node_type=int) assert list(H) == [7] assert H.nodes[7]["label"] == "77"
def test_read_attribute_graphml(self): G = self.attribute_graph H = eg.read_gexf(self.attribute_fh) assert sorted(G.nodes) == sorted(H.nodes) ge = sorted(G.edges) he = sorted(H.edges) for a, b in zip(ge, he): assert a == b self.attribute_fh.seek(0)
def test_write_read_simple_directed_graphml(self): G = self.simple_directed_graph fh = io.BytesIO() eg.write_gexf(G, fh) fh.seek(0) H = eg.read_gexf(fh) assert sorted(G.nodes) == sorted(H.nodes) assert sorted(G.edges) == sorted(H.edges) self.simple_directed_fh.seek(0)
def test_add_parent(self): G = eg.Graph() G.add_node(0, label="1", color="green", parents=[1, 2]) fh = io.BytesIO() eg.write_gexf(G, fh) fh.seek(0) H = eg.read_gexf(fh, node_type=int) assert sorted(G.nodes) == sorted(H.nodes) assert sorted(sorted(e) for e in G.edges) == sorted(sorted(e) for e in H.edges)
def test_dynamic_mode(self): G = eg.Graph() G.add_node(1, label="1", color="green") G.graph["mode"] = "dynamic" fh = io.BytesIO() eg.write_gexf(G, fh) fh.seek(0) H = eg.read_gexf(fh, node_type=int) assert sorted(G.nodes) == sorted(H.nodes) assert sorted(sorted(e) for e in G.edges) == sorted(sorted(e) for e in H.edges)
def test_slice_and_spell(self): # Test spell first, so version = 1.2 G = eg.Graph() G.add_node(0, label="1", color="green") G.nodes[0]["spells"] = [(1, 2)] fh = io.BytesIO() eg.write_gexf(G, fh) fh.seek(0) H = eg.read_gexf(fh, node_type=int) assert sorted(G.nodes) == sorted(H.nodes) assert sorted(sorted(e) for e in G.edges) == sorted(sorted(e) for e in H.edges) G = eg.Graph() G.add_node(0, label="1", color="green") G.nodes[0]["slices"] = [(1, 2)] fh = io.BytesIO() eg.write_gexf(G, fh, version="1.1draft") fh.seek(0) H = eg.read_gexf(fh, node_type=int) assert sorted(G.nodes) == sorted(H.nodes) assert sorted(sorted(e) for e in G.edges) == sorted(sorted(e) for e in H.edges)
def test_default_attribute(self): G = eg.Graph() G.add_node(1, label="1", color="green") eg.add_path(G, [0, 1, 2, 3]) G.add_edge(1, 2, foo=3) G.graph["node_default"] = {"color": "yellow"} G.graph["edge_default"] = {"foo": 7} fh = io.BytesIO() eg.write_gexf(G, fh) fh.seek(0) H = eg.read_gexf(fh, node_type=int) assert sorted(G.nodes) == sorted(H.nodes) # Reading a gexf graph always sets mode attribute to either # 'static' or 'dynamic'. Remove the mode attribute from the # read graph for the sake of comparing remaining attributes. del H.graph["mode"] assert G.graph == H.graph
def test_relabel(self): s = """<?xml version="1.0" encoding="UTF-8"?> <gexf xmlns="http://www.gexf.net/1.2draft" version='1.2'> <graph mode="static" defaultedgetype="directed" name=""> <nodes> <node id="0" label="Hello" /> <node id="1" label="Word" /> </nodes> <edges> <edge id="0" source="0" target="1"/> </edges> </graph> </gexf> """ fh = io.BytesIO(s.encode("UTF-8")) G = eg.read_gexf(fh, relabel=True) assert sorted(G.nodes) == ["Hello", "Word"]
def test_read_simple_undirected_graphml(self): G = self.simple_undirected_graph H = eg.read_gexf(self.simple_undirected_fh) assert sorted(G.nodes) == sorted(H.nodes) assert sorted(G.edges) == sorted(H.edges) self.simple_undirected_fh.seek(0)