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