1010import pytest
1111
1212import graphtik .network as network
13- from graphtik import compose , operation , optional , sideffect
13+ from graphtik import (
14+ abort_run ,
15+ AbortedException ,
16+ compose ,
17+ operation ,
18+ optional ,
19+ sideffect ,
20+ )
1421from graphtik .base import Operation
15- from graphtik .network import _EvictInstruction
22+
23+
24+ @pytest .fixture (params = ["sequential" , "parallel" ])
25+ def exemethod (request ):
26+ return request .param
1627
1728
1829def scream (* args , ** kwargs ):
@@ -21,8 +32,8 @@ def scream(*args, **kwargs):
2132 )
2233
2334
24- def identity (x ):
25- return x
35+ def identity (* x ):
36+ return x [ 0 ] if len ( x ) == 1 else x
2637
2738
2839def filtdict (d , * keys ):
@@ -856,7 +867,7 @@ def addplusplus(a, b, c=0):
856867def test_evict_instructions_vary_with_inputs ():
857868 # Check #21: _EvictInstructions positions vary when inputs change.
858869 def count_evictions (steps ):
859- return sum (isinstance (n , _EvictInstruction ) for n in steps )
870+ return sum (isinstance (n , network . _EvictInstruction ) for n in steps )
860871
861872 pipeline = compose (name = "pipeline" )(
862873 operation (name = "a free without b" , needs = ["a" ], provides = ["aa" ])(identity ),
@@ -1048,3 +1059,23 @@ def test_compose_another_network(bools):
10481059
10491060 sol = bigger_graph ({"a" : 2 , "b" : 5 , "c" : 5 }, outputs = ["a_minus_ab_minus_c" ])
10501061 assert sol == {"a_minus_ab_minus_c" : - 13 }
1062+
1063+
1064+ def test_abort (exemethod ):
1065+ pipeline = compose (name = "pipeline" )(
1066+ operation (name = "A" , needs = ["a" ], provides = ["b" ])(identity ),
1067+ operation (name = "B" , needs = ["b" ], provides = ["c" ])(lambda x : abort_run ()),
1068+ operation (name = "C" , needs = ["c" ], provides = ["d" ])(identity ),
1069+ )
1070+ pipeline .set_execution_method (exemethod )
1071+ with pytest .raises (AbortedException ) as exinfo :
1072+ pipeline ({"a" : 1 })
1073+ assert exinfo .value .jetsam ["solution" ] == {"a" : 1 , "b" : 1 , "c" : None }
1074+ executed = {op .name : val for op , val in exinfo .value .args [0 ].items ()}
1075+ assert executed == {"A" : True , "B" : True , "C" : False }
1076+
1077+ pipeline = compose (name = "pipeline" )(
1078+ operation (name = "A" , needs = ["a" ], provides = ["b" ])(identity )
1079+ )
1080+ pipeline .set_execution_method (exemethod )
1081+ assert pipeline ({"a" : 1 }) == {"a" : 1 , "b" : 1 }
0 commit comments