rockstar

Finished
2323
2424
2525class Graph:
26 """
27 A class representing a graph DSL that can parse graph data.
28
29 The graph data is a list of tuples that describe attributes, nodes, and edges.
30 Each tuple must have the correct structure based on its type.
31 """
32
2633 def __init__(self, data: list | None = None):
27 pass
34 """
35 Initialize a Graph with optional data.
36
37 Args:
38 data: A list of tuples representing graph elements. Each tuple must be
39 of the form (type, ...) where type is one of NODE, EDGE, or ATTR.
40
41 Raises:
42 TypeError: If data is not a list or if any tuple is malformed.
43 ValueError: If an unknown item type is encountered.
44 """
45 # Edge Case: Data is None
46 if data is None:
47 data = []
48
49 # Edge Case: Data is not a list
50 if not isinstance(data, list):
51 raise TypeError("Graph data malformed")
52
53 self.nodes = []
54 self.edges = []
55 self.attrs = {}
56
57 # Edge Case: Process each item in the data list
58 for item in data:
59 # Edge Case: Item is not a tuple
60 if not isinstance(item, tuple):
61 raise TypeError("Graph item malformed")
62
63 # Edge Case: Item is empty tuple
64 if len(item) == 0:
65 raise TypeError("Graph item malformed")
66
67 item_type = item[0]
68
69 # Edge Case: Node item processing
70 if item_type == NODE:
71 # Edge Case: Node tuple doesn't have exactly 3 elements
72 if len(item) != 3:
73 raise TypeError("Graph item malformed")
74
75 name, attrs = item[1], item[2]
76 # Edge Case: Node name is not a string
77 if not isinstance(name, str):
78 raise TypeError("Graph item malformed")
79
80 # Edge Case: Node attributes is not a dict
81 if not isinstance(attrs, dict):
82 raise TypeError("Graph item malformed")
83
84 self.nodes.append(Node(name, attrs))
85
86 # Edge Case: Edge item processing
87 elif item_type == EDGE:
88 # Edge Case: Edge tuple doesn't have exactly 4 elements
89 if len(item) != 4:
90 raise TypeError("Graph item malformed")
91
92 src, dst, attrs = item[1], item[2], item[3]
93 # Edge Case: Edge source is not a string
94 if not isinstance(src, str):
95 raise TypeError("Graph item malformed")
96
97 # Edge Case: Edge destination is not a string
98 if not isinstance(dst, str):
99 raise TypeError("Graph item malformed")
100
101 # Edge Case: Edge attributes is not a dict
102 if not isinstance(attrs, dict):
103 raise TypeError("Graph item malformed")
104
105 self.edges.append(Edge(src, dst, attrs))
106
107 # Edge Case: Attribute item processing
108 elif item_type == ATTR:
109 # Edge Case: Attribute tuple doesn't have exactly 3 elements
110 if len(item) != 3:
111 raise TypeError("Graph item malformed")
112
113 key, value = item[1], item[2]
114 # Edge Case: Attribute key is not a string
115 if not isinstance(key, str):
116 raise TypeError("Graph item malformed")
117
118 self.attrs[key] = value
119
120 # Edge Case: Unknown item type
121 else:
122 raise ValueError("Unknown item")
123 # Handled Edge Cases: Data is None; Data is not a list; Item is not a tuple; Item is empty tuple; Node tuple doesn't have exactly 3 elements; Node name is not a string; Node attributes is not a dict; Edge tuple doesn't have exactly 4 elements; Edge source is not a string; Edge destination is not a string; Edge attributes is not a dict; Attribute tuple doesn't have exactly 3 elements; Attribute key 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.