fortitude

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 | None): 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: Handle None input by initializing empty collections
38 if data is None:
39 self.nodes = []
40 self.edges = []
41 self.attrs = {}
42 return
43
44 # Edge Case: Ensure data is a list
45 if not isinstance(data, list):
46 raise TypeError("Graph data malformed")
47
48 # Initialize graph components
49 self.nodes = []
50 self.edges = []
51 self.attrs = {}
52
53 # Process each item in the data list
54 for item in data:
55 # Edge Case: Ensure each item is a tuple
56 if not isinstance(item, tuple):
57 raise TypeError("Graph item malformed")
58
59 # Edge Case: Ensure tuple is not empty
60 if len(item) == 0:
61 raise TypeError("Graph item malformed")
62
63 item_type = item[0]
64
65 # Edge Case: Handle NODE type
66 if item_type == NODE:
67 # Edge Case: Validate NODE tuple structure (3 elements: type, name, attrs)
68 if len(item) != 3:
69 raise TypeError("Graph item malformed")
70
71 name, attrs = item[1], item[2]
72 # Edge Case: Validate name is string and attrs is dict
73 if not isinstance(name, str) or not isinstance(attrs, dict):
74 raise TypeError("Node is malformed")
75
76 self.nodes.append(Node(name, attrs))
77
78 # Edge Case: Handle EDGE type
79 elif item_type == EDGE:
80 # Edge Case: Validate EDGE tuple structure (4 elements: type, src, dst, attrs)
81 if len(item) != 4:
82 raise TypeError("Graph item malformed")
83
84 src, dst, attrs = item[1], item[2], item[3]
85 # Edge Case: Validate src and dst are strings and attrs is dict
86 if not isinstance(src, str) or not isinstance(dst, str) or not isinstance(attrs, dict):
87 raise TypeError("Edge is malformed")
88
89 self.edges.append(Edge(src, dst, attrs))
90
91 # Edge Case: Handle ATTR type
92 elif item_type == ATTR:
93 # Edge Case: Validate ATTR tuple structure (3 elements: type, key, value)
94 if len(item) != 3:
95 raise TypeError("Graph item malformed")
96
97 key, value = item[1], item[2]
98 # Edge Case: Validate key and value are strings
99 if not isinstance(key, str) or not isinstance(value, str):
100 raise TypeError("Attribute is malformed")
101
102 self.attrs[key] = value
103
104 # Edge Case: Handle unknown item types
105 else:
106 raise ValueError("Unknown item")
107
108 # Handled Edge Cases: None input, non-list data, non-tuple items, empty tuples,
109 # incorrect tuple lengths for NODE/EDGE/ATTR, incorrect data types
110 # for NODE/EDGE/ATTR components, unknown item types
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.