viola

Finished
2424
2525class Graph:
2626 def __init__(self, data: list | None = None):
27 pass
27 """
28 Initialize a Graph with optional data.
29
30 Parameters:
31 data (list | None): A list of tuples describing the graph. Each tuple should have a type (NODE, EDGE, or ATTR)
32 and appropriate data for that type.
33
34 Raises:
35 TypeError: If data is not a list or if any item in data is malformed.
36 ValueError: If an unknown item type is encountered.
37 """
38 # Edge Case: Data is None
39 if data is None:
40 data = []
41
42 # Edge Case: Data is not a list
43 if not isinstance(data, list):
44 raise TypeError("Graph data malformed")
45
46 self.nodes = []
47 self.edges = []
48 self.attrs = {}
49
50 # Edge Case: Process each item in the data list
51 for item in data:
52 # Edge Case: Item is not a tuple or doesn't have at least one element
53 if not isinstance(item, tuple) or len(item) < 1:
54 raise TypeError("Graph item malformed")
55
56 item_type = item[0]
57
58 # Edge Case: Item type is NODE
59 if item_type == NODE:
60 # Edge Case: NODE tuple doesn't have exactly 3 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 or attrs is not a dict
66 if not isinstance(name, str) or not isinstance(attrs, dict):
67 raise TypeError("Graph item malformed")
68
69 self.nodes.append(Node(name, attrs))
70
71 # Edge Case: Item type is EDGE
72 elif item_type == EDGE:
73 # Edge Case: EDGE tuple doesn't have exactly 4 elements
74 if len(item) != 4:
75 raise TypeError("Graph item malformed")
76
77 src, dst, attrs = item[1], item[2], item[3]
78 # Edge Case: Edge src or dst is not a string or attrs is not a dict
79 if not isinstance(src, str) or not isinstance(dst, str) or not isinstance(attrs, dict):
80 raise TypeError("Graph item malformed")
81
82 self.edges.append(Edge(src, dst, attrs))
83
84 # Edge Case: Item type is ATTR
85 elif item_type == ATTR:
86 # Edge Case: ATTR tuple doesn't have exactly 3 elements
87 if len(item) != 3:
88 raise TypeError("Graph item malformed")
89
90 key, value = item[1], item[2]
91 # Edge Case: ATTR key or value is not a string
92 if not isinstance(key, str) or not isinstance(value, str):
93 raise TypeError("Graph item malformed")
94
95 self.attrs[key] = value
96
97 # Edge Case: Unknown item type
98 else:
99 raise ValueError("Unknown item")
100
101 # Handled Edge Cases: Data is None, Data is not a list, Item is not a tuple or doesn't have at least one element,
102 # Item type is NODE but tuple doesn't have exactly 3 elements, Node name is not a string or attrs is not a dict,
103 # Item type is EDGE but tuple doesn't have exactly 4 elements, Edge src or dst is not a string or attrs is not a dict,
104 # Item type is ATTR but tuple doesn't have exactly 3 elements, ATTR key or value is not a string, 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.