joseph

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