33from asyncio import Lock
44from collections .abc import Hashable
55
6- from edgygraph import Graph , Node , State , Shared , START , END , Config , ErrorConfig
7- from edgygraph .graphs import Entry , ErrorEntry
6+ from edgygraph import Graph , Node , State , Shared , START , END , Config
87from edgygraph .diff import ChangeTypes , Diff , Change , ChangeConflictException
98
109
11- class TestNode (Node [State , Shared ]):
12-
13- def __init__ (self , name : str , output : list [str ]):
14- self .name = name
15- self .output = output
16-
17- async def __call__ (self , state : State , shared : Shared ):
18- self .output .append (self .name )
19-
20-
21-
22- @pytest .mark .asyncio
23- async def test_graph_index_edges ():
24-
25- output : list [str ] = []
26-
27- node1 = TestNode ("node1" , output )
28- node2 = TestNode ("node2" , output )
29- node3 = TestNode ("node3" , output )
30- node4 = TestNode ("node4" , output )
31-
32-
33- graph = Graph (edges = [
34- (START , node1 ),
35- (node1 , node2 ),
36- (node2 , node3 , Config (instant = True )),
37- (node3 , END ),
38- (node3 , None ),
39- (Exception , node3 ),
40- (Exception , node4 , ErrorConfig (propagate = True )),
41- ((node3 , Exception ), node4 ),
42- ((node3 , Exception ), node4 , ErrorConfig (propagate = False )),
43- (([node3 , node4 ], Exception ), node4 )
44- ])
45-
46- assert graph .edge_index == {
47- START : [Entry (next = node1 , index = 0 , config = Config ())],
48- node1 : [Entry (next = node2 , index = 1 , config = Config ())],
49- node2 : [Entry (next = node3 , index = 2 , config = Config (instant = True ))],
50- node3 : [Entry (next = END , index = 3 , config = Config ()), Entry (next = None , index = 4 , config = Config ())],
51- }
52- assert graph .error_edge_index == {
53- Exception : [
54- ErrorEntry (next = node3 , index = 5 , config = ErrorConfig ()),
55- ErrorEntry (next = node4 , index = 6 , config = ErrorConfig (propagate = True )),
56- ],
57- (node3 , Exception ): [
58- ErrorEntry (next = node4 , index = 7 , config = ErrorConfig ()),
59- ErrorEntry (next = node4 , index = 8 , config = ErrorConfig (propagate = False )),
60- ErrorEntry (next = node4 , index = 9 , config = ErrorConfig ()),
61- ],
62- (node4 , Exception ): [
63- ErrorEntry (next = node4 , index = 9 , config = ErrorConfig ()),
64- ],
65-
66- }
67-
6810
6911### THE FOLLOWING PART IS CURRENTLY MOSTLY WRITTEN BY AI
7012
@@ -232,7 +174,7 @@ def noop(self):
232174 def test_single_node_increments_value (self ):
233175 state = SimpleState (value = 0 )
234176 shared = SimpleShared ()
235- g = Graph [SimpleState , SimpleShared ](edges = [(START , inc ), (inc , END )])
177+ g = Graph [SimpleState , SimpleShared ](edges = [(( START , inc ), (inc , END ) , END )])
236178 result_state , _ = asyncio .get_event_loop ().run_until_complete (g (state , shared ))
237179 assert result_state .value == 1
238180
@@ -241,14 +183,14 @@ def test_chain_of_two_nodes(self):
241183 n2 = IncrementNode ()
242184 state = SimpleState (value = 0 )
243185 shared = SimpleShared ()
244- g = Graph (edges = [(START , n1 ), (n1 , n2 ), (n2 , END )])
186+ g = Graph (edges = [(( START , n1 ), (n1 , n2 ), (n2 , END ) , END )])
245187 result_state , _ = asyncio .get_event_loop ().run_until_complete (g (state , shared ))
246188 assert result_state .value == 2
247189
248190 def test_empty_graph_returns_unchanged_state (self ):
249191 state = SimpleState (value = 42 )
250192 shared = SimpleShared ()
251- g = Graph [SimpleState , SimpleShared ](edges = [(START , END )])
193+ g = Graph [SimpleState , SimpleShared ](edges = [(( START , END ) , END )])
252194 result_state , _ = asyncio .get_event_loop ().run_until_complete (g (state , shared ))
253195 assert result_state .value == 42
254196
@@ -262,7 +204,7 @@ def test_no_edges_from_start_returns_unchanged(self):
262204 def test_shared_is_same_object (self ):
263205 state = SimpleState ()
264206 shared = SimpleShared ()
265- g = Graph [SimpleState , SimpleShared ](edges = [(START , inc ), (inc , END )])
207+ g = Graph [SimpleState , SimpleShared ](edges = [(( START , inc ), (inc , END ) , END )])
266208 _ , result_shared = asyncio .get_event_loop ().run_until_complete (g (state , shared ))
267209 assert result_shared is shared
268210
@@ -281,7 +223,7 @@ def router(state: SimpleState, shared: SimpleShared):
281223
282224 state = SimpleState (value = 1 )
283225 shared = SimpleShared ()
284- g = Graph (edges = [(START , inc ), (inc , router )])
226+ g = Graph (edges = [(( START , inc ), (inc , router ), END )])
285227 result_state , _ = asyncio .get_event_loop ().run_until_complete (g (state , shared ))
286228 # noop ran (value incremented once by inc, noop does nothing)
287229 assert result_state .value == 2
@@ -294,7 +236,7 @@ def router(state: SimpleState, shared: SimpleShared):
294236
295237 state = SimpleState (value = 0 )
296238 shared = SimpleShared ()
297- g = Graph (edges = [(START , inc ), (inc , router )])
239+ g = Graph (edges = [(( START , inc ), (inc , router ), END )])
298240 result_state , _ = asyncio .get_event_loop ().run_until_complete (g (state , shared ))
299241 assert result_state .value == 1
300242
@@ -307,7 +249,7 @@ async def async_router(state: SimpleState, shared: SimpleShared):
307249
308250 state = SimpleState (value = 0 )
309251 shared = SimpleShared ()
310- g = Graph (edges = [(START , inc ), (inc , async_router ), (noop , END )])
252+ g = Graph (edges = [(( START , inc ), (inc , async_router ), (noop , END ) , END )])
311253 result_state , _ = asyncio .get_event_loop ().run_until_complete (g (state , shared ))
312254 assert result_state .value == 1
313255
@@ -334,10 +276,11 @@ async def __call__(self, state: SimpleState, shared: SimpleShared) -> None:
334276
335277 state = SimpleState ()
336278 shared = SimpleShared ()
337- g = Graph (edges = [
279+ g = Graph (edges = [(
338280 (START , [sv , sn ]),
339281 ([sv , sn ], join ),
340282 (join , END ),
283+ END )
341284 ])
342285 result_state , _ = asyncio .get_event_loop ().run_until_complete (g (state , shared ))
343286 assert result_state .value == 99
@@ -359,8 +302,9 @@ async def __call__(self, state: SimpleState, shared: SimpleShared) -> None:
359302
360303 state = SimpleState ()
361304 shared = SimpleShared ()
362- g = Graph (edges = [
305+ g = Graph (edges = [(
363306 (START , [sv1 , sv2 ]),
307+ END )
364308 ])
365309 with pytest .raises ((ChangeConflictException , ExceptionGroup )):
366310 asyncio .get_event_loop ().run_until_complete (g (state , shared ))
@@ -377,10 +321,11 @@ def test_error_edge_by_exception_type(self):
377321
378322 state = SimpleState ()
379323 shared = SimpleShared ()
380- g = Graph (edges = [
324+ g = Graph (edges = [(
381325 (START , raiser ),
382326 (ValueError , recovery ),
383327 (recovery , END ),
328+ END )
384329 ])
385330 result_state , _ = asyncio .get_event_loop ().run_until_complete (g (state , shared ))
386331 assert result_state .name == "recovered"
@@ -391,10 +336,11 @@ def test_error_edge_by_node_and_exception_type(self):
391336
392337 state = SimpleState ()
393338 shared = SimpleShared ()
394- g = Graph (edges = [
339+ g = Graph (edges = [(
395340 (START , raiser ),
396341 ((raiser , RuntimeError ), recovery ),
397342 (recovery , END ),
343+ END )
398344 ])
399345 result_state , _ = asyncio .get_event_loop ().run_until_complete (g (state , shared ))
400346 assert result_state .name == "recovered"
@@ -404,8 +350,9 @@ def test_unhandled_error_propagates(self):
404350
405351 state = SimpleState ()
406352 shared = SimpleShared ()
407- g = Graph (edges = [
353+ g = Graph (edges = [(
408354 (START , raiser ),
355+ END )
409356 ])
410357 with pytest .raises (ExceptionGroup ):
411358 asyncio .get_event_loop ().run_until_complete (g (state , shared ))
@@ -416,9 +363,10 @@ def test_wrong_exception_type_not_caught(self):
416363
417364 state = SimpleState ()
418365 shared = SimpleShared ()
419- g = Graph (edges = [
366+ g = Graph (edges = [(
420367 (START , raiser ),
421368 (ValueError , RecoveryNode ()),
369+ END )
422370 ])
423371 with pytest .raises (ExceptionGroup ):
424372 asyncio .get_event_loop ().run_until_complete (g (state , shared ))
@@ -436,10 +384,11 @@ def test_instant_node_runs_in_same_step(self):
436384
437385 state = SimpleState (value = 0 )
438386 shared = SimpleShared ()
439- g = Graph (edges = [
387+ g = Graph (edges = [(
440388 (START , inc ),
441389 (inc , noop , Config (instant = True )),
442390 (noop , END ),
391+ END )
443392 ])
444393 result_state , _ = asyncio .get_event_loop ().run_until_complete (g (state , shared ))
445394 assert result_state .value == 1 # inc ran; noop is instant and runs too
@@ -459,11 +408,12 @@ def test_list_source_registers_for_each_node(self):
459408 state = SimpleState (value = 0 )
460409 shared = SimpleShared ()
461410
462- g = Graph (edges = [
411+ g = Graph (edges = [(
463412 (START , [n1 , n3 ]),
464413 (n3 , n2 ),
465414 ([n1 , n2 ], join ),
466415 (join , END ),
416+ END )
467417 ])
468418 result_state , _ = asyncio .get_event_loop ().run_until_complete (g (state , shared ))
469419 assert result_state .value == 2 # both increments applied
0 commit comments