2021-04-15 06:18:05 +12:00
|
|
|
defmodule Ash.Test.CodeInterfaceTest do
|
|
|
|
@moduledoc false
|
|
|
|
use ExUnit.Case, async: true
|
|
|
|
|
2024-03-28 09:06:40 +13:00
|
|
|
alias Ash.Test.Domain, as: Domain
|
|
|
|
|
2021-04-15 06:18:05 +12:00
|
|
|
defmodule User do
|
|
|
|
@moduledoc false
|
2024-03-28 09:06:40 +13:00
|
|
|
use Ash.Resource, domain: Domain, data_layer: Ash.DataLayer.Ets
|
2021-04-15 06:18:05 +12:00
|
|
|
|
|
|
|
ets do
|
|
|
|
private?(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
code_interface do
|
2024-03-28 09:06:40 +13:00
|
|
|
define :get_user, action: :read, get?: true
|
|
|
|
define :get_user_safely, action: :read, get?: true, not_found_error?: false
|
2021-04-15 06:18:05 +12:00
|
|
|
define :read_users, action: :read
|
2021-10-20 14:39:30 +13:00
|
|
|
define :get_by_id, action: :read, get_by: [:id]
|
2022-07-12 14:59:51 +12:00
|
|
|
define :create, args: [{:optional, :first_name}]
|
2023-05-02 17:54:53 +12:00
|
|
|
define :hello, args: [:name]
|
2023-01-09 17:39:12 +13:00
|
|
|
|
2023-10-22 12:52:37 +13:00
|
|
|
define :update, action: :update
|
|
|
|
|
2023-01-09 17:39:12 +13:00
|
|
|
define_calculation(:full_name, args: [:first_name, :last_name])
|
|
|
|
|
|
|
|
define_calculation(:full_name_opt,
|
|
|
|
calculation: :full_name,
|
|
|
|
args: [:first_name, :last_name, {:optional, :separator}]
|
|
|
|
)
|
2023-02-25 04:52:10 +13:00
|
|
|
|
|
|
|
define_calculation(:full_name_record, calculation: :full_name, args: [:_record])
|
2021-04-15 06:18:05 +12:00
|
|
|
end
|
|
|
|
|
|
|
|
actions do
|
2024-03-28 09:06:40 +13:00
|
|
|
default_accept :*
|
|
|
|
|
2021-04-15 06:18:05 +12:00
|
|
|
read :read do
|
|
|
|
primary? true
|
|
|
|
end
|
|
|
|
|
2022-04-04 17:48:37 +12:00
|
|
|
create :create
|
|
|
|
|
2023-10-22 12:52:37 +13:00
|
|
|
update :update
|
|
|
|
|
2021-04-15 06:18:05 +12:00
|
|
|
read :by_id do
|
|
|
|
argument :id, :uuid, allow_nil?: false
|
|
|
|
|
|
|
|
filter expr(id == ^arg(:id))
|
|
|
|
end
|
2023-05-02 17:54:53 +12:00
|
|
|
|
|
|
|
action :hello, :string do
|
|
|
|
argument :name, :string, allow_nil?: false
|
|
|
|
|
|
|
|
run(fn input, _ ->
|
|
|
|
{:ok, "Hello #{input.arguments.name}"}
|
|
|
|
end)
|
|
|
|
end
|
2021-04-15 06:18:05 +12:00
|
|
|
end
|
|
|
|
|
2023-01-09 17:39:12 +13:00
|
|
|
calculations do
|
|
|
|
calculate :full_name, :string, expr(first_name <> ^arg(:separator) <> last_name) do
|
2024-03-28 09:06:40 +13:00
|
|
|
public?(true)
|
2023-01-09 17:39:12 +13:00
|
|
|
argument :separator, :string, default: " ", allow_nil?: false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-04-15 06:18:05 +12:00
|
|
|
attributes do
|
|
|
|
uuid_primary_key :id
|
2022-07-12 14:59:51 +12:00
|
|
|
|
|
|
|
attribute :first_name, :string do
|
2024-03-28 09:06:40 +13:00
|
|
|
public?(true)
|
2022-07-12 14:59:51 +12:00
|
|
|
default "fred"
|
|
|
|
end
|
|
|
|
|
2024-03-28 09:06:40 +13:00
|
|
|
attribute :last_name, :string do
|
|
|
|
public?(true)
|
|
|
|
end
|
2021-10-07 19:41:02 +13:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-03-28 09:06:40 +13:00
|
|
|
defmodule Domain do
|
|
|
|
use Ash.Domain
|
2021-04-15 06:18:05 +12:00
|
|
|
|
|
|
|
resources do
|
2024-03-28 09:06:40 +13:00
|
|
|
resource User do
|
|
|
|
define :get_user, action: :read, get?: true
|
|
|
|
|
|
|
|
define_calculation(:full_name, args: [:first_name, :last_name])
|
|
|
|
end
|
2021-04-15 06:18:05 +12:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-05-03 12:00:18 +12:00
|
|
|
describe "generic actions" do
|
2023-06-15 01:43:51 +12:00
|
|
|
test "can be invoked" do
|
2023-05-02 17:54:53 +12:00
|
|
|
assert "Hello fred" == User.hello!("fred")
|
|
|
|
assert {:ok, "Hello george"} == User.hello("george")
|
|
|
|
end
|
2023-06-13 14:59:17 +12:00
|
|
|
|
2023-06-15 01:43:51 +12:00
|
|
|
test "have a helper method to produce inputs" do
|
2023-06-13 14:59:17 +12:00
|
|
|
assert %Ash.ActionInput{action: %{name: :hello}, params: %{name: "bob"}} =
|
|
|
|
User.input_to_hello("bob")
|
|
|
|
end
|
2023-06-14 01:35:27 +12:00
|
|
|
|
2023-06-15 01:43:51 +12:00
|
|
|
test "have a helper to test authorization" do
|
2023-09-19 02:09:27 +12:00
|
|
|
assert {:ok, true} == User.can_hello(nil, "fred")
|
|
|
|
assert User.can_hello?(nil, "fred")
|
2023-06-14 01:35:27 +12:00
|
|
|
end
|
2023-06-13 14:59:17 +12:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "read actions" do
|
2023-06-15 01:43:51 +12:00
|
|
|
test "have a helper methods to produce queries" do
|
2023-06-13 14:59:17 +12:00
|
|
|
assert %Ash.Query{action: %{name: :read}} = User.query_to_read_users()
|
|
|
|
assert %Ash.Query{action: %{name: :read}} = User.query_to_get_by_id("some uuid")
|
|
|
|
end
|
|
|
|
|
2023-06-15 01:43:51 +12:00
|
|
|
test "have a helper to test authorization" do
|
2023-09-19 02:09:27 +12:00
|
|
|
assert {:ok, true} == User.can_read_users(nil)
|
|
|
|
assert {:ok, true} == User.can_get_by_id(nil, "some uuid")
|
|
|
|
assert User.can_read_users?(nil)
|
|
|
|
assert User.can_get_by_id?(nil, "some uuid")
|
2023-06-13 14:59:17 +12:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-06-15 01:43:51 +12:00
|
|
|
describe "read get actions" do
|
|
|
|
test "raise on not found by default" do
|
|
|
|
assert_raise Ash.Error.Query.NotFound, fn ->
|
|
|
|
User.get_user!(Ash.UUID.generate())
|
|
|
|
end
|
2024-03-28 09:06:40 +13:00
|
|
|
|
|
|
|
assert_raise Ash.Error.Query.NotFound, fn ->
|
|
|
|
Domain.get_user!(Ash.UUID.generate())
|
|
|
|
end
|
2023-06-15 01:43:51 +12:00
|
|
|
end
|
|
|
|
|
|
|
|
test "do not raise on not found if were defined with `not_found_error?: false`" do
|
|
|
|
assert nil == User.get_user_safely!(Ash.UUID.generate())
|
|
|
|
end
|
|
|
|
|
|
|
|
test "do not raise on not found if option `not_found_error?: false` is passed" do
|
|
|
|
assert nil == User.get_user!(Ash.UUID.generate(), not_found_error?: false)
|
2024-03-28 09:06:40 +13:00
|
|
|
assert nil == Domain.get_user!(Ash.UUID.generate(), not_found_error?: false)
|
2023-06-15 01:43:51 +12:00
|
|
|
assert nil == User.get_user_safely!(Ash.UUID.generate(), not_found_error?: false)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "raise on not found if option `not_found_error?: true` is passed" do
|
|
|
|
assert_raise Ash.Error.Query.NotFound, fn ->
|
|
|
|
User.get_user_safely!(Ash.UUID.generate(), not_found_error?: true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-06-13 14:59:17 +12:00
|
|
|
describe "create actions" do
|
2023-06-15 01:43:51 +12:00
|
|
|
test "have a helper methods to produce changeset" do
|
2023-06-13 14:59:17 +12:00
|
|
|
assert %Ash.Changeset{action: %{name: :create}, attributes: %{first_name: "fred"}} =
|
|
|
|
User.changeset_to_create()
|
|
|
|
|
|
|
|
assert %Ash.Changeset{action: %{name: :create}, attributes: %{first_name: "bob"}} =
|
|
|
|
User.changeset_to_create("bob")
|
|
|
|
end
|
|
|
|
|
2023-06-15 01:43:51 +12:00
|
|
|
test "have a helper to test authorization" do
|
2023-09-19 02:09:27 +12:00
|
|
|
assert {:ok, true} == User.can_create(nil)
|
|
|
|
assert {:ok, true} == User.can_create(nil, "bob")
|
|
|
|
assert User.can_create?(nil)
|
|
|
|
assert User.can_create?(nil, "bob")
|
2023-06-13 14:59:17 +12:00
|
|
|
end
|
2023-05-02 17:54:53 +12:00
|
|
|
end
|
|
|
|
|
2023-01-09 17:39:12 +13:00
|
|
|
describe "calculations" do
|
|
|
|
test "calculation value can be fetched dynamically" do
|
|
|
|
assert {:ok, "Zach Daniel"} =
|
2024-03-28 09:06:40 +13:00
|
|
|
Ash.calculate(User, :full_name, refs: %{first_name: "Zach", last_name: "Daniel"})
|
2023-01-09 17:39:12 +13:00
|
|
|
end
|
|
|
|
|
|
|
|
test "the same calculation can be fetched with the calculation interface" do
|
|
|
|
assert "Zach Daniel" = User.full_name!("Zach", "Daniel")
|
2024-03-28 09:06:40 +13:00
|
|
|
assert "Zach Daniel" = Domain.full_name!("Zach", "Daniel")
|
2023-01-09 17:39:12 +13:00
|
|
|
end
|
|
|
|
|
|
|
|
test "the same calculation can be fetched with the calculation interface with optional" do
|
|
|
|
assert "Zach Daniel" = User.full_name_opt!("Zach", "Daniel")
|
|
|
|
assert "Zach-Daniel" = User.full_name_opt!("Zach", "Daniel", "-")
|
|
|
|
end
|
2023-02-25 04:52:10 +13:00
|
|
|
|
|
|
|
test "the calculation can accept a record" do
|
|
|
|
user =
|
|
|
|
User
|
|
|
|
|> Ash.Changeset.for_create(:create, %{first_name: "Zach", last_name: "Daniel"})
|
2024-03-28 09:06:40 +13:00
|
|
|
|> Ash.create!()
|
2023-02-25 04:52:10 +13:00
|
|
|
|
|
|
|
assert "Zach Daniel" = User.full_name_record!(user)
|
|
|
|
end
|
2023-01-09 17:39:12 +13:00
|
|
|
end
|
|
|
|
|
2021-10-20 14:39:30 +13:00
|
|
|
test "get_by! adds the proper arguments and filters" do
|
|
|
|
user =
|
|
|
|
User
|
|
|
|
|> Ash.Changeset.for_create(:create, %{first_name: "ted", last_name: "Danson"})
|
2024-03-28 09:06:40 +13:00
|
|
|
|> Ash.create!()
|
2021-10-20 14:39:30 +13:00
|
|
|
|
|
|
|
assert User.get_by_id!(user.id).id == user.id
|
|
|
|
end
|
2022-07-12 14:59:51 +12:00
|
|
|
|
|
|
|
test "optional arguments are optional" do
|
|
|
|
assert User.create!().first_name == "fred"
|
|
|
|
assert User.create!("joe").first_name == "joe"
|
|
|
|
end
|
2021-04-15 06:18:05 +12:00
|
|
|
end
|