def add_two_rollback(ctx: Context) -> Context: n = ctx["n"] result = ctx.get("result", n) ctx.update(result=result - 2) return ctx
def add_two(ctx: Context) -> Context: n = ctx["n"] result = ctx.get("result", n) ctx.update(result=result + 2) return ctx
def test_can_roll_back_function(): ctx = Context.make({"n": 4}) organizer = Organizer([add_two, add_three, fail_context]) result_ctx = organizer.run(ctx) assert ctx.is_failure assert result_ctx["result"] == 7
def test_can_run_functions(): ctx = Context.make({"n": 4}) organizer = Organizer([add_two, add_three]) result_ctx = organizer.run(ctx) assert ctx.is_success assert result_ctx["result"] == 9
def test_can_run_functions_with_failure(): ctx = Context.make({"n": 4}) organizer = Organizer([add_two, fail_context, add_three]) result_ctx = organizer.run(ctx) assert ctx.is_failure assert result_ctx["result"] == 6
def add_two(ctx: Context) -> Context: number = ctx.get("n", 0) ctx["result"] = number + 2 return ctx
def fail_context(ctx: Context) -> Context: ctx.fail("I don't like what I see here") raise Organizer.ContextFailed(fail_context)
def ctx() -> Context: return Context.make()
def fail_context(ctx: Context) -> Context: ctx.fail("I don't like what I see here") return ctx
def skip_rest(ctx: Context) -> Context: ctx.skip("No need to run the rest") return ctx
def fail_context(ctx: Context) -> Context: ctx.fail("Something went wrong...") return ctx
def test_can_fail_with_a_message(ctx: Context) -> None: ctx.fail("An error occurred") assert ctx.is_failure assert ctx.message == "An error occurred"
def test_can_be_pushed_into_a_skipped_state(ctx: Context) -> None: ctx.skip("No need to run the rest") assert ctx.is_skipped assert ctx.message == "No need to run the rest"
def test_can_be_pushed_into_a_failure_state(ctx: Context) -> None: ctx.fail() assert ctx.is_failure
def test_fn__will_not_execute_if_in_failed_state(ctx: Context) -> None: ctx.fail() ctx["n"] = 3 add_two(ctx) assert "result" not in ctx.keys()