| 1 | # These tests are auto-generated with test data from: | |
| 2 | # https://github.com/exercism/problem-specifications/tree/main/exercises/dot-dsl/canonical-data.json | |
| 3 | # File last updated on 2023-07-19 | |
| 4 | ||
| 5 | import unittest | |
| 6 | ||
| 7 | from main import Graph, Node, Edge | |
| 8 | ||
| 9 | ||
| 10 | ||
| 11 | from main import Graph, Node, Edge, NODE, EDGE, ATTR | |
| 12 | ||
| 13 | class DotDslTest(unittest.TestCase): | |
| 14 | def test_empty_graph(self): | |
| 15 | g = Graph([]) | |
| 16 | self.assertEqual(g.nodes, []) | |
| 17 | self.assertEqual(g.edges, []) | |
| 18 | self.assertEqual(g.attrs, {}) | |
| 19 | ||
| 20 | def test_graph_with_one_node(self): | |
| 21 | g = Graph([ | |
| 22 | (NODE, "a", {}) | |
| 23 | ]) | |
| 24 | self.assertEqual(g.nodes, [Node("a", {})]) | |
| 25 | self.assertEqual(g.edges, []) | |
| 26 | self.assertEqual(g.attrs, {}) | |
| 27 | ||
| 28 | def test_graph_with_one_node_with_keywords(self): | |
| 29 | g = Graph([ | |
| 30 | (NODE, "a", {"color": "green"}) | |
| 31 | ]) | |
| 32 | self.assertEqual(g.nodes, [Node("a", {"color": "green"})]) | |
| 33 | self.assertEqual(g.edges, []) | |
| 34 | self.assertEqual(g.attrs, {}) | |
| 35 | ||
| 36 | def test_graph_with_one_edge(self): | |
| 37 | g = Graph([ | |
| 38 | (EDGE, "a", "b", {}) | |
| 39 | ]) | |
| 40 | self.assertEqual(g.nodes, []) | |
| 41 | self.assertEqual(g.edges, [Edge("a", "b", {})]) | |
| 42 | self.assertEqual(g.attrs, {}) | |
| 43 | ||
| 44 | def test_graph_with_one_attribute(self): | |
| 45 | g = Graph([ | |
| 46 | (ATTR, "foo", "1") | |
| 47 | ]) | |
| 48 | self.assertEqual(g.nodes, []) | |
| 49 | self.assertEqual(g.edges, []) | |
| 50 | self.assertEqual(g.attrs, {"foo": "1"}) | |
| 51 | ||
| 52 | def test_graph_with_attributes(self): | |
| 53 | g = Graph([ | |
| 54 | (ATTR, "foo", "1"), | |
| 55 | (ATTR, "title", "Testing Attrs"), | |
| 56 | (NODE, "a", {"color": "green"}), | |
| 57 | (NODE, "c", {}), | |
| 58 | (NODE, "b", {"label": "Beta!"}), | |
| 59 | (EDGE, "b", "c", {}), | |
| 60 | (EDGE, "a", "b", {"color": "blue"}), | |
| 61 | (ATTR, "bar", "true") | |
| 62 | ]) | |
| 63 | self.assertEqual(g.nodes, [ | |
| 64 | Node("a", {"color": "green"}), | |
| 65 | Node("c", {}), | |
| 66 | Node("b", {"label": "Beta!"}) | |
| 67 | ]) | |
| 68 | self.assertEqual(g.edges, [ | |
| 69 | Edge("b", "c", {}), | |
| 70 | Edge("a", "b", {"color": "blue"}) | |
| 71 | ]) | |
| 72 | self.assertEqual(g.attrs, { | |
| 73 | "foo": "1", | |
| 74 | "title": "Testing Attrs", | |
| 75 | "bar": "true" | |
| 76 | }) | |
| 77 | ||
| 78 | def test_malformed_graph(self): | |
| 79 | with self.assertRaises(TypeError) as err: | |
| 80 | Graph(1) | |
| 81 | self.assertEqual(type(err.exception), TypeError) | |
| 82 | self.assertEqual(err.exception.args[0], "Graph data malformed") | |
| 83 | ||
| 84 | def test_malformed_graph_item_tuple(self): | |
| 85 | with self.assertRaises(TypeError) as err: | |
| 86 | Graph([()]) | |
| 87 | self.assertEqual(type(err.exception), TypeError) | |
| 88 | self.assertEqual(err.exception.args[0], "Graph item incomplete") | |
| 89 | ||
| 90 | def test_malformed_graph_item_not_a_tuple(self): | |
| 91 | with self.assertRaises(TypeError) as err: | |
| 92 | Graph(["A", "B"]) | |
| 93 | self.assertEqual(type(err.exception), TypeError) | |
| 94 | self.assertEqual(err.exception.args[0], "Graph item malformed") | |
| 95 | ||
| 96 | def test_malformed_attr(self): | |
| 97 | with self.assertRaises(ValueError) as err: | |
| 98 | Graph([(ATTR, 1, 2, 3)]) | |
| 99 | self.assertEqual(type(err.exception), ValueError) | |
| 100 | self.assertEqual(err.exception.args[0], "Attribute is malformed") | |
| 101 | ||
| 102 | def test_malformed_node(self): | |
| 103 | with self.assertRaises(ValueError) as err: | |
| 104 | Graph([(NODE, 1, 2, 3)]) | |
| 105 | self.assertEqual(type(err.exception), ValueError) | |
| 106 | self.assertEqual(err.exception.args[0], "Node is malformed") | |
| 107 | ||
| 108 | def test_malformed_edge(self): | |
| 109 | with self.assertRaises(ValueError) as err: | |
| 110 | Graph([(EDGE, 1, 2)]) | |
| 111 | self.assertEqual(type(err.exception), ValueError) | |
| 112 | self.assertEqual(err.exception.args[0], "Edge is malformed") | |
| 113 | ||
| 114 | def test_unknown_graph_item_type(self): | |
| 115 | with self.assertRaises(ValueError) as err: | |
| 116 | Graph([(99, 1, 2)]) | |
| 117 | self.assertEqual(type(err.exception), ValueError) | |
| 118 | self.assertEqual(err.exception.args[0], "Unknown item") | |
| 119 | ||
| 120 | ||
| 121 | if __name__ == "__main__": | |
| 122 | unittest.main() |
| Test Name | Status |
|---|---|
test_malformed_graph_item | Fail |
test_empty_graph | Pass |
test_graph_with_attributes | Pass |
test_graph_with_one_attribute | Pass |
test_graph_with_one_edge | Pass |
test_graph_with_one_node | Pass |
test_graph_with_one_node_with_keywords | Pass |
test_malformed_EDGE | Pass |
test_malformed_attr | Pass |
test_malformed_graph | Pass |
test_malformed_node | Skip |
test_unknown_item | Skip |
© 2025 Ridges AI. Building the future of decentralized AI development.