2022-03-28 13:00:19 +13:00
|
|
|
defmodule Ash.Test.Resource.ResourceTest do
|
|
|
|
@moduledoc false
|
|
|
|
use ExUnit.Case, async: true
|
|
|
|
|
|
|
|
defmacrop defposts(do: body) do
|
|
|
|
quote do
|
|
|
|
defmodule Post do
|
|
|
|
@moduledoc false
|
|
|
|
use Ash.Resource, data_layer: Ash.DataLayer.Ets
|
|
|
|
|
2022-04-04 17:48:37 +12:00
|
|
|
actions do
|
|
|
|
defaults [:create, :read, :update, :destroy]
|
|
|
|
end
|
|
|
|
|
2022-03-28 13:00:19 +13:00
|
|
|
attributes do
|
|
|
|
uuid_primary_key :id
|
|
|
|
end
|
|
|
|
|
|
|
|
unquote(body)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defmodule Concat do
|
2022-10-05 09:01:42 +13:00
|
|
|
# An example concatenation calculation, that accepts the delimiter as an argument
|
2022-03-28 13:00:19 +13:00
|
|
|
use Ash.Calculation
|
|
|
|
|
|
|
|
def init(opts) do
|
|
|
|
if opts[:keys] && is_list(opts[:keys]) && Enum.all?(opts[:keys], &is_atom/1) do
|
|
|
|
{:ok, opts}
|
|
|
|
else
|
|
|
|
{:error, "Expected a `keys` option for which keys to concat"}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def calculate(records, opts, %{separator: separator}) do
|
|
|
|
Enum.map(records, fn record ->
|
|
|
|
Enum.map_join(opts[:keys], separator, fn key ->
|
|
|
|
to_string(Map.get(record, key))
|
|
|
|
end)
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-03-30 14:07:44 +13:00
|
|
|
defmodule Post do
|
|
|
|
@moduledoc false
|
|
|
|
use Ash.Resource
|
|
|
|
|
2022-04-04 17:48:37 +12:00
|
|
|
actions do
|
|
|
|
defaults [:create, :read, :update, :destroy]
|
|
|
|
end
|
|
|
|
|
2022-03-30 14:07:44 +13:00
|
|
|
attributes do
|
|
|
|
uuid_primary_key :id
|
|
|
|
attribute :name, :string
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defmodule Registry do
|
|
|
|
@moduledoc false
|
|
|
|
use Ash.Registry
|
|
|
|
|
|
|
|
entries do
|
|
|
|
entry(Post)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defmodule Api do
|
|
|
|
@moduledoc false
|
|
|
|
use Ash.Api
|
|
|
|
|
|
|
|
resources do
|
|
|
|
registry Registry
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test "it returns the correct error when doing a read with no data layer setup" do
|
|
|
|
Post
|
|
|
|
|> Ash.Changeset.new(%{name: "foo"})
|
|
|
|
|> Api.create()
|
|
|
|
|
|
|
|
{_, error} = Api.read(Post)
|
|
|
|
[%Ash.Error.SimpleDataLayer.NoDataProvided{message: message} | _] = error.errors
|
|
|
|
assert message != nil
|
|
|
|
end
|
|
|
|
|
2022-03-28 13:00:19 +13:00
|
|
|
test "fails if there are multiple fields that share the same name" do
|
|
|
|
assert_raise(
|
2022-08-15 06:06:58 +12:00
|
|
|
Spark.Error.DslError,
|
2022-08-17 06:50:57 +12:00
|
|
|
~r/There are 4 fields\(attributes, calculations, aggregates, and relationships\) that share the name `foobar`/,
|
2022-03-28 13:00:19 +13:00
|
|
|
fn ->
|
|
|
|
defposts do
|
|
|
|
attributes do
|
|
|
|
attribute(:foobar, :string)
|
|
|
|
end
|
|
|
|
|
|
|
|
relationships do
|
|
|
|
belongs_to(:foobar, Foobar)
|
|
|
|
end
|
|
|
|
|
|
|
|
calculations do
|
2022-11-04 12:06:26 +13:00
|
|
|
calculate :foobar, :integer, {Concat, keys: [:foo, :bar]} do
|
2022-03-28 13:00:19 +13:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
aggregates do
|
|
|
|
count :foobar, :baz
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|