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