Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 

281 рядки
11 KiB

  1. import os
  2. import shutil
  3. import unittest
  4. from diagrams import Cluster, Diagram, Node, Edge
  5. from diagrams import getcluster, getdiagram, setcluster, setdiagram
  6. class DiagramTest(unittest.TestCase):
  7. def setUp(self):
  8. self.name = "diagram_test"
  9. def tearDown(self):
  10. setdiagram(None)
  11. setcluster(None)
  12. # Only some tests generate the image file.
  13. try:
  14. shutil.rmtree(self.name)
  15. except OSError:
  16. # Consider it file
  17. try:
  18. os.remove(self.name + ".png")
  19. except FileNotFoundError:
  20. pass
  21. def test_validate_direction(self):
  22. # Normal directions.
  23. for dir in ("TB", "BT", "LR", "RL"):
  24. Diagram(direction=dir)
  25. # Invalid directions.
  26. for dir in ("BR", "TL", "Unknown"):
  27. with self.assertRaises(ValueError):
  28. Diagram(direction=dir)
  29. def test_validate_curvestyle(self):
  30. # Normal directions.
  31. for cvs in ("ortho", "curved"):
  32. Diagram(curvestyle=cvs)
  33. # Invalid directions.
  34. for cvs in ("tangent", "unknown"):
  35. with self.assertRaises(ValueError):
  36. Diagram(curvestyle=cvs)
  37. def test_validate_outformat(self):
  38. # Normal output formats.
  39. for fmt in ("png", "jpg", "svg", "pdf"):
  40. Diagram(outformat=fmt)
  41. # Invalid output formats.
  42. for fmt in ("pnp", "jpe", "unknown"):
  43. with self.assertRaises(ValueError):
  44. Diagram(outformat=fmt)
  45. def test_with_global_context(self):
  46. self.assertIsNone(getdiagram())
  47. with Diagram(name=os.path.join(self.name, "with_global_context"), show=False):
  48. self.assertIsNotNone(getdiagram())
  49. self.assertIsNone(getdiagram())
  50. def test_node_not_in_diagram(self):
  51. # Node must be belong to a diagrams.
  52. with self.assertRaises(EnvironmentError):
  53. Node("node")
  54. def test_node_to_node(self):
  55. with Diagram(name=os.path.join(self.name, "node_to_node"), show=False):
  56. node1 = Node("node1")
  57. node2 = Node("node2")
  58. self.assertEqual(node1 - node2, node2)
  59. self.assertEqual(node1 >> node2, node2)
  60. self.assertEqual(node1 << node2, node2)
  61. def test_node_to_nodes(self):
  62. with Diagram(name=os.path.join(self.name, "node_to_nodes"), show=False):
  63. node1 = Node("node1")
  64. nodes = [Node("node2"), Node("node3")]
  65. self.assertEqual(node1 - nodes, nodes)
  66. self.assertEqual(node1 >> nodes, nodes)
  67. self.assertEqual(node1 << nodes, nodes)
  68. def test_nodes_to_node(self):
  69. with Diagram(name=os.path.join(self.name, "nodes_to_node"), show=False):
  70. node1 = Node("node1")
  71. nodes = [Node("node2"), Node("node3")]
  72. self.assertEqual(nodes - node1, node1)
  73. self.assertEqual(nodes >> node1, node1)
  74. self.assertEqual(nodes << node1, node1)
  75. def test_default_filename(self):
  76. self.name = "example_1"
  77. with Diagram(name="Example 1", show=False):
  78. Node("node1")
  79. self.assertTrue(os.path.exists(f"{self.name}.png"))
  80. def test_custom_filename(self):
  81. self.name = "my_custom_name"
  82. with Diagram(name="Example 1", filename=self.name, show=False):
  83. Node("node1")
  84. self.assertTrue(os.path.exists(f"{self.name}.png"))
  85. class ClusterTest(unittest.TestCase):
  86. def setUp(self):
  87. self.name = "cluster_test"
  88. def tearDown(self):
  89. setdiagram(None)
  90. setcluster(None)
  91. # Only some tests generate the image file.
  92. try:
  93. shutil.rmtree(self.name)
  94. except OSError:
  95. pass
  96. def test_validate_direction(self):
  97. # Normal directions.
  98. for dir in ("TB", "BT", "LR", "RL"):
  99. with Diagram(name=os.path.join(self.name, "validate_direction"), show=False):
  100. Cluster(direction=dir)
  101. # Invalid directions.
  102. for dir in ("BR", "TL", "Unknown"):
  103. with self.assertRaises(ValueError):
  104. with Diagram(name=os.path.join(self.name, "validate_direction"), show=False):
  105. Cluster(direction=dir)
  106. def test_with_global_context(self):
  107. with Diagram(name=os.path.join(self.name, "with_global_context"), show=False):
  108. self.assertIsNone(getcluster())
  109. with Cluster():
  110. self.assertIsNotNone(getcluster())
  111. self.assertIsNone(getcluster())
  112. def test_with_nested_cluster(self):
  113. with Diagram(name=os.path.join(self.name, "with_nested_cluster"), show=False):
  114. self.assertIsNone(getcluster())
  115. with Cluster() as c1:
  116. self.assertEqual(c1, getcluster())
  117. with Cluster() as c2:
  118. self.assertEqual(c2, getcluster())
  119. self.assertEqual(c1, getcluster())
  120. self.assertIsNone(getcluster())
  121. def test_node_not_in_diagram(self):
  122. # Node must be belong to a diagrams.
  123. with self.assertRaises(EnvironmentError):
  124. Node("node")
  125. def test_node_to_node(self):
  126. with Diagram(name=os.path.join(self.name, "node_to_node"), show=False):
  127. with Cluster():
  128. node1 = Node("node1")
  129. node2 = Node("node2")
  130. self.assertEqual(node1 - node2, node2)
  131. self.assertEqual(node1 >> node2, node2)
  132. self.assertEqual(node1 << node2, node2)
  133. def test_node_to_nodes(self):
  134. with Diagram(name=os.path.join(self.name, "node_to_nodes"), show=False):
  135. with Cluster():
  136. node1 = Node("node1")
  137. nodes = [Node("node2"), Node("node3")]
  138. self.assertEqual(node1 - nodes, nodes)
  139. self.assertEqual(node1 >> nodes, nodes)
  140. self.assertEqual(node1 << nodes, nodes)
  141. def test_nodes_to_node(self):
  142. with Diagram(name=os.path.join(self.name, "nodes_to_node"), show=False):
  143. with Cluster():
  144. node1 = Node("node1")
  145. nodes = [Node("node2"), Node("node3")]
  146. self.assertEqual(nodes - node1, node1)
  147. self.assertEqual(nodes >> node1, node1)
  148. self.assertEqual(nodes << node1, node1)
  149. class EdgeTest(unittest.TestCase):
  150. def setUp(self):
  151. self.name = "edge_test"
  152. def tearDown(self):
  153. setdiagram(None)
  154. setcluster(None)
  155. # Only some tests generate the image file.
  156. try:
  157. shutil.rmtree(self.name)
  158. except OSError:
  159. pass
  160. def test_node_to_node(self):
  161. with Diagram(name=os.path.join(self.name, "node_to_node"), show=False):
  162. node1 = Node("node1")
  163. node2 = Node("node2")
  164. self.assertEqual(node1 - Edge(color="red") - node2, node2)
  165. def test_node_to_nodes(self):
  166. with Diagram(name=os.path.join(self.name, "node_to_nodes"), show=False):
  167. with Cluster():
  168. node1 = Node("node1")
  169. nodes = [Node("node2"), Node("node3")]
  170. self.assertEqual(node1 - Edge(color="red") - nodes, nodes)
  171. def test_nodes_to_node(self):
  172. with Diagram(name=os.path.join(self.name, "nodes_to_node"), show=False):
  173. with Cluster():
  174. node1 = Node("node1")
  175. nodes = [Node("node2"), Node("node3")]
  176. self.assertEqual(nodes - Edge(color="red") - node1, node1)
  177. def test_nodes_to_node_with_additional_attributes(self):
  178. with Diagram(name=os.path.join(self.name, "nodes_to_node_with_additional_attributes"), show=False):
  179. with Cluster():
  180. node1 = Node("node1")
  181. nodes = [Node("node2"), Node("node3")]
  182. self.assertEqual(nodes - Edge(color="red") - Edge(color="green") - node1, node1)
  183. def test_node_to_node_with_attributes(self):
  184. with Diagram(name=os.path.join(self.name, "node_to_node_with_attributes"), show=False):
  185. with Cluster():
  186. node1 = Node("node1")
  187. node2 = Node("node2")
  188. self.assertEqual(node1 << Edge(color="red", label="1.1") << node2, node2)
  189. self.assertEqual(node1 >> Edge(color="green", label="1.2") >> node2, node2)
  190. self.assertEqual(node1 << Edge(color="blue", label="1.3") >> node2, node2)
  191. def test_node_to_node_with_additional_attributes(self):
  192. with Diagram(name=os.path.join(self.name, "node_to_node_with_additional_attributes"), show=False):
  193. with Cluster():
  194. node1 = Node("node1")
  195. node2 = Node("node2")
  196. self.assertEqual(node1 << Edge(color="red", label="2.1") << Edge(color="blue") << node2, node2)
  197. self.assertEqual(node1 >> Edge(color="green", label="2.2") >> Edge(color="red") >> node2, node2)
  198. self.assertEqual(node1 << Edge(color="blue", label="2.3") >> Edge(color="black") >> node2, node2)
  199. def test_nodes_to_node_with_attributes_loop(self):
  200. with Diagram(name=os.path.join(self.name, "nodes_to_node_with_attributes_loop"), show=False):
  201. with Cluster():
  202. node = Node("node")
  203. self.assertEqual(node >> Edge(color="red", label="3.1") >> node, node)
  204. self.assertEqual(node << Edge(color="green", label="3.2") << node, node)
  205. self.assertEqual(node >> Edge(color="blue", label="3.3") << node, node)
  206. self.assertEqual(node << Edge(color="pink", label="3.4") >> node, node)
  207. def test_nodes_to_node_with_attributes_bothdirectional(self):
  208. with Diagram(
  209. name=os.path.join(self.name, "nodes_to_node_with_attributes_bothdirectional"), show=False
  210. ) as diagram:
  211. with Cluster():
  212. node1 = Node("node1")
  213. nodes = [Node("node2"), Node("node3")]
  214. self.assertEqual(nodes << Edge(color="green", label="4") >> node1, node1)
  215. def test_nodes_to_node_with_attributes_bidirectional(self):
  216. with Diagram(name=os.path.join(self.name, "nodes_to_node_with_attributes_bidirectional"), show=False):
  217. with Cluster():
  218. node1 = Node("node1")
  219. nodes = [Node("node2"), Node("node3")]
  220. self.assertEqual(nodes << Edge(color="blue", label="5") >> node1, node1)
  221. def test_nodes_to_node_with_attributes_onedirectional(self):
  222. with Diagram(name=os.path.join(self.name, "nodes_to_node_with_attributes_onedirectional"), show=False):
  223. with Cluster():
  224. node1 = Node("node1")
  225. nodes = [Node("node2"), Node("node3")]
  226. self.assertEqual(nodes >> Edge(color="red", label="6.1") >> node1, node1)
  227. self.assertEqual(nodes << Edge(color="green", label="6.2") << node1, node1)
  228. def test_nodes_to_node_with_additional_attributes_directional(self):
  229. with Diagram(name=os.path.join(self.name, "nodes_to_node_with_additional_attributes_directional"), show=False):
  230. with Cluster():
  231. node1 = Node("node1")
  232. nodes = [Node("node2"), Node("node3")]
  233. self.assertEqual(
  234. nodes >> Edge(color="red", label="6.1") >> Edge(color="blue", label="6.2") >> node1, node1
  235. )
  236. self.assertEqual(
  237. nodes << Edge(color="green", label="6.3") << Edge(color="pink", label="6.4") << node1, node1
  238. )