2022-03-16 10:41:25 +13:00
|
|
|
defmodule Ash.FlowTest.SimpleFlowTest do
|
|
|
|
@moduledoc false
|
2022-03-20 16:51:16 +13:00
|
|
|
use ExUnit.Case, async: false
|
2022-03-16 10:41:25 +13:00
|
|
|
|
2022-10-05 08:30:50 +13:00
|
|
|
alias Ash.Flow.Result
|
2022-10-07 10:04:02 +13:00
|
|
|
alias Ash.Test.Flow.{Api, Org, User}
|
|
|
|
|
|
|
|
alias Ash.Test.Flow.Flows.{
|
|
|
|
GetOrgAndUsers,
|
|
|
|
GetOrgByName,
|
|
|
|
SignUpAndDeleteUser,
|
|
|
|
SignUpUser
|
|
|
|
}
|
2022-03-16 10:41:25 +13:00
|
|
|
|
2022-03-20 16:51:16 +13:00
|
|
|
setup do
|
|
|
|
ExUnit.CaptureLog.capture_log(fn ->
|
2022-10-07 10:04:02 +13:00
|
|
|
Ash.DataLayer.Mnesia.start(Ash.Test.Flow.Api)
|
2022-03-20 16:51:16 +13:00
|
|
|
end)
|
2022-03-16 10:41:25 +13:00
|
|
|
|
2022-03-20 16:51:16 +13:00
|
|
|
on_exit(fn ->
|
|
|
|
ExUnit.CaptureLog.capture_log(fn ->
|
|
|
|
:mnesia.stop()
|
|
|
|
:mnesia.delete_schema([node()])
|
|
|
|
end)
|
|
|
|
end)
|
2022-03-16 10:41:25 +13:00
|
|
|
end
|
|
|
|
|
|
|
|
test "a simple flow can be run" do
|
|
|
|
org =
|
|
|
|
Org
|
|
|
|
|> Ash.Changeset.for_create(:create, %{name: "Org 1"})
|
|
|
|
|> Api.create!()
|
|
|
|
|
|
|
|
org_id = org.id
|
|
|
|
|
2022-10-05 08:30:50 +13:00
|
|
|
assert %Result{result: %{id: ^org_id}} = GetOrgByName.run!("Org 1")
|
2022-03-16 10:41:25 +13:00
|
|
|
end
|
|
|
|
|
|
|
|
test "a flow with multiple steps and dependencies can be run" do
|
|
|
|
org =
|
|
|
|
Org
|
|
|
|
|> Ash.Changeset.for_create(:create, %{name: "Org 1"})
|
|
|
|
|> Api.create!()
|
|
|
|
|
|
|
|
User
|
|
|
|
|> Ash.Changeset.for_create(:create, %{first_name: "abc", org: org.id})
|
2022-09-20 07:44:06 +12:00
|
|
|
|> Ash.Changeset.manage_relationship(:org, org.id, type: :append_and_remove)
|
2022-03-16 10:41:25 +13:00
|
|
|
|> Api.create!()
|
|
|
|
|
|
|
|
User
|
|
|
|
|> Ash.Changeset.for_create(:create, %{first_name: "def", org: org.id})
|
|
|
|
|> Api.create!()
|
|
|
|
|
|
|
|
org_id = org.id
|
|
|
|
|
2022-10-05 08:30:50 +13:00
|
|
|
assert %Result{result: %{org: %{id: ^org_id}, users: users}} = GetOrgAndUsers.run!("Org 1")
|
2022-03-16 10:41:25 +13:00
|
|
|
|
|
|
|
assert users |> Enum.map(& &1.first_name) |> Enum.sort() == ["abc", "def"]
|
|
|
|
end
|
|
|
|
|
|
|
|
test "a flow with a create and an update step works" do
|
|
|
|
org =
|
|
|
|
Org
|
|
|
|
|> Ash.Changeset.for_create(:create, %{name: "Org 1"})
|
|
|
|
|> Api.create!()
|
|
|
|
|
2022-10-05 08:30:50 +13:00
|
|
|
assert %Result{
|
|
|
|
result: %{
|
|
|
|
org: %{name: "Org 1"},
|
|
|
|
user: %{first_name: "Bruce", last_name: "Wayne", approved: true}
|
|
|
|
}
|
2022-10-07 10:04:02 +13:00
|
|
|
} = SignUpUser.run!(org.name, "Bruce", "Wayne")
|
2022-03-16 10:41:25 +13:00
|
|
|
end
|
|
|
|
|
|
|
|
test "a flow with a create and an update and a destroy step works" do
|
|
|
|
org =
|
|
|
|
Org
|
|
|
|
|> Ash.Changeset.for_create(:create, %{name: "Org 1"})
|
|
|
|
|> Api.create!()
|
|
|
|
|
2022-10-05 08:30:50 +13:00
|
|
|
assert %Result{
|
|
|
|
result: %{
|
|
|
|
org: %{name: "Org 1"},
|
|
|
|
user: %{first_name: "Bruce", last_name: "Wayne", approved: true}
|
|
|
|
}
|
2022-10-07 10:04:02 +13:00
|
|
|
} = SignUpAndDeleteUser.run!(org.name, "Bruce", "Wayne")
|
2022-03-16 10:41:25 +13:00
|
|
|
|
|
|
|
assert Api.read!(User) == []
|
|
|
|
end
|
|
|
|
end
|