Debian Codegraph @

tests/test_graphstrategy.py

raw symbols SCIP
1 import pytest
2
3
4 @pytest.fixture
5 def graph_strategy(target):
6 from labgrid.strategy import GraphStrategy
7
8 class TestStrategy(GraphStrategy):
9 def state_Root(self):
10 pass
11
12 @GraphStrategy.depends("Root")
13 def state_A1(self):
14 pass
15
16 @GraphStrategy.depends("Root")
17 def state_A2(self):
18 pass
19
20 @GraphStrategy.depends("A1", "A2")
21 def state_B(self):
22 pass
23
24 @GraphStrategy.depends("B")
25 def state_C1(self):
26 pass
27
28 @GraphStrategy.depends("B")
29 def state_C2(self):
30 pass
31
32 @GraphStrategy.depends("C1", "C2")
33 def state_D(self):
34 pass
35
36 return TestStrategy(target, "strategy")
37
38
39 # api tests ###################################################################
40 @pytest.mark.dependency
41 def test_fixture(graph_strategy, target):
42 # test root state
43 assert graph_strategy.root_state == "Root"
44
45 # test path
46 assert graph_strategy.path == []
47
48 # test state discovering
49 state_names = sorted(list(graph_strategy.states.keys()))
50 expected_state_names = ["A1", "A2", "B", "C1", "C2", "D", "Root"]
51
52 assert state_names == expected_state_names
53
54
55 @pytest.mark.dependency(depends=["test_fixture"])
56 def test_strategy_without_states(target):
57 from labgrid.strategy import GraphStrategy, InvalidGraphStrategyError
58
59 class TestStrategy(GraphStrategy):
60 pass
61
62 with pytest.raises(InvalidGraphStrategyError) as e:
63 TestStrategy(target, "strategy")
64
65 assert e.value.msg == "GraphStrategies without states are invalid"
66
67
68 @pytest.mark.dependency(depends=["test_fixture"])
69 def test_strategy_without_root_state(target):
70 from labgrid.strategy import GraphStrategy, InvalidGraphStrategyError
71
72 class TestStrategy(GraphStrategy):
73 @GraphStrategy.depends("B")
74 def state_A(self):
75 pass
76
77 @GraphStrategy.depends("A")
78 def state_B(self):
79 pass
80
81 with pytest.raises(InvalidGraphStrategyError) as e:
82 TestStrategy(target, "strategy")
83
84 assert e.value.msg == "GraphStrategies without root state are invalid"
85
86
87 @pytest.mark.dependency(depends=["test_fixture"])
88 def test_multiple_root_states(target):
89 from labgrid.strategy import GraphStrategy, InvalidGraphStrategyError
90
91 class TestStrategy(GraphStrategy):
92 def state_Root(self):
93 pass
94
95 def state_Root2(self):
96 pass
97
98 with pytest.raises(InvalidGraphStrategyError) as e:
99 TestStrategy(target, "strategy")
100
101 assert e.value.msg.startswith("Only one root state supported.")
102
103
104 @pytest.mark.dependency(depends=["test_fixture"])
105 def test_unknown_dependencies(target):
106 from labgrid.strategy import GraphStrategy, InvalidGraphStrategyError
107
108 class TestStrategy(GraphStrategy):
109 def state_Root(self):
110 pass
111
112 @GraphStrategy.depends("Root")
113 def state_A(self):
114 pass
115
116 @GraphStrategy.depends("Root", "A", "C")
117 def state_B(self):
118 pass
119
120 with pytest.raises(InvalidGraphStrategyError) as e:
121 TestStrategy(target, "strategy")
122
123 assert e.value.msg.startswith("B: State 'C' is unknown")
124
125
126 @pytest.mark.dependency(depends=["test_fixture"])
127 def test_strategy_with_uncallable_states(target):
128 from labgrid.strategy import GraphStrategy, InvalidGraphStrategyError
129
130 class TestStrategy(GraphStrategy):
131 state_foo = 1
132
133 def state_Root(self):
134 pass
135
136 with pytest.raises(InvalidGraphStrategyError) as e:
137 TestStrategy(target, "strategy")
138
139 assert e.value.msg.startswith("GraphStrategy state 'state_foo' is not callable")
140
141
142 @pytest.mark.dependency(
143 name="api-works",
144 depends=[
145 "test_fixture",
146 "test_strategy_without_states",
147 "test_strategy_without_root_state",
148 "test_unknown_dependencies",
149 "test_strategy_with_uncallable_states",
150 ],
151 )
152 def test_api_works():
153 pass
154
155
156 # functional tests ############################################################
157 @pytest.mark.dependency(depends=["api-works"])
158 def test_graphviz_graph(graph_strategy):
159 pytest.importorskip("graphviz")
160 graph_strategy.graph
161 graph_strategy.graph # trigger the caching branch in the graph code
162
163 graph_strategy.path = ["Root", "A1", "B"] # fake a post transition graph
164 graph_strategy.graph
165
166
167 @pytest.mark.dependency(depends=["api-works"])
168 def test_transition(graph_strategy):
169 assert graph_strategy.transition("B") == ["Root", "A1", "B"]
170 assert graph_strategy.path == ["Root", "A1", "B"]
171
172 assert graph_strategy.transition("B") == []
173 assert graph_strategy.path == ["Root", "A1", "B"]
174
175
176 @pytest.mark.dependency(depends=["api-works", "test_transition"])
177 def test_transition_to_unknown_state(graph_strategy):
178 from labgrid.strategy import GraphStrategyRuntimeError
179
180 with pytest.raises(GraphStrategyRuntimeError) as e:
181 graph_strategy.transition("G")
182
183 assert e.value.msg.startswith("Unknown state 'G'.")
184
185
186 @pytest.mark.dependency(depends=["api-works", "test_transition"])
187 def test_interleaved_transitions(target):
188 from labgrid.strategy import GraphStrategy, GraphStrategyRuntimeError
189
190 class TestStrategy(GraphStrategy):
191 def state_Root(self):
192 pass
193
194 @GraphStrategy.depends("Root")
195 def state_A(self):
196 self.transition("B")
197
198 @GraphStrategy.depends("Root")
199 def state_B(self):
200 self.transition("A")
201
202 strategy = TestStrategy(target, "strategy")
203
204 with pytest.raises(GraphStrategyRuntimeError) as e:
205 strategy.transition("A")
206
207 assert e.value.msg == "Another transition is already running"
208
209
210 @pytest.mark.dependency(depends=["api-works", "test_transition"])
211 def test_transition_via(graph_strategy):
212 assert graph_strategy.transition("D", via=["A2"]) == ["Root", "A2", "B", "C1", "D"]
213 assert graph_strategy.path == ["Root", "A2", "B", "C1", "D"]
214
215 graph_strategy.invalidate()
216
217 assert graph_strategy.transition("D:A2") == ["Root", "A2", "B", "C1", "D"]
218 assert graph_strategy.path == ["Root", "A2", "B", "C1", "D"]
219
220 assert graph_strategy.transition("D:C2,A2") == ["Root", "A2", "B", "C2", "D"]
221 assert graph_strategy.path == ["Root", "A2", "B", "C2", "D"]
222
223
224 @pytest.mark.dependency(depends=["api-works", "test_transition", "test_transition_via"])
225 def test_incremental_transition(graph_strategy):
226 assert graph_strategy.transition("B") == ["Root", "A1", "B"]
227 assert graph_strategy.transition("D") == ["C1", "D"]
228
229 graph_strategy.invalidate()
230
231 assert graph_strategy.transition("B", via=["A2"]) == ["Root", "A2", "B"]
232 assert graph_strategy.transition("D") == ["Root", "A1", "B", "C1", "D"]
233
234
235 @pytest.mark.dependency(depends=["api-works", "test_transition"])
236 def test_transition_error(target):
237 from labgrid.strategy import GraphStrategy
238
239 class TestStrategy(GraphStrategy):
240 def state_Root(self):
241 pass
242
243 @GraphStrategy.depends("Root")
244 def state_A(self):
245 pass
246
247 @GraphStrategy.depends("A")
248 def state_B(self):
249 raise Exception
250
251 strategy = TestStrategy(target, "strategy")
252
253 strategy.transition("A")
254 assert strategy.path == ["Root", "A"]
255
256 with pytest.raises(Exception):
257 strategy.transition("B")
258
259 with pytest.raises(Exception):
260 strategy.transition("B", via="B")
261
262 assert strategy.path == []