jackson

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: Item is not a tuple
42 if not isinstance(item, tuple):
43 raise TypeError("Graph item malformed")
44
45 # Edge Case: Item is an empty tuple
46 if len(item) == 0:
47 raise TypeError("Graph item malformed")
48
49 item_type = item[0]
50
51 # Process attribute
52 if item_type == ATTR:
53 # Edge Case: Attribute tuple has wrong number of elements
54 if len(item) != 3:
55 raise TypeError("Graph item malformed")
56
57 _, key, value = item
58 # Edge Case: Attribute key or value is not a string
59 if not isinstance(key, str) or not isinstance(value, str):
60 raise ValueError("Attribute value must be a string")
61
62 self.attrs[key] = value
63
64 # Process node
65 elif item_type == NODE:
66 # Edge Case: Node tuple has wrong number of elements
67 if len(item) != 3:
68 raise TypeError("Graph item malformed")
69
70 _, name, attrs = item
71 # Edge Case: Node name is not a string
72 if not isinstance(name, str):
73 raise TypeError("Node name must be a string")
74
75 # Edge Case: Node attributes is not a dict
76 if not isinstance(attrs, dict):
77 raise TypeError("Node attributes must be a dict")
78
79 self.nodes.append(Node(name, attrs))
80
81 # Process edge
82 elif item_type == EDGE:
83 # Edge Case: Edge tuple has wrong number of elements
84 if len(item) != 4:
85 raise TypeError("Graph item malformed")
86
87 _, src, dst, attrs = item
88 # Edge Case: Edge source or destination is not a string
89 if not isinstance(src, str) or not isinstance(dst, str):
90 raise TypeError("Edge source and destination must be strings")
91
92 # Edge Case: Edge attributes is not a dict
93 if not isinstance(attrs, dict):
94 raise TypeError("Edge attributes must be a dict")
95
96 self.edges.append(Edge(src, dst, attrs))
97
98 # Edge Case: Unknown item type
99 else:
100 raise ValueError("Unknown item")
101 # Handled Edge Cases: Graph data is None; Graph data is not a list; Item is not a tuple; Item is an empty tuple; Attribute tuple has wrong number of elements; Attribute key or value is not a string; 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 or destination is not a string; Edge attributes is not a dict; 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.