Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 

150 righe
5.0 KiB

  1. import os
  2. import unittest
  3. from diagrams import Cluster, Diagram, Node
  4. from diagrams import getcluster, getdiagram, setcluster, setdiagram
  5. class DiagramTest(unittest.TestCase):
  6. def setUp(self):
  7. self.name = "test"
  8. def tearDown(self):
  9. setdiagram(None)
  10. setcluster(None)
  11. # Only some tests generate the image file.
  12. try:
  13. os.remove(self.name + ".png")
  14. except FileNotFoundError:
  15. pass
  16. def test_validate_direction(self):
  17. # Normal directions.
  18. for dir in ("TB", "BT", "LR", "RL"):
  19. Diagram(direction=dir)
  20. # Invalid directions.
  21. for dir in ("BR", "TL", "Unknown"):
  22. with self.assertRaises(ValueError):
  23. Diagram(direction=dir)
  24. def test_validate_outformat(self):
  25. # Normal output formats.
  26. for fmt in ("png", "jpg", "svg", "pdf"):
  27. Diagram(outformat=fmt)
  28. # Invalid output formats.
  29. for fmt in ("pnp", "jpe", "unknown"):
  30. with self.assertRaises(ValueError):
  31. Diagram(outformat=fmt)
  32. def test_with_global_context(self):
  33. self.assertIsNone(getdiagram())
  34. with Diagram(name=self.name, show=False):
  35. self.assertIsNotNone(getdiagram())
  36. self.assertIsNone(getdiagram())
  37. def test_node_not_in_diagram(self):
  38. # Node must be belong to a diagrams.
  39. with self.assertRaises(EnvironmentError):
  40. Node("node")
  41. def test_node_to_node(self):
  42. with Diagram(name=self.name, show=False):
  43. node1 = Node("node1")
  44. node2 = Node("node2")
  45. self.assertEqual(node1 - node2, node2)
  46. self.assertEqual(node1 >> node2, node2)
  47. self.assertEqual(node1 << node2, node2)
  48. def test_node_to_nodes(self):
  49. with Diagram(name=self.name, show=False):
  50. node1 = Node("node1")
  51. nodes = [Node("node2"), Node("node3")]
  52. self.assertEqual(node1 - nodes, nodes)
  53. self.assertEqual(node1 >> nodes, nodes)
  54. self.assertEqual(node1 << nodes, nodes)
  55. def test_nodes_to_node(self):
  56. with Diagram(name=self.name, show=False):
  57. node1 = Node("node1")
  58. nodes = [Node("node2"), Node("node3")]
  59. self.assertEqual(nodes - node1, node1)
  60. self.assertEqual(nodes >> node1, node1)
  61. self.assertEqual(nodes << node1, node1)
  62. class ClusterTest(unittest.TestCase):
  63. def setUp(self):
  64. self.name = "test"
  65. def tearDown(self):
  66. setdiagram(None)
  67. setcluster(None)
  68. # Only some tests generate the image file.
  69. try:
  70. os.remove(self.name + ".png")
  71. except FileNotFoundError:
  72. pass
  73. def test_validate_direction(self):
  74. # Normal directions.
  75. for dir in ("TB", "BT", "LR", "RL"):
  76. with Diagram(name=self.name, show=False):
  77. Cluster(direction=dir)
  78. # Invalid directions.
  79. for dir in ("BR", "TL", "Unknown"):
  80. with self.assertRaises(ValueError):
  81. with Diagram(name=self.name, show=False):
  82. Cluster(direction=dir)
  83. def test_with_global_context(self):
  84. with Diagram(name=self.name, show=False):
  85. self.assertIsNone(getcluster())
  86. with Cluster():
  87. self.assertIsNotNone(getcluster())
  88. self.assertIsNone(getcluster())
  89. def test_with_nested_cluster(self):
  90. with Diagram(name=self.name, show=False):
  91. self.assertIsNone(getcluster())
  92. with Cluster() as c1:
  93. self.assertEqual(c1, getcluster())
  94. with Cluster() as c2:
  95. self.assertEqual(c2, getcluster())
  96. self.assertEqual(c1, getcluster())
  97. self.assertIsNone(getcluster())
  98. def test_node_not_in_diagram(self):
  99. # Node must be belong to a diagrams.
  100. with self.assertRaises(EnvironmentError):
  101. Node("node")
  102. def test_node_to_node(self):
  103. with Diagram(name=self.name, show=False):
  104. with Cluster():
  105. node1 = Node("node1")
  106. node2 = Node("node2")
  107. self.assertEqual(node1 - node2, node2)
  108. self.assertEqual(node1 >> node2, node2)
  109. self.assertEqual(node1 << node2, node2)
  110. def test_node_to_nodes(self):
  111. with Diagram(name=self.name, show=False):
  112. with Cluster():
  113. node1 = Node("node1")
  114. nodes = [Node("node2"), Node("node3")]
  115. self.assertEqual(node1 - nodes, nodes)
  116. self.assertEqual(node1 >> nodes, nodes)
  117. self.assertEqual(node1 << nodes, nodes)
  118. def test_nodes_to_node(self):
  119. with Diagram(name=self.name, show=False):
  120. with Cluster():
  121. node1 = Node("node1")
  122. nodes = [Node("node2"), Node("node3")]
  123. self.assertEqual(nodes - node1, node1)
  124. self.assertEqual(nodes >> node1, node1)
  125. self.assertEqual(nodes << node1, node1)