Merry

Finished
2323
2424
2525class Graph:
26 """
27 A class representing a graph DSL that can parse graph data.
28
29 The graph data is a list of tuples that describe attributes, nodes, and edges.
30 Each tuple has a specific structure based on its type (ATTR, NODE, EDGE).
31 """
32
2633 def __init__(self, data: list | None = None):
27 pass
34 """
35 Initialize a Graph with optional data.
36
37 Args:
38 data: A list of tuples representing graph elements.
39
40 Raises:
41 TypeError: If data is not a list or if any tuple is malformed.
42 ValueError: If an unknown item type is encountered.
43 """
44 # Edge Case: Data is None
45 if data is None:
46 data = []
47
48 # Edge Case: Data is not a list
49 if not isinstance(data, list):
50 raise TypeError("Graph data malformed")
51
52 self.attrs = {}
53 self.nodes = []
54 self.edges = []
55
56 # Edge Case: Process each item in the data list
57 for item in data:
58 # Edge Case: Item is not a tuple
59 if not isinstance(item, tuple):
60 raise TypeError("Graph item malformed")
61
62 # Edge Case: Item is an empty tuple
63 if len(item) == 0:
64 raise TypeError("Graph item malformed")
65
66 item_type = item[0]
67
68 # Edge Case: Process ATTR type
69 if item_type == ATTR:
70 # Edge Case: ATTR tuple doesn't have exactly 3 elements
71 if len(item) != 3:
72 raise TypeError("Graph item malformed")
73 key, value = item[1], item[2]
74 self.attrs[key] = value
75
76 # Edge Case: Process NODE type
77 elif item_type == NODE:
78 # Edge Case: NODE tuple doesn't have exactly 3 elements
79 if len(item) != 3:
80 raise TypeError("Graph item malformed")
81 name, attrs = item[1], item[2]
82 # Edge Case: Node attributes is not a dict
83 if not isinstance(attrs, dict):
84 raise TypeError("Node attributes must be a dict")
85 self.nodes.append(Node(name, attrs))
86
87 # Edge Case: Process EDGE type
88 elif item_type == EDGE:
89 # Edge Case: EDGE tuple doesn't have exactly 4 elements
90 if len(item) != 4:
91 raise TypeError("Graph item malformed")
92 src, dst, attrs = item[1], item[2], item[3]
93 # Edge Case: Edge attributes is not a dict
94 if not isinstance(attrs, dict):
95 raise TypeError("Edge attributes must be a dict")
96 self.edges.append(Edge(src, dst, attrs))
97
98 # Edge Case: Unknown item type
99 else:
100 raise ValueError("Unknown item")
101 # Handled Edge Cases: Data is None; Data is not a list; Item is not a tuple;
102 # Item is an empty tuple; ATTR tuple doesn't have exactly 3 elements;
103 # NODE tuple doesn't have exactly 3 elements; Node attributes is not a dict;
104 # EDGE tuple doesn't have exactly 4 elements; Edge attributes is not a dict;
105 # Unknown item type
Test NameStatus
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_graph_item
Pass
test_malformed_node
Pass
test_unknown_item
Pass

© 2025 Ridges AI. Building the future of decentralized AI development.