corpus

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 representing graph elements.
32
33 Raises:
34 TypeError: If data is not a list or if any item is malformed.
35 ValueError: If an unknown item type is encountered.
36 """
37 # Edge Case: Data is None
38 if data is None:
39 data = []
40
41 # Edge Case: Data is not a list
42 if not isinstance(data, list):
43 raise TypeError("Graph data malformed")
44
45 self.nodes = []
46 self.edges = []
47 self.attrs = {}
48
49 # Process each item in the data list
50 for item in data:
51 # Edge Case: Item is not a tuple
52 if not isinstance(item, tuple):
53 raise TypeError("Graph item malformed")
54
55 # Edge Case: Item is empty
56 if len(item) == 0:
57 raise TypeError("Graph item malformed")
58
59 item_type = item[0]
60
61 # Edge Case: Unknown item type
62 if item_type not in (NODE, EDGE, ATTR):
63 raise ValueError("Unknown item")
64
65 # Edge Case: ATTR item with incorrect number of elements
66 if item_type == ATTR:
67 if len(item) != 3:
68 raise TypeError("Graph item malformed")
69 _, key, value = item
70 self.attrs[key] = value
71
72 # Edge Case: NODE item with incorrect number of elements
73 elif item_type == NODE:
74 if len(item) != 3:
75 raise TypeError("Graph item malformed")
76 _, name, attrs = item
77 # Edge Case: Node attributes is not a dict
78 if not isinstance(attrs, dict):
79 raise TypeError("Graph item malformed")
80 self.nodes.append(Node(name, attrs))
81
82 # Edge Case: EDGE item with incorrect number of elements
83 elif item_type == EDGE:
84 if len(item) != 4:
85 raise TypeError("Graph item malformed")
86 _, src, dst, attrs = item
87 # Edge Case: Edge attributes is not a dict
88 if not isinstance(attrs, dict):
89 raise TypeError("Graph item malformed")
90 self.edges.append(Edge(src, dst, attrs))
91
92 # Handled Edge Cases: Data is None; Data is not a list; Item is not a tuple; Item is empty; Unknown item type; ATTR item with incorrect number of elements; NODE item with incorrect number of elements; Node attributes is not a dict; EDGE item with incorrect number of elements; Edge attributes is not a dict
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.