awesome

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