示例#1
0
def test_flow_run_raises_if_no_more_scheduled_runs():
    schedule = prefect.schedules.OneTimeSchedule(
        start_date=pendulum.now("utc").add(days=-1))
    f = Flow(name="test", schedule=schedule)
    with pytest.raises(ValueError) as exc:
        f.run()
    assert "no more scheduled runs" in str(exc.value)
示例#2
0
    def test_flow_dot_run_runs_on_schedule(self):
        class MockSchedule(prefect.schedules.Schedule):
            call_count = 0

            def next(self, n):
                if self.call_count < 2:
                    self.call_count += 1
                    # add small delta to trigger "naptime"
                    return [pendulum.now("utc").add(seconds=0.05)]
                else:
                    raise SyntaxError("Cease scheduling!")

        class StatefulTask(Task):
            call_count = 0

            def run(self):
                self.call_count += 1

        t = StatefulTask()
        schedule = MockSchedule()
        f = Flow(name="test", tasks=[t], schedule=schedule)
        with pytest.raises(SyntaxError) as exc:
            f.run()
        assert "Cease" in str(exc.value)
        assert t.call_count == 2
示例#3
0
def test_flow_run_respects_task_state_kwarg():
    t, s = Task(), Task()
    f = Flow(name="test", tasks=[t, s])
    flow_state = f.run(task_states={t: Failed("unique.")})
    assert flow_state.is_failed()
    assert flow_state.result[t].is_failed()
    assert flow_state.result[t].message == "unique."
    assert flow_state.result[s].is_successful()
示例#4
0
def test_bad_flow_runner_code_still_returns_state_obj():
    class BadFlowRunner(prefect.engine.flow_runner.FlowRunner):
        def initialize_run(self, *args, **kwargs):
            import blig  # will raise ImportError

    f = Flow(name="test", tasks=[Task()])
    res = f.run(runner_cls=BadFlowRunner)
    assert isinstance(res, State)
    assert res.is_failed()
    assert isinstance(res.result, ImportError)
示例#5
0
def test_calling_a_task_without_context_returns_a_copy():
    t = AddTask()

    f = Flow(name="test")
    t.bind(4, 2, flow=f)
    t2 = t(9, 0, flow=f)

    assert isinstance(t2, AddTask)
    assert t != t2

    res = f.run().result
    assert res[t].result == 6
    assert res[t2].result == 9
示例#6
0
    def test_flow_dot_run_stops_on_schedule(self):
        class MockSchedule(prefect.schedules.Schedule):
            call_count = 0

            def next(self, n):
                if self.call_count < 1:
                    self.call_count += 1
                    return [pendulum.now("utc").add(seconds=0.05)]
                else:
                    return []

        class StatefulTask(Task):
            call_count = 0

            def run(self):
                self.call_count += 1

        t = StatefulTask()
        schedule = MockSchedule()
        f = Flow(name="test", tasks=[t], schedule=schedule)
        f.run()
        assert t.call_count == 1
示例#7
0
def test_scheduled_runs_handle_retries():
    class MockSchedule(prefect.schedules.Schedule):
        call_count = 0

        def next(self, n):
            if self.call_count < 1:
                self.call_count += 1
                return [pendulum.now("utc")]
            else:
                raise SyntaxError("Cease scheduling!")

    class StatefulTask(Task):
        call_count = 0

        def run(self):
            self.call_count += 1
            if self.call_count == 1:
                raise OSError("I need to run again.")

    state_history = []

    def handler(task, old, new):
        state_history.append(new)
        return new

    t = StatefulTask(
        max_retries=1,
        retry_delay=datetime.timedelta(minutes=0),
        state_handlers=[handler],
    )
    schedule = MockSchedule()
    f = Flow(name="test", tasks=[t], schedule=schedule)
    with pytest.raises(SyntaxError) as exc:
        f.run()
    assert "Cease" in str(exc.value)
    assert t.call_count == 2
    assert len(
        state_history) == 5  # Running, Failed, Retrying, Running, Success
示例#8
0
    def test_flow_dot_run_responds_to_config(self):
        class MockSchedule(prefect.schedules.Schedule):
            call_count = 0

            def next(self, n):
                if self.call_count < 2:
                    self.call_count += 1
                    # add small delta to trigger "naptime"
                    return [pendulum.now("utc").add(seconds=0.05)]
                else:
                    raise SyntaxError("Cease scheduling!")

        class StatefulTask(Task):
            call_count = 0

            def run(self):
                self.call_count += 1

        t = StatefulTask()
        schedule = MockSchedule()
        f = Flow(name="test", tasks=[t], schedule=schedule)
        with set_temporary_config({"flows.run_on_schedule": False}):
            state = f.run()
        assert t.call_count == 1
示例#9
0
def test_flow_run_respects_state_kwarg():
    f = Flow(name="test")
    state = f.run(state=Failed("Unique."))
    assert state.is_failed()
    assert state.message == "Unique."
示例#10
0
def test_flow_run_raises_informative_error_for_certain_kwargs():
    f = Flow(name="test")
    with pytest.raises(ValueError) as exc:
        f.run(return_tasks=f.tasks)
    assert "`return_tasks` keyword cannot be provided" in str(exc.value)
示例#11
0
def test_flow_run_accepts_state_kwarg():
    f = Flow(name="test")
    state = f.run(state=Finished())
    assert state.is_finished()