Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

test_diagram.py 12 KiB

4 år sedan
4 år sedan
4 år sedan
4 år sedan
4 år sedan
4 år sedan
4 år sedan
4 år sedan
4 år sedan
4 år sedan
4 år sedan
4 år sedan
4 år sedan
4 år sedan
4 år sedan
4 år sedan
4 år sedan
4 år sedan
4 år sedan
4 år sedan
4 år sedan
4 år sedan
4 år sedan
4 år sedan
4 år sedan
4 år sedan
4 år sedan
4 år sedan
4 år sedan
4 år sedan
4 år sedan
4 år sedan
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. import os
  2. import shutil
  3. import unittest
  4. from diagrams import Cluster, Diagram, Edge, Node
  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. def test_empty_name(self):
  86. """Check that providing an empty name don't crash, but save in a diagrams_image.xxx file."""
  87. self.name = 'diagrams_image'
  88. with Diagram(show=False):
  89. Node("node1")
  90. self.assertTrue(os.path.exists(f"{self.name}.png"))
  91. class ClusterTest(unittest.TestCase):
  92. def setUp(self):
  93. self.name = "cluster_test"
  94. def tearDown(self):
  95. setdiagram(None)
  96. setcluster(None)
  97. # Only some tests generate the image file.
  98. try:
  99. shutil.rmtree(self.name)
  100. except OSError:
  101. pass
  102. def test_validate_direction(self):
  103. # Normal directions.
  104. for dir in ("TB", "BT", "LR", "RL"):
  105. with Diagram(name=os.path.join(self.name, "validate_direction"), show=False):
  106. Cluster(direction=dir)
  107. # Invalid directions.
  108. for dir in ("BR", "TL", "Unknown"):
  109. with self.assertRaises(ValueError):
  110. with Diagram(name=os.path.join(self.name, "validate_direction"), show=False):
  111. Cluster(direction=dir)
  112. def test_with_global_context(self):
  113. with Diagram(name=os.path.join(self.name, "with_global_context"), show=False):
  114. self.assertIsNone(getcluster())
  115. with Cluster():
  116. self.assertIsNotNone(getcluster())
  117. self.assertIsNone(getcluster())
  118. def test_with_nested_cluster(self):
  119. with Diagram(name=os.path.join(self.name, "with_nested_cluster"), show=False):
  120. self.assertIsNone(getcluster())
  121. with Cluster() as c1:
  122. self.assertEqual(c1, getcluster())
  123. with Cluster() as c2:
  124. self.assertEqual(c2, getcluster())
  125. self.assertEqual(c1, getcluster())
  126. self.assertIsNone(getcluster())
  127. def test_node_not_in_diagram(self):
  128. # Node must be belong to a diagrams.
  129. with self.assertRaises(EnvironmentError):
  130. Node("node")
  131. def test_node_to_node(self):
  132. with Diagram(name=os.path.join(self.name, "node_to_node"), show=False):
  133. with Cluster():
  134. node1 = Node("node1")
  135. node2 = Node("node2")
  136. self.assertEqual(node1 - node2, node2)
  137. self.assertEqual(node1 >> node2, node2)
  138. self.assertEqual(node1 << node2, node2)
  139. def test_node_to_nodes(self):
  140. with Diagram(name=os.path.join(self.name, "node_to_nodes"), show=False):
  141. with Cluster():
  142. node1 = Node("node1")
  143. nodes = [Node("node2"), Node("node3")]
  144. self.assertEqual(node1 - nodes, nodes)
  145. self.assertEqual(node1 >> nodes, nodes)
  146. self.assertEqual(node1 << nodes, nodes)
  147. def test_nodes_to_node(self):
  148. with Diagram(name=os.path.join(self.name, "nodes_to_node"), show=False):
  149. with Cluster():
  150. node1 = Node("node1")
  151. nodes = [Node("node2"), Node("node3")]
  152. self.assertEqual(nodes - node1, node1)
  153. self.assertEqual(nodes >> node1, node1)
  154. self.assertEqual(nodes << node1, node1)
  155. class EdgeTest(unittest.TestCase):
  156. def setUp(self):
  157. self.name = "edge_test"
  158. def tearDown(self):
  159. setdiagram(None)
  160. setcluster(None)
  161. # Only some tests generate the image file.
  162. try:
  163. shutil.rmtree(self.name)
  164. except OSError:
  165. pass
  166. def test_node_to_node(self):
  167. with Diagram(name=os.path.join(self.name, "node_to_node"), show=False):
  168. node1 = Node("node1")
  169. node2 = Node("node2")
  170. self.assertEqual(node1 - Edge(color="red") - node2, node2)
  171. def test_node_to_nodes(self):
  172. with Diagram(name=os.path.join(self.name, "node_to_nodes"), show=False):
  173. with Cluster():
  174. node1 = Node("node1")
  175. nodes = [Node("node2"), Node("node3")]
  176. self.assertEqual(node1 - Edge(color="red") - nodes, nodes)
  177. def test_nodes_to_node(self):
  178. with Diagram(name=os.path.join(self.name, "nodes_to_node"), show=False):
  179. with Cluster():
  180. node1 = Node("node1")
  181. nodes = [Node("node2"), Node("node3")]
  182. self.assertEqual(nodes - Edge(color="red") - node1, node1)
  183. def test_nodes_to_node_with_additional_attributes(self):
  184. with Diagram(name=os.path.join(self.name, "nodes_to_node_with_additional_attributes"), show=False):
  185. with Cluster():
  186. node1 = Node("node1")
  187. nodes = [Node("node2"), Node("node3")]
  188. self.assertEqual(nodes - Edge(color="red") - Edge(color="green") - node1, node1)
  189. def test_node_to_node_with_attributes(self):
  190. with Diagram(name=os.path.join(self.name, "node_to_node_with_attributes"), show=False):
  191. with Cluster():
  192. node1 = Node("node1")
  193. node2 = Node("node2")
  194. self.assertEqual(node1 << Edge(color="red", label="1.1") << node2, node2)
  195. self.assertEqual(node1 >> Edge(color="green", label="1.2") >> node2, node2)
  196. self.assertEqual(node1 << Edge(color="blue", label="1.3") >> node2, node2)
  197. def test_node_to_node_with_additional_attributes(self):
  198. with Diagram(name=os.path.join(self.name, "node_to_node_with_additional_attributes"), show=False):
  199. with Cluster():
  200. node1 = Node("node1")
  201. node2 = Node("node2")
  202. self.assertEqual(node1 << Edge(color="red", label="2.1") << Edge(color="blue") << node2, node2)
  203. self.assertEqual(node1 >> Edge(color="green", label="2.2") >> Edge(color="red") >> node2, node2)
  204. self.assertEqual(node1 << Edge(color="blue", label="2.3") >> Edge(color="black") >> node2, node2)
  205. def test_nodes_to_node_with_attributes_loop(self):
  206. with Diagram(name=os.path.join(self.name, "nodes_to_node_with_attributes_loop"), show=False):
  207. with Cluster():
  208. node = Node("node")
  209. self.assertEqual(node >> Edge(color="red", label="3.1") >> node, node)
  210. self.assertEqual(node << Edge(color="green", label="3.2") << node, node)
  211. self.assertEqual(node >> Edge(color="blue", label="3.3") << node, node)
  212. self.assertEqual(node << Edge(color="pink", label="3.4") >> node, node)
  213. def test_nodes_to_node_with_attributes_bothdirectional(self):
  214. with Diagram(name=os.path.join(self.name, "nodes_to_node_with_attributes_bothdirectional"), show=False):
  215. with Cluster():
  216. node1 = Node("node1")
  217. nodes = [Node("node2"), Node("node3")]
  218. self.assertEqual(nodes << Edge(color="green", label="4") >> node1, node1)
  219. def test_nodes_to_node_with_attributes_bidirectional(self):
  220. with Diagram(name=os.path.join(self.name, "nodes_to_node_with_attributes_bidirectional"), show=False):
  221. with Cluster():
  222. node1 = Node("node1")
  223. nodes = [Node("node2"), Node("node3")]
  224. self.assertEqual(nodes << Edge(color="blue", label="5") >> node1, node1)
  225. def test_nodes_to_node_with_attributes_onedirectional(self):
  226. with Diagram(name=os.path.join(self.name, "nodes_to_node_with_attributes_onedirectional"), show=False):
  227. with Cluster():
  228. node1 = Node("node1")
  229. nodes = [Node("node2"), Node("node3")]
  230. self.assertEqual(nodes >> Edge(color="red", label="6.1") >> node1, node1)
  231. self.assertEqual(nodes << Edge(color="green", label="6.2") << node1, node1)
  232. def test_nodes_to_node_with_additional_attributes_directional(self):
  233. with Diagram(name=os.path.join(self.name, "nodes_to_node_with_additional_attributes_directional"), show=False):
  234. with Cluster():
  235. node1 = Node("node1")
  236. nodes = [Node("node2"), Node("node3")]
  237. self.assertEqual(
  238. nodes >> Edge(color="red", label="6.1") >> Edge(color="blue", label="6.2") >> node1, node1
  239. )
  240. self.assertEqual(
  241. nodes << Edge(color="green", label="6.3") << Edge(color="pink", label="6.4") << node1, node1
  242. )