Immune

Finished
2323
2424
2525class Graph:
26 """
27 A class representing a graph structure that can be built from a DSL-like input.
28
29 The Graph is initialized with a list of tuples that define attributes, nodes, and edges.
30 Each tuple must follow specific formats:
31 - ATTR: (ATTR, key, value)
32 - NODE: (NODE, name, {attrs})
33 - EDGE: (EDGE, src, dst, {attrs})
34
35 Raises:
36 TypeError: If the input data is not a list or if any tuple is malformed.
37 ValueError: If an unknown item type is encountered.
38 """
2639 def __init__(self, data: list | None = None):
27 pass
40 # Edge Case: Handle None input by initializing empty collections
41 if data is None:
42 self.attrs = {}
43 self.nodes = []
44 self.edges = []
45 return
46
47 # Edge Case: Graph data must be a list
48 if not isinstance(data, list):
49 raise TypeError("Graph data malformed")
50
51 self.attrs = {}
52 self.nodes = []
53 self.edges = []
54
55 # Process each item in the data list
56 for item in data:
57 # Edge Case: Each item must be a tuple
58 if not isinstance(item, tuple):
59 raise TypeError("Graph item malformed")
60
61 # Edge Case: Each item must have at least one element (the type)
62 if len(item) < 1:
63 raise TypeError("Graph item malformed")
64
65 item_type = item[0]
66
67 # Edge Case: Handle ATTR type items
68 if item_type == ATTR:
69 # Edge Case: ATTR items must have exactly 3 elements
70 if len(item) != 3:
71 raise TypeError("Graph item malformed")
72 key, value = item[1], item[2]
73 self.attrs[key] = value
74
75 # Edge Case: Handle NODE type items
76 elif item_type == NODE:
77 # Edge Case: NODE items must have exactly 3 elements
78 if len(item) != 3:
79 raise TypeError("Graph item malformed")
80 name, attrs = item[1], item[2]
81 # Edge Case: Node attributes must be a dictionary
82 if not isinstance(attrs, dict):
83 raise TypeError("Node attributes must be a dictionary")
84 self.nodes.append(Node(name, attrs))
85
86 # Edge Case: Handle EDGE type items
87 elif item_type == EDGE:
88 # Edge Case: EDGE items must have exactly 4 elements
89 if len(item) != 4:
90 raise TypeError("Graph item malformed")
91 src, dst, attrs = item[1], item[2], item[3]
92 # Edge Case: Edge attributes must be a dictionary
93 if not isinstance(attrs, dict):
94 raise TypeError("Edge attributes must be a dictionary")
95 self.edges.append(Edge(src, dst, attrs))
96
97 # Edge Case: Handle unknown item types
98 else:
99 raise ValueError("Unknown item")
100
101 # Handled Edge Cases: None input, non-list input, non-tuple items, items with insufficient elements,
102 # incorrect tuple lengths for each type, non-dict attributes, unknown item types
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.