xas

Finished
2424
2525class Graph:
2626 def __init__(self, data: list | None = None):
27 pass
27 """
28 Initialize a Graph with optional data.
29
30 Parameters:
31 data (list | None): A list of tuples describing the graph. Each tuple should be of the form:
32 - (ATTR, key, value) for graph attributes
33 - (NODE, name, attrs) for nodes
34 - (EDGE, src, dst, attrs) for edges
35
36 Raises:
37 TypeError: If data is not a list or if any tuple is malformed.
38 ValueError: If an unknown item type is encountered.
39 """
40 # Edge Case: Data is None
41 if data is None:
42 data = []
43
44 # Edge Case: Data is not a list
45 if not isinstance(data, list):
46 raise TypeError("Graph data malformed")
47
48 self.attrs = {}
49 self.nodes = []
50 self.edges = []
51
52 # Edge Case: Process each item in the data list
53 for item in data:
54 # Edge Case: Item is not a tuple
55 if not isinstance(item, tuple):
56 raise TypeError("Graph item malformed")
57
58 # Edge Case: Item type identifier is missing
59 if len(item) == 0:
60 raise TypeError("Graph item malformed")
61
62 item_type = item[0]
63
64 # Edge Case: Processing ATTR items
65 if item_type == ATTR:
66 # Edge Case: ATTR tuple has incorrect length
67 if len(item) != 3:
68 raise TypeError("Graph item malformed")
69
70 _, key, value = item
71 self.attrs[key] = value
72
73 # Edge Case: Processing NODE items
74 elif item_type == NODE:
75 # Edge Case: NODE tuple has incorrect length
76 if len(item) != 3:
77 raise TypeError("Graph item malformed")
78
79 _, name, attrs = item
80 # Edge Case: Node attributes is not a dict
81 if not isinstance(attrs, dict):
82 raise TypeError("Graph item malformed")
83
84 self.nodes.append(Node(name, attrs))
85
86 # Edge Case: Processing EDGE items
87 elif item_type == EDGE:
88 # Edge Case: EDGE tuple has incorrect length
89 if len(item) != 4:
90 raise TypeError("Graph item malformed")
91
92 _, src, dst, attrs = item
93 # Edge Case: Edge attributes is not a dict
94 if not isinstance(attrs, dict):
95 raise TypeError("Graph item malformed")
96
97 self.edges.append(Edge(src, dst, attrs))
98
99 # Edge Case: Unknown item type
100 else:
101 raise ValueError("Unknown item")
102
103 # Handled Edge Cases: Data is None; Data is not a list; Item is not a tuple; Item type identifier is missing; ATTR tuple has incorrect length; NODE tuple has incorrect length; Node attributes is not a dict; EDGE tuple has incorrect length; Edge attributes is not a dict; 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.