couple

Finished
2424
2525class Graph:
2626 def __init__(self, data: list | None = None):
27 pass
27 """
28 Initialize a Graph with a list of data tuples.
29
30 Parameters:
31 data (list): A list of tuples representing graph elements.
32
33 Raises:
34 TypeError: If data is not a list or if any tuple 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 # Edge Case: 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 tuple is empty
56 if len(item) == 0:
57 raise TypeError("Graph item malformed")
58
59 item_type = item[0]
60
61 # Edge Case: Process ATTR item
62 if item_type == ATTR:
63 # Edge Case: ATTR tuple must have exactly 3 elements
64 if len(item) != 3:
65 raise TypeError("Graph item malformed")
66
67 _, key, value = item
68 self.attrs[key] = value
69
70 # Edge Case: Process NODE item
71 elif item_type == NODE:
72 # Edge Case: NODE tuple must have exactly 3 elements
73 if len(item) != 3:
74 raise TypeError("Graph item malformed")
75
76 _, name, attrs = item
77 # Edge Case: Node attributes must be a dictionary
78 if not isinstance(attrs, dict):
79 raise TypeError("Graph item malformed")
80
81 self.nodes.append(Node(name, attrs))
82
83 # Edge Case: Process EDGE item
84 elif item_type == EDGE:
85 # Edge Case: EDGE tuple must have exactly 4 elements
86 if len(item) != 4:
87 raise TypeError("Graph item malformed")
88
89 _, src, dst, attrs = item
90 # Edge Case: Edge attributes must be a dictionary
91 if not isinstance(attrs, dict):
92 raise TypeError("Graph item malformed")
93
94 self.edges.append(Edge(src, dst, attrs))
95
96 # Edge Case: Unknown item type
97 else:
98 raise ValueError("Unknown item")
99
100 # Handled Edge Cases: Data is None, Data is not a list, Item is not a tuple, Item tuple is empty, ATTR tuple must have exactly 3 elements, NODE tuple must have exactly 3 elements, Node attributes must be a dictionary, EDGE tuple must have exactly 4 elements, Edge attributes must be a dictionary, 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.