test_p2g.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import io
  2. import networkx as nx
  3. from networkx.readwrite.p2g import read_p2g, write_p2g
  4. from networkx.utils import edges_equal
  5. class TestP2G:
  6. @classmethod
  7. def setup_class(cls):
  8. cls.G = nx.Graph(name="test")
  9. e = [("a", "b"), ("b", "c"), ("c", "d"), ("d", "e"), ("e", "f"), ("a", "f")]
  10. cls.G.add_edges_from(e)
  11. cls.G.add_node("g")
  12. cls.DG = nx.DiGraph(cls.G)
  13. def test_read_p2g(self):
  14. s = b"""\
  15. name
  16. 3 4
  17. a
  18. 1 2
  19. b
  20. c
  21. 0 2
  22. """
  23. bytesIO = io.BytesIO(s)
  24. G = read_p2g(bytesIO)
  25. assert G.name == "name"
  26. assert sorted(G) == ["a", "b", "c"]
  27. edges = [(str(u), str(v)) for u, v in G.edges()]
  28. assert edges_equal(G.edges(), [("a", "c"), ("a", "b"), ("c", "a"), ("c", "c")])
  29. def test_write_p2g(self):
  30. s = b"""foo
  31. 3 2
  32. 1
  33. 1
  34. 2
  35. 2
  36. 3
  37. """
  38. fh = io.BytesIO()
  39. G = nx.DiGraph()
  40. G.name = "foo"
  41. G.add_edges_from([(1, 2), (2, 3)])
  42. write_p2g(G, fh)
  43. fh.seek(0)
  44. r = fh.read()
  45. assert r == s
  46. def test_write_read_p2g(self):
  47. fh = io.BytesIO()
  48. G = nx.DiGraph()
  49. G.name = "foo"
  50. G.add_edges_from([("a", "b"), ("b", "c")])
  51. write_p2g(G, fh)
  52. fh.seek(0)
  53. H = read_p2g(fh)
  54. assert edges_equal(G.edges(), H.edges())