Skip to content

Commit bada5c8

Browse files
authored
Merge pull request #410 from alexhsamuel/fix/skip-dup-jso
Fix/skip dup jso
2 parents 409f51c + c4f4b5b commit bada5c8

File tree

3 files changed

+75
-5
lines changed

3 files changed

+75
-5
lines changed

python/apsis/cond/skip_duplicate.py

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
from apsis.lib import py
44
from apsis.lib.json import check_schema
5-
from apsis.runs import Instance, Run
6-
from apsis.states import to_state
5+
from apsis.runs import Instance
6+
from apsis.states import to_state, to_states
77
from .base import Condition, NonmonotonicRunStoreCondition
88

99
log = logging.getLogger(__name__)
@@ -48,6 +48,16 @@ def __str__(self):
4848
return f"transition to {target} if another run is {states}"
4949

5050

51+
@property
52+
def check_states(self):
53+
return frozenset(self.__check_states)
54+
55+
56+
@property
57+
def target_state(self):
58+
return self.__target_state
59+
60+
5161
def to_jso(self):
5262
return {
5363
**super().to_jso(),
@@ -100,6 +110,26 @@ def __str__(self):
100110
return f"transition to {target} if another {self.__inst} is {states}"
101111

102112

113+
@property
114+
def check_states(self):
115+
return frozenset(self.__check_states)
116+
117+
118+
@property
119+
def target_state(self):
120+
return self.__target_state
121+
122+
123+
@property
124+
def inst(self):
125+
return self.__inst
126+
127+
128+
@property
129+
def run_id(self):
130+
return self.__run_id
131+
132+
103133
def to_jso(self):
104134
return {
105135
**super().to_jso(),
@@ -114,9 +144,10 @@ def to_jso(self):
114144
def from_jso(cls, jso):
115145
with check_schema(jso) as pop:
116146
return cls(
117-
check_states =pop("check_states"),
118-
target_state =pop("target_state"),
147+
check_states =pop("check_states", to_states),
148+
target_state =pop("target_state", to_state),
119149
inst =pop("instance", Instance.from_jso),
150+
run_id =pop("run_id", str),
120151
)
121152

122153

python/apsis/states.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,19 @@ def to_state(state):
3131
raise ValueError(f"not a state: {state!r}")
3232

3333

34+
def to_states(states):
35+
"""
36+
Convers `states` to an iterator of States instances.
37+
38+
>>> tuple(to_states("running"))
39+
(<State.running: 5>,)
40+
>>> tuple(to_states(["failure", State.error]))
41+
(<State.failure: 7>, <State.error: 8>)
42+
43+
"""
44+
return ( to_state(s) for s in py.iterize(states) )
45+
46+
3447
# State model. Allowed transitions _to_ each state.
3548
TRANSITIONS = {
3649
State.new : set(),

test/unit/test_cond.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
from apsis.cond import Condition
2+
from apsis.cond.dependency import Dependency
3+
from apsis.cond.skip_duplicate import SkipDuplicate
14
from apsis.jobs import Job
25
from apsis.runs import Run, Instance
3-
from apsis.cond.dependency import Dependency
6+
from apsis.states import State
47

58
#-------------------------------------------------------------------------------
69

@@ -53,3 +56,26 @@ def test_bind3():
5356
assert bound.states == dep.states
5457

5558

59+
def test_skip_duplicate_jso():
60+
cond = SkipDuplicate()
61+
cond = Condition.from_jso(cond.to_jso())
62+
assert cond.check_states == {State.waiting, State.starting, State.running}
63+
assert cond.target_state == State.skipped
64+
65+
cond = SkipDuplicate(check_states="running", target_state="error")
66+
cond = Condition.from_jso(cond.to_jso())
67+
assert cond.check_states == {State.running}
68+
assert cond.target_state == State.error
69+
70+
run = Run(Instance("my job", {"foo": "orange"}))
71+
run.run_id = "r12345"
72+
cond = cond.bind(run, jobs=None) # jobs not used
73+
74+
cond = Condition.from_jso(cond.to_jso())
75+
assert cond.check_states == {State.running}
76+
assert cond.target_state == State.error
77+
assert cond.inst.job_id == "my job"
78+
assert cond.inst.args == {"foo": "orange"}
79+
assert cond.run_id == "r12345"
80+
81+

0 commit comments

Comments
 (0)