def test_data_types(self): data = [ True, False, 10**20, -2e33, "'", '"&&&""', [{ (b"\xfd", ): "\x7f", chr(0x4444): (1, 2) }, (2, "3")], ] data.append(chr(0x14444)) data.append(literal_eval("{2.3j, 1 - 2.3j, ()}")) G = eg.Graph() G.name = data G.graph["data"] = data G.add_node(0, int=-1, data=dict(data=data)) G.add_edge(0, 0, float=-2.5, data=data) gml = "\n".join(eg.generate_gml(G, stringizer=literal_stringizer)) G = eg.parse_gml(gml, destringizer=literal_destringizer) assert data == G.name assert {"name": data, "data": data} == G.graph assert G.nodes == {0: dict(int=-1, data=dict(data=data))} assert list(G.edges) == [(0, 0, dict(float=-2.5, data=data))] G = eg.Graph() G.graph["data"] = "frozenset([1, 2, 3])" G = eg.parse_gml(eg.generate_gml(G), destringizer=literal_eval) assert G.graph["data"] == "frozenset([1, 2, 3])"
def test_graph_types(self): for directed in [None, False, True]: for multigraph in [None, False, True]: gml = "graph [" if directed is not None: gml += " directed " + str(int(directed)) if multigraph is not None: gml += " multigraph " + str(int(multigraph)) gml += ' node [ id 0 label "0" ]' gml += " edge [ source 0 target 0 ]" gml += " ]" G = eg.parse_gml(gml) assert bool(directed) == G.is_directed() assert bool(multigraph) == G.is_multigraph() gml = "graph [\n" if directed is True: gml += " directed 1\n" if multigraph is True: gml += " multigraph 1\n" gml += """ node [ id 0 label "0" ] edge [ source 0 target 0 """ if multigraph: gml += " key 0\n" gml += " ]\n]" assert gml == "\n".join(eg.generate_gml(G))
def test_labels_are_strings(self): # GML requires labels to be strings (i.e., in quotes) answer = """graph [ node [ id 0 label "1203" ] ]""" G = eg.Graph() G.add_node(1203) data = "\n".join(eg.generate_gml(G, stringizer=literal_stringizer)) assert data == answer
def test_escape_unescape(self): gml = """graph [ name "&"䑄��&unknown;" ]""" G = eg.parse_gml(gml) assert ('&"\x0f' + chr(0x4444) + "��&unknown;" == G.name) gml = "\n".join(eg.generate_gml(G)) alnu = "#1234567890;&#x1234567890abcdef" answer = ("""graph [ name "&"䑄&""" + alnu + """;&unknown;" ]""") assert answer == gml
def assert_generate_error(*args, **kwargs): pytest.raises(eg.EasyGraphError, lambda: list(eg.generate_gml(*args, **kwargs)))