|
| 1 | +import unittest |
| 2 | + |
| 3 | +from dot_dsl import Graph, Node, Edge, NODE, EDGE, ATTR |
| 4 | + |
| 5 | + |
| 6 | +class DotDslTest(unittest.TestCase): |
| 7 | + def test_empty_graph(self): |
| 8 | + g = Graph() |
| 9 | + |
| 10 | + self.assertEqual(g.nodes, []) |
| 11 | + self.assertEqual(g.edges, []) |
| 12 | + self.assertEqual(g.attrs, {}) |
| 13 | + |
| 14 | + def test_graph_with_one_node(self): |
| 15 | + g = Graph([ |
| 16 | + (NODE, "a", {}) |
| 17 | + ]) |
| 18 | + |
| 19 | + self.assertEqual(g.nodes, [Node("a")]) |
| 20 | + self.assertEqual(g.edges, []) |
| 21 | + self.assertEqual(g.attrs, {}) |
| 22 | + |
| 23 | + def test_graph_with_one_node_with_keywords(self): |
| 24 | + g = Graph([ |
| 25 | + (NODE, "a", {"color": "green"}) |
| 26 | + ]) |
| 27 | + |
| 28 | + self.assertEqual(g.nodes, [Node("a", {"color": "green"})]) |
| 29 | + self.assertEqual(g.edges, []) |
| 30 | + self.assertEqual(g.attrs, {}) |
| 31 | + |
| 32 | + def test_graph_with_one_edge(self): |
| 33 | + g = Graph([ |
| 34 | + (EDGE, "a", "b", {}) |
| 35 | + ]) |
| 36 | + |
| 37 | + self.assertEqual(g.nodes, []) |
| 38 | + self.assertEqual(g.edges, [Edge("a", "b", {})]) |
| 39 | + self.assertEqual(g.attrs, {}) |
| 40 | + |
| 41 | + def test_graph_with_one_attribute(self): |
| 42 | + g = Graph([ |
| 43 | + (ATTR, "foo", "1") |
| 44 | + ]) |
| 45 | + |
| 46 | + self.assertEqual(g.nodes, []) |
| 47 | + self.assertEqual(g.edges, []) |
| 48 | + self.assertEqual(g.attrs, {"foo": "1"}) |
| 49 | + |
| 50 | + def test_graph_with_attributes(self): |
| 51 | + g = Graph([ |
| 52 | + (ATTR, "foo", "1"), |
| 53 | + (ATTR, "title", "Testing Attrs"), |
| 54 | + (NODE, "a", {"color": "green"}), |
| 55 | + (NODE, "c", {}), |
| 56 | + (NODE, "b", {"label", "Beta!"}), |
| 57 | + (EDGE, "b", "c", {}), |
| 58 | + (EDGE, "a", "b", {"color": "blue"}), |
| 59 | + (ATTR, "bar", "true") |
| 60 | + ]) |
| 61 | + |
| 62 | + self.assertEqual(g.nodes, [Node("a", {"color": "green"}), |
| 63 | + Node("c", {}), |
| 64 | + Node("b", {"label", "Beta!"})]) |
| 65 | + self.assertEqual(g.edges, [Edge("b", "c", {}), |
| 66 | + Edge("a", "b", {"color": "blue"})]) |
| 67 | + self.assertEqual(g.attrs, { |
| 68 | + "foo": "1", |
| 69 | + "title": "Testing Attrs", |
| 70 | + "bar": "true" |
| 71 | + }) |
| 72 | + |
| 73 | + def test_malformed_graph(self): |
| 74 | + with self.assertRaises(TypeError): |
| 75 | + Graph(1) |
| 76 | + |
| 77 | + with self.assertRaises(TypeError): |
| 78 | + Graph("problematic") |
| 79 | + |
| 80 | + def test_malformed_graph_item(self): |
| 81 | + with self.assertRaises(TypeError): |
| 82 | + Graph([ |
| 83 | + () |
| 84 | + ]) |
| 85 | + |
| 86 | + with self.assertRaises(TypeError): |
| 87 | + Graph([ |
| 88 | + (ATTR, ) |
| 89 | + ]) |
| 90 | + |
| 91 | + def test_malformed_attr(self): |
| 92 | + with self.assertRaises(ValueError): |
| 93 | + Graph([ |
| 94 | + (ATTR, 1, 2, 3) |
| 95 | + ]) |
| 96 | + |
| 97 | + def test_malformed_node(self): |
| 98 | + with self.assertRaises(ValueError): |
| 99 | + Graph([ |
| 100 | + (NODE, 1, 2, 3) |
| 101 | + ]) |
| 102 | + |
| 103 | + def test_malformed_EDGE(self): |
| 104 | + with self.assertRaises(ValueError): |
| 105 | + Graph([ |
| 106 | + (EDGE, 1, 2) |
| 107 | + ]) |
| 108 | + |
| 109 | + def test_unknown_item(self): |
| 110 | + with self.assertRaises(ValueError): |
| 111 | + Graph([ |
| 112 | + (99, 1, 2) |
| 113 | + ]) |
| 114 | + |
| 115 | + |
| 116 | +if __name__ == '__main__': |
| 117 | + unittest.main() |
0 commit comments