You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

300 lines
12 KiB

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