2021-04-15 06:18:05 +12:00
|
|
|
defmodule Ash.Test.CodeInterfaceTest do
|
|
|
|
@moduledoc false
|
|
|
|
use ExUnit.Case, async: true
|
|
|
|
|
|
|
|
defmodule User do
|
|
|
|
@moduledoc false
|
|
|
|
use Ash.Resource, data_layer: Ash.DataLayer.Ets
|
|
|
|
|
|
|
|
ets do
|
|
|
|
private?(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
code_interface do
|
2021-10-14 11:42:42 +13:00
|
|
|
define_for Ash.Test.CodeInterfaceTest.Api
|
2021-04-15 06:18:05 +12:00
|
|
|
define :get_user, action: :read, get?: true, args: [:id]
|
|
|
|
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-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
|
|
|
|
read :read do
|
|
|
|
primary? true
|
|
|
|
end
|
|
|
|
|
2022-04-04 17:48:37 +12:00
|
|
|
create :create
|
|
|
|
|
2021-04-15 06:18:05 +12:00
|
|
|
read :by_id do
|
|
|
|
argument :id, :uuid, allow_nil?: false
|
|
|
|
|
|
|
|
filter expr(id == ^arg(:id))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-01-09 17:39:12 +13:00
|
|
|
calculations do
|
|
|
|
calculate :full_name, :string, expr(first_name <> ^arg(:separator) <> last_name) do
|
|
|
|
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
|
|
|
|
default "fred"
|
|
|
|
end
|
|
|
|
|
2021-04-15 06:18:05 +12:00
|
|
|
attribute :last_name, :string
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-10-07 19:41:02 +13:00
|
|
|
defmodule Registry do
|
|
|
|
@moduledoc false
|
|
|
|
use Ash.Registry
|
|
|
|
|
|
|
|
entries do
|
|
|
|
entry(User)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-04-15 06:18:05 +12:00
|
|
|
defmodule Api do
|
|
|
|
@moduledoc false
|
|
|
|
use Ash.Api
|
|
|
|
|
|
|
|
resources do
|
2021-10-07 19:41:02 +13:00
|
|
|
registry Registry
|
2021-04-15 06:18:05 +12:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-01-09 17:39:12 +13:00
|
|
|
describe "calculations" do
|
|
|
|
test "calculation value can be fetched dynamically" do
|
|
|
|
assert {:ok, "Zach Daniel"} =
|
|
|
|
Api.calculate(User, :full_name, refs: %{first_name: "Zach", last_name: "Daniel"})
|
|
|
|
end
|
|
|
|
|
|
|
|
test "the same calculation can be fetched with the calculation interface" do
|
|
|
|
assert "Zach Daniel" = User.full_name!("Zach", "Daniel")
|
|
|
|
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"})
|
|
|
|
|> Api.create!()
|
|
|
|
|
|
|
|
assert "Zach Daniel" = User.full_name_record!(user)
|
|
|
|
end
|
2023-01-09 17:39:12 +13:00
|
|
|
end
|
|
|
|
|
2021-04-15 06:18:05 +12:00
|
|
|
test "get! raises on not found" do
|
|
|
|
assert_raise Ash.Error.Query.NotFound, fn ->
|
2021-10-14 11:42:42 +13:00
|
|
|
User.get_user!(Ash.UUID.generate())
|
2021-04-15 06:18:05 +12:00
|
|
|
end
|
|
|
|
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"})
|
|
|
|
|> Api.create!()
|
|
|
|
|
|
|
|
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
|