您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 

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