2023-04-22 16:33:28 +12:00
|
|
|
defmodule AshStateMachineTest do
|
2023-04-22 05:43:36 +12:00
|
|
|
use ExUnit.Case
|
2023-04-22 16:33:28 +12:00
|
|
|
doctest AshStateMachine
|
2023-04-22 05:43:36 +12:00
|
|
|
|
2023-04-22 07:25:39 +12:00
|
|
|
describe "transformers" do
|
2023-08-26 01:37:45 +12:00
|
|
|
test "infers all states, excluding star (:*)" do
|
2023-04-22 16:33:28 +12:00
|
|
|
assert Enum.sort(AshStateMachine.Info.state_machine_all_states(ThreeStates)) ==
|
2023-04-22 07:25:39 +12:00
|
|
|
Enum.sort([:executing, :pending, :complete])
|
|
|
|
end
|
2023-04-22 05:43:36 +12:00
|
|
|
end
|
|
|
|
|
2023-04-22 07:25:39 +12:00
|
|
|
describe "behavior" do
|
|
|
|
test "begins in the appropriate state" do
|
|
|
|
assert ThreeStates.create!().state == :pending
|
|
|
|
end
|
|
|
|
|
|
|
|
test "it transitions to the appropriate state" do
|
2023-04-22 16:33:28 +12:00
|
|
|
state_machine = ThreeStates.create!()
|
2023-04-22 07:25:39 +12:00
|
|
|
|
2023-04-22 16:33:28 +12:00
|
|
|
assert ThreeStates.begin!(state_machine).state == :executing
|
2023-04-22 07:25:39 +12:00
|
|
|
end
|
|
|
|
|
|
|
|
test "it transitions again to the appropriate state" do
|
2023-04-22 16:33:28 +12:00
|
|
|
state_machine = ThreeStates.create!() |> ThreeStates.begin!()
|
2023-04-22 07:25:39 +12:00
|
|
|
|
2023-04-22 16:33:28 +12:00
|
|
|
assert ThreeStates.complete!(state_machine).state == :complete
|
2023-04-22 07:25:39 +12:00
|
|
|
end
|
2023-09-13 00:40:18 +12:00
|
|
|
|
|
|
|
test "`from: :*` can transition from any state" do
|
|
|
|
for state <- [:pending, :confirmed, :on_its_way, :arrived, :error] do
|
|
|
|
assert {:ok, machine} = Order.abort(%Order{state: state})
|
|
|
|
assert machine.state == :aborted
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test "`from: :*` cannot transition _to_ any state" do
|
|
|
|
for state <- [:pending, :confirmed, :on_its_way, :arrived, :error] do
|
|
|
|
assert {:error, reason} = Order.reroute(%Order{state: state})
|
2024-05-02 01:15:40 +12:00
|
|
|
|
2024-05-04 06:11:16 +12:00
|
|
|
# if state != :aborted do
|
|
|
|
# assert Ash.can?({%Order{state: state}, :reroute}, nil) == false
|
|
|
|
# end
|
2024-05-02 01:15:40 +12:00
|
|
|
|
2023-09-13 00:40:18 +12:00
|
|
|
assert Exception.message(reason) =~ ~r/no matching transition/i
|
|
|
|
end
|
|
|
|
end
|
2023-04-22 05:43:36 +12:00
|
|
|
end
|
2023-04-23 10:50:23 +12:00
|
|
|
|
|
|
|
describe "charts" do
|
|
|
|
test "it generates the appropriate chart" do
|
|
|
|
assert AshStateMachine.Charts.mermaid_flowchart(ThreeStates) ==
|
|
|
|
"""
|
|
|
|
flowchart TD
|
|
|
|
pending --> |begin| executing
|
|
|
|
executing --> |complete| complete
|
2023-09-13 00:40:18 +12:00
|
|
|
complete --> pending
|
|
|
|
executing --> pending
|
|
|
|
pending --> pending
|
2023-04-23 10:50:23 +12:00
|
|
|
"""
|
|
|
|
|> String.trim_trailing()
|
|
|
|
end
|
|
|
|
end
|
2023-09-08 13:35:51 +12:00
|
|
|
|
|
|
|
describe "next state" do
|
|
|
|
test "when there is only one next state, it transitions into it" do
|
|
|
|
assert {:ok, nsm} = NextStateMachine.create(%{state: :a})
|
|
|
|
assert {:ok, nsm} = NextStateMachine.next(nsm)
|
|
|
|
assert nsm.state == :b
|
|
|
|
end
|
|
|
|
|
|
|
|
test "when there is more than one next state, it makes an oopsie" do
|
|
|
|
assert {:ok, nsm} = NextStateMachine.create(%{state: :b})
|
|
|
|
assert {:error, reason} = NextStateMachine.next(nsm)
|
|
|
|
assert Exception.message(reason) =~ ~r/multiple next states/i
|
|
|
|
end
|
|
|
|
|
|
|
|
test "when there are no next states available, it also makes an oopsie" do
|
|
|
|
assert {:ok, nsm} = NextStateMachine.create(%{state: :c})
|
|
|
|
assert {:error, reason} = NextStateMachine.next(nsm)
|
|
|
|
assert Exception.message(reason) =~ ~r/no next state/i
|
|
|
|
end
|
|
|
|
end
|
2023-09-15 15:15:29 +12:00
|
|
|
|
|
|
|
describe "possible_next_states/1" do
|
|
|
|
test "it correctly returns the next states" do
|
2024-03-30 11:01:07 +13:00
|
|
|
record = ThreeStates.create!()
|
2023-09-15 15:15:29 +12:00
|
|
|
assert [:executing, :pending] = AshStateMachine.possible_next_states(record)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "possible_next_states/2" do
|
|
|
|
test "it correctly returns the next states" do
|
2024-03-30 11:01:07 +13:00
|
|
|
record = ThreeStates.create!()
|
2023-09-15 15:15:29 +12:00
|
|
|
assert [:pending] = AshStateMachine.possible_next_states(record, :complete)
|
|
|
|
end
|
|
|
|
end
|
2023-04-22 05:43:36 +12:00
|
|
|
end
|