2023-03-14 12:20:41 +13:00
|
|
|
defmodule Ash.Test.Resource.Preparations.LifecycleHooksTest do
|
|
|
|
@moduledoc false
|
|
|
|
use ExUnit.Case, async: true
|
|
|
|
|
2024-03-28 09:06:40 +13:00
|
|
|
alias Ash.Test.Domain, as: Domain
|
|
|
|
|
2023-03-14 12:20:41 +13:00
|
|
|
defmodule TimeMachine do
|
2024-03-28 09:06:40 +13:00
|
|
|
use Ash.Resource, domain: Domain, data_layer: Ash.DataLayer.Ets
|
2023-03-14 12:20:41 +13:00
|
|
|
|
|
|
|
attributes do
|
|
|
|
uuid_primary_key :id
|
2024-03-28 09:06:40 +13:00
|
|
|
|
|
|
|
attribute :name, :string do
|
|
|
|
public?(true)
|
|
|
|
end
|
2023-03-14 12:20:41 +13:00
|
|
|
end
|
|
|
|
|
|
|
|
actions do
|
2024-03-28 09:06:40 +13:00
|
|
|
default_accept :*
|
|
|
|
|
2023-03-14 12:20:41 +13:00
|
|
|
read :read_with_before_action do
|
|
|
|
argument :caller, :term
|
|
|
|
|
2024-04-10 05:20:35 +12:00
|
|
|
prepare before_action(fn query, _context ->
|
2023-03-14 12:20:41 +13:00
|
|
|
send(query.arguments.caller, query.phase)
|
|
|
|
query
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
|
|
|
read :read_with_after_action do
|
|
|
|
argument :caller, :term
|
|
|
|
|
2024-04-10 05:20:35 +12:00
|
|
|
prepare after_action(fn query, records, _context ->
|
2023-03-14 12:20:41 +13:00
|
|
|
send(query.arguments.caller, query.phase)
|
|
|
|
{:ok, records}
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
|
|
|
read :read_with_multiple_before_actions do
|
|
|
|
argument :caller, :term
|
|
|
|
|
2024-04-10 05:20:35 +12:00
|
|
|
prepare before_action(fn query, _context ->
|
2023-03-14 12:20:41 +13:00
|
|
|
send(query.arguments.caller, {query.phase, 1})
|
|
|
|
query
|
|
|
|
end)
|
|
|
|
|
2024-04-10 05:20:35 +12:00
|
|
|
prepare before_action(fn query, _context ->
|
2023-03-14 12:20:41 +13:00
|
|
|
send(query.arguments.caller, {query.phase, 2})
|
|
|
|
query
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
|
|
|
read :read_with_multiple_after_actions do
|
|
|
|
argument :caller, :term
|
|
|
|
|
2024-04-10 05:20:35 +12:00
|
|
|
prepare after_action(fn query, records, _context ->
|
2023-03-14 12:20:41 +13:00
|
|
|
send(query.arguments.caller, {query.phase, 1})
|
|
|
|
{:ok, records}
|
|
|
|
end)
|
|
|
|
|
2024-04-10 05:20:35 +12:00
|
|
|
prepare after_action(fn query, records, _context ->
|
2023-03-14 12:20:41 +13:00
|
|
|
send(query.arguments.caller, {query.phase, 2})
|
|
|
|
{:ok, records}
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "before_action/1" do
|
|
|
|
test "it is called before the action is run" do
|
|
|
|
TimeMachine
|
|
|
|
|> Ash.Query.for_read(:read_with_before_action, caller: self())
|
2024-03-28 09:06:40 +13:00
|
|
|
|> Ash.read!()
|
2023-03-14 12:20:41 +13:00
|
|
|
|
|
|
|
assert_received :before_action
|
|
|
|
end
|
|
|
|
|
|
|
|
test "multiple before actions have the same phase" do
|
|
|
|
TimeMachine
|
|
|
|
|> Ash.Query.for_read(:read_with_multiple_before_actions, caller: self())
|
2024-03-28 09:06:40 +13:00
|
|
|
|> Ash.read!()
|
2023-03-14 12:20:41 +13:00
|
|
|
|
|
|
|
assert_received {:before_action, 1}
|
|
|
|
assert_received {:before_action, 2}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "after_action/1" do
|
|
|
|
test "it is called after the action is run" do
|
|
|
|
TimeMachine
|
|
|
|
|> Ash.Query.for_read(:read_with_after_action, caller: self())
|
2024-03-28 09:06:40 +13:00
|
|
|
|> Ash.read!()
|
2023-03-14 12:20:41 +13:00
|
|
|
|
|
|
|
assert_received :after_action
|
|
|
|
end
|
|
|
|
|
|
|
|
test "multiple after actions have the same phase" do
|
|
|
|
TimeMachine
|
|
|
|
|> Ash.Query.for_read(:read_with_multiple_after_actions, caller: self())
|
2024-03-28 09:06:40 +13:00
|
|
|
|> Ash.read!()
|
2023-03-14 12:20:41 +13:00
|
|
|
|
|
|
|
assert_received {:after_action, 1}
|
|
|
|
assert_received {:after_action, 2}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|