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 should
39 have a type (ATTR, NODE, or EDGE) as the first element, followed
40 by type-specific data.
41
42 Raises:
43 TypeError: If data is not a list or if any tuple is malformed.
44 ValueError: If an unknown item type is encountered.
45 """
46 # Edge Case: Handle None data
47 if data is None:
48 data = []
49
50 # Edge Case: Graph data is not a list
51 if not isinstance(data, list):
52 raise TypeError("Graph data malformed")
53
54 self.attrs = {}
55 self.nodes = []
56 self.edges = []
57
58 # Edge Case: Process each item in the data list
59 for item in data:
60 # Edge Case: Graph item is not a tuple
61 if not isinstance(item, tuple):
62 raise TypeError("Graph item malformed")
63
64 # Edge Case: Graph item is empty
65 if len(item) == 0:
66 raise TypeError("Graph item malformed")
67
68 item_type = item[0]
69
70 # Edge Case: Process attribute item
71 if item_type == ATTR:
72 # Edge Case: Attribute tuple has wrong number of elements
73 if len(item) != 3:
74 raise TypeError("Graph item malformed")
75
76 key, value = item[1], item[2]
77 self.attrs[key] = value
78
79 # Edge Case: Process node item
80 elif item_type == NODE:
81 # Edge Case: Node tuple has wrong number of elements
82 if len(item) != 3:
83 raise TypeError("Graph item malformed")
84
85 name, attrs = item[1], item[2]
86 # Edge Case: Node attributes is not a dict
87 if not isinstance(attrs, dict):
88 raise TypeError("Graph item malformed")
89
90 self.nodes.append(Node(name, attrs))
91
92 # Edge Case: Process edge item
93 elif item_type == EDGE:
94 # Edge Case: Edge tuple has wrong number of elements
95 if len(item) != 4:
96 raise TypeError("Graph item malformed")
97
98 src, dst, attrs = item[1], item[2], item[3]
99 # Edge Case: Edge attributes is not a dict
100 if not isinstance(attrs, dict):
101 raise TypeError("Graph item malformed")
102
103 self.edges.append(Edge(src, dst, attrs))
104
105 # Edge Case: Unknown item type
106 else:
107 raise ValueError("Unknown item")
108
109 # Handled Edge Cases: None data, non-list data, non-tuple items, empty tuples,
110 # wrong tuple lengths for each type, non-dict attributes,
111 # 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.