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.
 
 
 
 
 

318 regels
13 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", "tb"):
  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", "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", "PNG", "dot"):
  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. def test_autolabel(self):
  93. with Diagram(name=os.path.join(self.name, "nodes_to_node"), show=False):
  94. node1 = Node("node1")
  95. self.assertTrue(node1.label,"Node\nnode1")
  96. def test_outformat_list(self):
  97. """Check that outformat render all the files from the list."""
  98. self.name = 'diagrams_image'
  99. with Diagram(show=False, outformat=["dot", "png"]):
  100. Node("node1")
  101. # both files must exist
  102. self.assertTrue(os.path.exists(f"{self.name}.png"))
  103. self.assertTrue(os.path.exists(f"{self.name}.dot"))
  104. # clean the dot file as it only generated here
  105. os.remove(self.name + ".dot")
  106. class ClusterTest(unittest.TestCase):
  107. def setUp(self):
  108. self.name = "cluster_test"
  109. def tearDown(self):
  110. setdiagram(None)
  111. setcluster(None)
  112. # Only some tests generate the image file.
  113. try:
  114. shutil.rmtree(self.name)
  115. except OSError:
  116. pass
  117. def test_validate_direction(self):
  118. # Normal directions.
  119. for dir in ("TB", "BT", "LR", "RL"):
  120. with Diagram(name=os.path.join(self.name, "validate_direction"), show=False):
  121. Cluster(direction=dir)
  122. # Invalid directions.
  123. for dir in ("BR", "TL", "Unknown"):
  124. with self.assertRaises(ValueError):
  125. with Diagram(name=os.path.join(self.name, "validate_direction"), show=False):
  126. Cluster(direction=dir)
  127. def test_with_global_context(self):
  128. with Diagram(name=os.path.join(self.name, "with_global_context"), show=False):
  129. self.assertEqual(getcluster(), getdiagram())
  130. with Cluster():
  131. self.assertNotEqual(getcluster(), getdiagram())
  132. self.assertEqual(getcluster(), getdiagram())
  133. def test_with_nested_cluster(self):
  134. with Diagram(name=os.path.join(self.name, "with_nested_cluster"), show=False):
  135. self.assertEqual(getcluster(), getdiagram())
  136. with Cluster() as c1:
  137. self.assertEqual(c1, getcluster())
  138. with Cluster() as c2:
  139. self.assertEqual(c2, getcluster())
  140. self.assertEqual(c1, getcluster())
  141. self.assertEqual(getcluster(), getdiagram())
  142. def test_node_not_in_diagram(self):
  143. # Node must be belong to a diagrams.
  144. with self.assertRaises(EnvironmentError):
  145. Node("node")
  146. def test_node_to_node(self):
  147. with Diagram(name=os.path.join(self.name, "node_to_node"), show=False):
  148. with Cluster():
  149. node1 = Node("node1")
  150. node2 = Node("node2")
  151. self.assertEqual(node1 - node2, node2)
  152. self.assertEqual(node1 >> node2, node2)
  153. self.assertEqual(node1 << node2, node2)
  154. def test_node_to_nodes(self):
  155. with Diagram(name=os.path.join(self.name, "node_to_nodes"), show=False):
  156. with Cluster():
  157. node1 = Node("node1")
  158. nodes = [Node("node2"), Node("node3")]
  159. self.assertEqual(node1 - nodes, nodes)
  160. self.assertEqual(node1 >> nodes, nodes)
  161. self.assertEqual(node1 << nodes, nodes)
  162. def test_nodes_to_node(self):
  163. with Diagram(name=os.path.join(self.name, "nodes_to_node"), show=False):
  164. with Cluster():
  165. node1 = Node("node1")
  166. nodes = [Node("node2"), Node("node3")]
  167. self.assertEqual(nodes - node1, node1)
  168. self.assertEqual(nodes >> node1, node1)
  169. self.assertEqual(nodes << node1, node1)
  170. class EdgeTest(unittest.TestCase):
  171. def setUp(self):
  172. self.name = "edge_test"
  173. def tearDown(self):
  174. setdiagram(None)
  175. setcluster(None)
  176. # Only some tests generate the image file.
  177. try:
  178. shutil.rmtree(self.name)
  179. except OSError:
  180. pass
  181. def test_node_to_node(self):
  182. with Diagram(name=os.path.join(self.name, "node_to_node"), show=False):
  183. node1 = Node("node1")
  184. node2 = Node("node2")
  185. self.assertEqual(node1 - Edge(color="red") - node2, node2)
  186. def test_node_to_nodes(self):
  187. with Diagram(name=os.path.join(self.name, "node_to_nodes"), show=False):
  188. with Cluster():
  189. node1 = Node("node1")
  190. nodes = [Node("node2"), Node("node3")]
  191. self.assertEqual(node1 - Edge(color="red") - nodes, nodes)
  192. def test_nodes_to_node(self):
  193. with Diagram(name=os.path.join(self.name, "nodes_to_node"), show=False):
  194. with Cluster():
  195. node1 = Node("node1")
  196. nodes = [Node("node2"), Node("node3")]
  197. self.assertEqual(nodes - Edge(color="red") - node1, node1)
  198. def test_nodes_to_node_with_additional_attributes(self):
  199. with Diagram(name=os.path.join(self.name, "nodes_to_node_with_additional_attributes"), show=False):
  200. with Cluster():
  201. node1 = Node("node1")
  202. nodes = [Node("node2"), Node("node3")]
  203. self.assertEqual(nodes - Edge(color="red") - Edge(color="green") - node1, node1)
  204. def test_node_to_node_with_attributes(self):
  205. with Diagram(name=os.path.join(self.name, "node_to_node_with_attributes"), show=False):
  206. with Cluster():
  207. node1 = Node("node1")
  208. node2 = Node("node2")
  209. self.assertEqual(node1 << Edge(color="red", label="1.1") << node2, node2)
  210. self.assertEqual(node1 >> Edge(color="green", label="1.2") >> node2, node2)
  211. self.assertEqual(node1 << Edge(color="blue", label="1.3") >> node2, node2)
  212. def test_node_to_node_with_additional_attributes(self):
  213. with Diagram(name=os.path.join(self.name, "node_to_node_with_additional_attributes"), show=False):
  214. with Cluster():
  215. node1 = Node("node1")
  216. node2 = Node("node2")
  217. self.assertEqual(node1 << Edge(color="red", label="2.1") << Edge(color="blue") << node2, node2)
  218. self.assertEqual(node1 >> Edge(color="green", label="2.2") >> Edge(color="red") >> node2, node2)
  219. self.assertEqual(node1 << Edge(color="blue", label="2.3") >> Edge(color="black") >> node2, node2)
  220. def test_nodes_to_node_with_attributes_loop(self):
  221. with Diagram(name=os.path.join(self.name, "nodes_to_node_with_attributes_loop"), show=False):
  222. with Cluster():
  223. node = Node("node")
  224. self.assertEqual(node >> Edge(color="red", label="3.1") >> node, node)
  225. self.assertEqual(node << Edge(color="green", label="3.2") << node, node)
  226. self.assertEqual(node >> Edge(color="blue", label="3.3") << node, node)
  227. self.assertEqual(node << Edge(color="pink", label="3.4") >> node, node)
  228. def test_nodes_to_node_with_attributes_bothdirectional(self):
  229. with Diagram(name=os.path.join(self.name, "nodes_to_node_with_attributes_bothdirectional"), show=False):
  230. with Cluster():
  231. node1 = Node("node1")
  232. nodes = [Node("node2"), Node("node3")]
  233. self.assertEqual(nodes << Edge(color="green", label="4") >> node1, node1)
  234. def test_nodes_to_node_with_attributes_bidirectional(self):
  235. with Diagram(name=os.path.join(self.name, "nodes_to_node_with_attributes_bidirectional"), show=False):
  236. with Cluster():
  237. node1 = Node("node1")
  238. nodes = [Node("node2"), Node("node3")]
  239. self.assertEqual(nodes << Edge(color="blue", label="5") >> node1, node1)
  240. def test_nodes_to_node_with_attributes_onedirectional(self):
  241. with Diagram(name=os.path.join(self.name, "nodes_to_node_with_attributes_onedirectional"), show=False):
  242. with Cluster():
  243. node1 = Node("node1")
  244. nodes = [Node("node2"), Node("node3")]
  245. self.assertEqual(nodes >> Edge(color="red", label="6.1") >> node1, node1)
  246. self.assertEqual(nodes << Edge(color="green", label="6.2") << node1, node1)
  247. def test_nodes_to_node_with_additional_attributes_directional(self):
  248. with Diagram(name=os.path.join(self.name, "nodes_to_node_with_additional_attributes_directional"), show=False):
  249. with Cluster():
  250. node1 = Node("node1")
  251. nodes = [Node("node2"), Node("node3")]
  252. self.assertEqual(
  253. nodes >> Edge(color="red", label="6.1") >> Edge(color="blue", label="6.2") >> node1, node1
  254. )
  255. self.assertEqual(
  256. nodes << Edge(color="green", label="6.3") << Edge(color="pink", label="6.4") << node1, node1
  257. )
  258. class ResourcesTest(unittest.TestCase):
  259. def test_folder_depth(self):
  260. """
  261. The code currently only handles resource folders up to a dir depth of 2
  262. i.e. resources/<provider>/<type>/<image>, so check that this depth isn't
  263. exceeded.
  264. """
  265. resources_dir = pathlib.Path(__file__).parent.parent / 'resources'
  266. max_depth = max(os.path.relpath(d, resources_dir).count(os.sep) + 1
  267. for d, _, _ in os.walk(resources_dir))
  268. self.assertLessEqual(max_depth, 2)