選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 

162 行
5.4 KiB

  1. import os
  2. import unittest
  3. from diagrams import Cluster, Diagram, Node
  4. from diagrams import getcluster, getdiagram, setcluster, setdiagram
  5. class DiagramTest(unittest.TestCase):
  6. def setUp(self):
  7. self.name = "test"
  8. def tearDown(self):
  9. setdiagram(None)
  10. setcluster(None)
  11. # Only some tests generate the image file.
  12. try:
  13. os.remove(self.name + ".png")
  14. except FileNotFoundError:
  15. pass
  16. def test_validate_direction(self):
  17. # Normal directions.
  18. for dir in ("TB", "BT", "LR", "RL"):
  19. Diagram(direction=dir)
  20. # Invalid directions.
  21. for dir in ("BR", "TL", "Unknown"):
  22. with self.assertRaises(ValueError):
  23. Diagram(direction=dir)
  24. def test_validate_outformat(self):
  25. # Normal output formats.
  26. for fmt in ("png", "jpg", "svg", "pdf"):
  27. Diagram(outformat=fmt)
  28. # Invalid output formats.
  29. for fmt in ("pnp", "jpe", "unknown"):
  30. with self.assertRaises(ValueError):
  31. Diagram(outformat=fmt)
  32. def test_with_global_context(self):
  33. self.assertIsNone(getdiagram())
  34. with Diagram(name=self.name, show=False):
  35. self.assertIsNotNone(getdiagram())
  36. self.assertIsNone(getdiagram())
  37. def test_node_not_in_diagram(self):
  38. # Node must be belong to a diagrams.
  39. with self.assertRaises(EnvironmentError):
  40. Node("node")
  41. def test_node_to_node(self):
  42. with Diagram(name=self.name, show=False):
  43. node1 = Node("node1")
  44. node2 = Node("node2")
  45. self.assertEqual(node1 - node2, node2)
  46. self.assertEqual(node1 >> node2, node2)
  47. self.assertEqual(node1 << node2, node2)
  48. def test_node_to_nodes(self):
  49. with Diagram(name=self.name, show=False):
  50. node1 = Node("node1")
  51. nodes = [Node("node2"), Node("node3")]
  52. self.assertEqual(node1 - nodes, nodes)
  53. self.assertEqual(node1 >> nodes, nodes)
  54. self.assertEqual(node1 << nodes, nodes)
  55. def test_nodes_to_node(self):
  56. with Diagram(name=self.name, show=False):
  57. node1 = Node("node1")
  58. nodes = [Node("node2"), Node("node3")]
  59. self.assertEqual(nodes - node1, node1)
  60. self.assertEqual(nodes >> node1, node1)
  61. self.assertEqual(nodes << node1, node1)
  62. def test_default_filename(self):
  63. self.name = "example_1"
  64. with Diagram(name="Example 1", show=False):
  65. Node("node1")
  66. self.assertTrue(os.path.exists(f"{self.name}.png"))
  67. def test_custom_filename(self):
  68. self.name = "my_custom_name"
  69. with Diagram(name="Example 1", filename=self.name, show=False):
  70. Node("node1")
  71. self.assertTrue(os.path.exists(f"{self.name}.png"))
  72. class ClusterTest(unittest.TestCase):
  73. def setUp(self):
  74. self.name = "test"
  75. def tearDown(self):
  76. setdiagram(None)
  77. setcluster(None)
  78. # Only some tests generate the image file.
  79. try:
  80. os.remove(self.name + ".png")
  81. except FileNotFoundError:
  82. pass
  83. def test_validate_direction(self):
  84. # Normal directions.
  85. for dir in ("TB", "BT", "LR", "RL"):
  86. with Diagram(name=self.name, show=False):
  87. Cluster(direction=dir)
  88. # Invalid directions.
  89. for dir in ("BR", "TL", "Unknown"):
  90. with self.assertRaises(ValueError):
  91. with Diagram(name=self.name, show=False):
  92. Cluster(direction=dir)
  93. def test_with_global_context(self):
  94. with Diagram(name=self.name, show=False):
  95. self.assertIsNone(getcluster())
  96. with Cluster():
  97. self.assertIsNotNone(getcluster())
  98. self.assertIsNone(getcluster())
  99. def test_with_nested_cluster(self):
  100. with Diagram(name=self.name, show=False):
  101. self.assertIsNone(getcluster())
  102. with Cluster() as c1:
  103. self.assertEqual(c1, getcluster())
  104. with Cluster() as c2:
  105. self.assertEqual(c2, getcluster())
  106. self.assertEqual(c1, getcluster())
  107. self.assertIsNone(getcluster())
  108. def test_node_not_in_diagram(self):
  109. # Node must be belong to a diagrams.
  110. with self.assertRaises(EnvironmentError):
  111. Node("node")
  112. def test_node_to_node(self):
  113. with Diagram(name=self.name, show=False):
  114. with Cluster():
  115. node1 = Node("node1")
  116. node2 = Node("node2")
  117. self.assertEqual(node1 - node2, node2)
  118. self.assertEqual(node1 >> node2, node2)
  119. self.assertEqual(node1 << node2, node2)
  120. def test_node_to_nodes(self):
  121. with Diagram(name=self.name, show=False):
  122. with Cluster():
  123. node1 = Node("node1")
  124. nodes = [Node("node2"), Node("node3")]
  125. self.assertEqual(node1 - nodes, nodes)
  126. self.assertEqual(node1 >> nodes, nodes)
  127. self.assertEqual(node1 << nodes, nodes)
  128. def test_nodes_to_node(self):
  129. with Diagram(name=self.name, show=False):
  130. with Cluster():
  131. node1 = Node("node1")
  132. nodes = [Node("node2"), Node("node3")]
  133. self.assertEqual(nodes - node1, node1)
  134. self.assertEqual(nodes >> node1, node1)
  135. self.assertEqual(nodes << node1, node1)