def test_call_runner_target_handlers_calls_handlers_appropriately(): class TestRunner(Runner): def call_runner_target_handlers(self, old_state, new_state): return new_state my_handler = MagicMock(return_value=state.Running()) TestRunner(state_handlers=[my_handler]).handle_state_change( state.Pending(), state.Running()) assert my_handler.call_args[0][1:] == (state.Pending(), state.Running())
def complex_states(): res1 = SafeResult(1, result_handler=JSONResultHandler()) res2 = SafeResult({"z": 2}, result_handler=JSONResultHandler()) res3 = SafeResult(dict(x=1, y={"z": 2}), result_handler=JSONResultHandler()) naive_dt = datetime.datetime(2020, 1, 1) utc_dt = pendulum.datetime(2020, 1, 1) complex_result = {"x": res1, "y": res2} cached_state = state.Cached( cached_inputs=complex_result, result=res3, cached_parameters={ "x": 1, "y": { "z": 2 } }, cached_result_expiration=utc_dt, ) cached_state_naive = state.Cached( cached_inputs=complex_result, result=res3, cached_parameters={ "x": 1, "y": { "z": 2 } }, cached_result_expiration=naive_dt, ) running_tags = state.Running() running_tags.context = dict(tags=["1", "2", "3"]) test_states = [ state.Looped(loop_count=45), state.Pending(cached_inputs=complex_result), state.Paused(cached_inputs=complex_result), state.Retrying(start_time=utc_dt, run_count=3), state.Retrying(start_time=naive_dt, run_count=3), state.Scheduled(start_time=utc_dt), state.Scheduled(start_time=naive_dt), state.Resume(start_time=utc_dt), state.Resume(start_time=naive_dt), running_tags, state.Submitted(state=state.Retrying(start_time=utc_dt, run_count=2)), state.Submitted(state=state.Resume(start_time=utc_dt)), state.Queued(state=state.Pending()), state.Queued(state=state.Pending(), start_time=utc_dt), state.Queued(state=state.Retrying(start_time=utc_dt, run_count=2)), cached_state, cached_state_naive, state.TimedOut(cached_inputs=complex_result), ] return test_states
def test_call_runner_target_handlers_reraises_appropriately(exc): class TestRunner(Runner): def call_runner_target_handlers(self, old_state, new_state): raise exc() with pytest.raises(exc): TestRunner().handle_state_change(state.Pending(), state.Running())
def test_call_runner_target_handlers_gets_called_in_handle_state_change(): """tests that the `call_runner_target_handlers` helper method is called""" class TestRunner(Runner): def call_runner_target_handlers(self, old_state, new_state): raise ValueError() with pytest.raises(ENDRUN): TestRunner().handle_state_change(state.Pending(), state.Running())
class TestInitializeRun: def test_initialize_run_returns_state_and_context(self): state_, context = state.Pending(), {} s, c = Runner().initialize_run(state=state_, context=context) assert s is state_ assert c is context @pytest.mark.parametrize( "state", [ state.Success(), state.Failed(), state.Pending(), state.Scheduled(), state.Skipped(), state.Cached(), state.Retrying(), state.Running(), ], ) def test_initialize_run_returns_state(self, state): new_state, _ = Runner().initialize_run(state, context={}) assert new_state is state @pytest.mark.parametrize( "state", [ state.Submitted(state=state.Pending()), state.Submitted(state=state.Retrying()), state.Submitted(state=state.Scheduled()), state.Submitted(state=state.Resume()), state.Queued(state=state.Pending()), state.Queued(state=state.Retrying()), state.Queued(state=state.Scheduled()), state.Queued(state=state.Resume()), ], ) def test_initialize_run_gets_wrapped_state_from_submitted_states( self, state): new_state, _ = Runner().initialize_run(state, context={}) assert new_state is state.state def test_initialize_run_creates_pending_if_no_state_provided(self): new_state, _ = Runner().initialize_run(state=None, context={}) assert isinstance(new_state, state.Pending)
def test_call_runner_target_handlers_allows_for_none_return_values(): class TestRunner(Runner): def call_runner_target_handlers(self, old_state, new_state): return new_state my_handler = MagicMock(return_value=None) res = TestRunner(state_handlers=[my_handler]).handle_state_change( state.Pending(), state.Running()) assert res == state.Running()
def complex_states(): res1 = results.PrefectResult(value=1) res2 = results.PrefectResult(value={"z": 2}) res3 = results.PrefectResult(location=json.dumps(dict(x=1, y={"z": 2}))) naive_dt = datetime.datetime(2020, 1, 1) utc_dt = pendulum.datetime(2020, 1, 1) cached_state = state.Cached( hashed_inputs=dict(x="foo", y="bar"), result=res3, cached_result_expiration=utc_dt, ) cached_state_naive = state.Cached( hashed_inputs=dict(x="foo", y="bar"), result=res3, cached_result_expiration=naive_dt, ) running_tags = state.Running() running_tags.context = dict(tags=["1", "2", "3"]) test_states = [ state.Looped(loop_count=45), state.Retrying(start_time=utc_dt, run_count=3), state.Retrying(start_time=naive_dt, run_count=3), state.Scheduled(start_time=utc_dt), state.Scheduled(start_time=naive_dt), state.Resume(start_time=utc_dt), state.Resume(start_time=naive_dt), running_tags, state.Submitted(state=state.Retrying(start_time=utc_dt, run_count=2)), state.Submitted(state=state.Resume(start_time=utc_dt)), state.Queued(state=state.Pending()), state.Queued(state=state.Pending(), start_time=utc_dt), state.Queued(state=state.Retrying(start_time=utc_dt, run_count=2)), cached_state, cached_state_naive, ] return test_states
cached_result_expiration=utc_dt, ) cached_state_naive = state.Cached( cached_inputs=complex_result, result=res3, cached_parameters={"x": 1, "y": {"z": 2}}, >>>>>>> prefect clone cached_result_expiration=naive_dt, ) running_tags = state.Running() running_tags.context = dict(tags=["1", "2", "3"]) test_states = [ state.Looped(loop_count=45), <<<<<<< HEAD ======= state.Pending(cached_inputs=complex_result), state.Paused(cached_inputs=complex_result), >>>>>>> prefect clone state.Retrying(start_time=utc_dt, run_count=3), state.Retrying(start_time=naive_dt, run_count=3), state.Scheduled(start_time=utc_dt), state.Scheduled(start_time=naive_dt), state.Resume(start_time=utc_dt), state.Resume(start_time=naive_dt), running_tags, state.Submitted(state=state.Retrying(start_time=utc_dt, run_count=2)), state.Submitted(state=state.Resume(start_time=utc_dt)), state.Queued(state=state.Pending()), state.Queued(state=state.Pending(), start_time=utc_dt), state.Queued(state=state.Retrying(start_time=utc_dt, run_count=2)), cached_state,
def test_initialize_run_returns_state_and_context(self): state_, context = state.Pending(), {} s, c = Runner().initialize_run(state=state_, context=context) assert s is state_ assert c is context