2019-12-02 17:04:16 +13:00
|
|
|
defmodule Ash.Test.Actions.ReadTest do
|
2020-06-02 17:47:25 +12:00
|
|
|
@moduledoc false
|
2019-11-28 18:24:29 +13:00
|
|
|
use ExUnit.Case, async: true
|
|
|
|
|
2021-04-19 11:41:49 +12:00
|
|
|
import Ash.Changeset
|
2022-06-22 13:00:47 +12:00
|
|
|
import Ash.Test
|
2021-04-19 11:41:49 +12:00
|
|
|
|
2020-10-08 18:22:55 +13:00
|
|
|
require Ash.Query
|
|
|
|
|
2022-09-21 11:44:04 +12:00
|
|
|
alias Ash.Test.AnyApi, as: Api
|
|
|
|
|
2021-05-15 03:25:42 +12:00
|
|
|
defmodule PostPreparation do
|
|
|
|
@moduledoc false
|
|
|
|
use Ash.Resource.Preparation
|
|
|
|
|
|
|
|
def prepare(query, _, _) do
|
|
|
|
Ash.Query.after_action(query, fn _query, authors ->
|
2022-08-16 08:02:22 +12:00
|
|
|
{:ok, Enum.map(authors, &Ash.Resource.set_metadata(&1, %{prepared?: true}))}
|
2021-05-15 03:25:42 +12:00
|
|
|
end)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-12-12 11:45:59 +13:00
|
|
|
defmodule Author do
|
2020-06-02 17:47:25 +12:00
|
|
|
@moduledoc false
|
2020-06-14 18:39:11 +12:00
|
|
|
use Ash.Resource, data_layer: Ash.DataLayer.Ets
|
|
|
|
|
|
|
|
ets do
|
|
|
|
private?(true)
|
|
|
|
end
|
2019-12-12 11:45:59 +13:00
|
|
|
|
2022-04-04 17:48:37 +12:00
|
|
|
actions do
|
|
|
|
defaults [:read, :create, :update, :destroy]
|
|
|
|
end
|
|
|
|
|
2019-12-12 11:45:59 +13:00
|
|
|
attributes do
|
2021-01-13 09:40:55 +13:00
|
|
|
uuid_primary_key :id
|
2019-12-12 11:45:59 +13:00
|
|
|
attribute :name, :string
|
|
|
|
end
|
|
|
|
|
|
|
|
relationships do
|
2022-08-16 06:00:02 +12:00
|
|
|
has_many :posts, Ash.Test.Actions.ReadTest.Post, destination_attribute: :author1_id
|
2019-12-12 11:45:59 +13:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-12-02 17:04:16 +13:00
|
|
|
defmodule Post do
|
2020-06-02 17:47:25 +12:00
|
|
|
@moduledoc false
|
2020-06-14 18:39:11 +12:00
|
|
|
use Ash.Resource, data_layer: Ash.DataLayer.Ets
|
|
|
|
|
2021-01-22 09:21:58 +13:00
|
|
|
identities do
|
2022-09-21 11:44:04 +12:00
|
|
|
identity :backup_id, [:uuid], pre_check_with: Api
|
2020-08-25 18:06:01 +12:00
|
|
|
end
|
|
|
|
|
2020-06-14 18:39:11 +12:00
|
|
|
ets do
|
2020-08-25 18:06:01 +12:00
|
|
|
private? true
|
2020-06-14 18:39:11 +12:00
|
|
|
end
|
2019-11-28 18:24:29 +13:00
|
|
|
|
2019-12-02 17:04:16 +13:00
|
|
|
actions do
|
2022-04-04 17:48:37 +12:00
|
|
|
defaults [:read, :create, :update, :destroy]
|
2021-05-15 03:25:42 +12:00
|
|
|
|
|
|
|
read :read_with_after_action do
|
|
|
|
prepare PostPreparation
|
|
|
|
end
|
2023-03-21 18:03:06 +13:00
|
|
|
|
|
|
|
read :get_by_id do
|
|
|
|
get_by :id
|
|
|
|
end
|
|
|
|
|
|
|
|
read :get_by_id_and_uuid do
|
|
|
|
get_by [:id, :uuid]
|
|
|
|
end
|
2019-12-02 17:04:16 +13:00
|
|
|
end
|
|
|
|
|
|
|
|
attributes do
|
2021-01-13 09:40:55 +13:00
|
|
|
uuid_primary_key :id
|
2021-02-24 06:27:49 +13:00
|
|
|
attribute :uuid, :uuid, default: &Ash.UUID.generate/0
|
2019-12-02 17:04:16 +13:00
|
|
|
attribute :title, :string
|
|
|
|
attribute :contents, :string
|
|
|
|
end
|
2019-12-12 11:45:59 +13:00
|
|
|
|
|
|
|
relationships do
|
|
|
|
belongs_to :author1, Ash.Test.Actions.ReadTest.Author
|
|
|
|
belongs_to :author2, Ash.Test.Actions.ReadTest.Author
|
|
|
|
end
|
2019-12-02 17:04:16 +13:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "api.get/3" do
|
2019-11-28 18:24:29 +13:00
|
|
|
setup do
|
2020-07-12 18:25:53 +12:00
|
|
|
post =
|
|
|
|
Post
|
2020-07-15 17:38:42 +12:00
|
|
|
|> new(%{title: "test", contents: "yeet"})
|
2020-07-12 18:25:53 +12:00
|
|
|
|> Api.create!()
|
2022-06-22 13:00:47 +12:00
|
|
|
|> strip_metadata()
|
2020-07-12 18:25:53 +12:00
|
|
|
|
2019-11-28 18:24:29 +13:00
|
|
|
%{post: post}
|
|
|
|
end
|
|
|
|
|
|
|
|
test "it returns a matching record", %{post: post} do
|
2019-12-02 17:04:16 +13:00
|
|
|
assert {:ok, fetched_post} = Api.get(Post, post.id)
|
2019-11-28 18:24:29 +13:00
|
|
|
|
2022-06-22 13:00:47 +12:00
|
|
|
assert strip_metadata(fetched_post) == post
|
2019-11-28 18:24:29 +13:00
|
|
|
end
|
|
|
|
|
2021-09-09 18:18:26 +12:00
|
|
|
test "it returns an error when there is no matching record" do
|
2022-04-04 17:48:37 +12:00
|
|
|
assert {:error, %Ash.Error.Invalid{errors: [%Ash.Error.Query.NotFound{}]}} =
|
|
|
|
Api.get(Post, Ash.UUID.generate())
|
2019-11-28 18:24:29 +13:00
|
|
|
end
|
2020-08-25 18:06:01 +12:00
|
|
|
|
|
|
|
test "it uses identities if they exist", %{post: post} do
|
|
|
|
assert {:ok, fetched_post} = Api.get(Post, uuid: post.uuid)
|
|
|
|
|
2022-06-22 13:00:47 +12:00
|
|
|
assert strip_metadata(fetched_post) == post
|
2020-08-25 18:06:01 +12:00
|
|
|
end
|
2021-04-19 11:45:38 +12:00
|
|
|
|
|
|
|
test "raises an error when the first argument is not a module" do
|
|
|
|
res = assert_raise Ash.Error.Invalid.NoSuchResource, fn -> Api.get("bogus", 1, []) end
|
2022-09-21 11:44:04 +12:00
|
|
|
assert res.message =~ ~r/Ash.Test.AnyApi.get\/3/
|
2021-04-19 11:45:38 +12:00
|
|
|
assert res.message =~ ~r/expected an Ash Resource but instead got "bogus"/
|
|
|
|
end
|
|
|
|
|
|
|
|
test "raises an error when the first argument is a module that is not an ash resource" do
|
|
|
|
res = assert_raise Ash.Error.Invalid.NoSuchResource, fn -> Api.get(BadModuleName, []) end
|
2022-09-21 11:44:04 +12:00
|
|
|
assert res.message =~ ~r/Ash.Test.AnyApi.get\/3/
|
2021-04-19 11:45:38 +12:00
|
|
|
assert res.message =~ ~r/expected an Ash Resource but instead got BadModuleName/
|
|
|
|
end
|
|
|
|
|
|
|
|
test "raises an error when the third argument is not a list" do
|
|
|
|
res = assert_raise RuntimeError, fn -> Api.get(Post, "id", 1) end
|
2022-09-21 11:44:04 +12:00
|
|
|
assert res.message =~ ~r/Ash.Test.AnyApi.get\/3/
|
2021-04-19 11:45:38 +12:00
|
|
|
assert res.message =~ ~r/expected a keyword list, but instead got 1/
|
|
|
|
end
|
|
|
|
|
|
|
|
test "raises an error when the third argument is not a valid keyword list" do
|
|
|
|
res = assert_raise RuntimeError, fn -> Api.get(Post, "id", [1]) end
|
2022-09-21 11:44:04 +12:00
|
|
|
assert res.message =~ ~r/Ash.Test.AnyApi.get\/3/
|
2021-04-19 11:45:38 +12:00
|
|
|
assert res.message =~ ~r/expected a keyword list, but instead got \[1\]/
|
|
|
|
end
|
2019-11-28 18:24:29 +13:00
|
|
|
end
|
|
|
|
|
2019-12-08 10:33:31 +13:00
|
|
|
describe "api.get!/3" do
|
|
|
|
setup do
|
2020-07-12 18:25:53 +12:00
|
|
|
post =
|
|
|
|
Post
|
2020-07-15 17:38:42 +12:00
|
|
|
|> new(%{title: "test", contents: "yeet"})
|
2020-07-12 18:25:53 +12:00
|
|
|
|> Api.create!()
|
2022-06-22 13:00:47 +12:00
|
|
|
|> strip_metadata()
|
2020-07-12 18:25:53 +12:00
|
|
|
|
2019-12-08 10:33:31 +13:00
|
|
|
%{post: post}
|
|
|
|
end
|
|
|
|
|
|
|
|
test "it returns a matching record", %{post: post} do
|
2022-06-22 13:00:47 +12:00
|
|
|
assert ^post = strip_metadata(Api.get!(Post, post.id))
|
2019-12-08 10:33:31 +13:00
|
|
|
end
|
|
|
|
|
2022-11-21 20:51:37 +13:00
|
|
|
test "it gives an invalid primary key error when invalid input is provided" do
|
|
|
|
assert_raise Ash.Error.Invalid, ~r/invalid primary key "not good"/, fn ->
|
|
|
|
Api.get!(Post, "not good")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-05-19 13:37:04 +12:00
|
|
|
test "it raises when there is no matching record" do
|
|
|
|
res =
|
|
|
|
assert_raise Ash.Error.Invalid, fn ->
|
|
|
|
Api.get!(Post, Ash.UUID.generate())
|
|
|
|
end
|
|
|
|
|
|
|
|
assert [%Ash.Error.Query.NotFound{}] = res.errors
|
|
|
|
end
|
|
|
|
|
2021-04-19 11:45:38 +12:00
|
|
|
test "raises an error when the first argument is not a module", %{post: post} do
|
|
|
|
res = assert_raise Ash.Error.Invalid.NoSuchResource, fn -> Api.get("bogus", post.id, []) end
|
2022-09-21 11:44:04 +12:00
|
|
|
assert res.message =~ ~r/Ash.Test.AnyApi.get\/3/
|
2021-04-19 11:45:38 +12:00
|
|
|
assert res.message =~ ~r/expected an Ash Resource but instead got "bogus"/
|
|
|
|
end
|
|
|
|
|
|
|
|
test "raises an error when the first argument is a module that is not an ash resource", %{
|
|
|
|
post: post
|
|
|
|
} do
|
|
|
|
res =
|
|
|
|
assert_raise Ash.Error.Invalid.NoSuchResource, fn ->
|
|
|
|
Api.get(BadModuleName, post.id, [])
|
|
|
|
end
|
|
|
|
|
2022-09-21 11:44:04 +12:00
|
|
|
assert res.message =~ ~r/Ash.Test.AnyApi.get\/3/
|
2021-04-19 11:45:38 +12:00
|
|
|
assert res.message =~ ~r/expected an Ash Resource but instead got BadModuleName/
|
|
|
|
end
|
|
|
|
|
|
|
|
test "raises an error when the third argument is not a list", %{post: post} do
|
|
|
|
res = assert_raise RuntimeError, fn -> Api.get(Post, post.id, 1) end
|
2022-09-21 11:44:04 +12:00
|
|
|
assert res.message =~ ~r/Ash.Test.AnyApi.get\/3/
|
2021-04-19 11:45:38 +12:00
|
|
|
assert res.message =~ ~r/expected a keyword list, but instead got 1/
|
|
|
|
end
|
|
|
|
|
|
|
|
test "raises an error when the third argument is not a valid keyword list", %{post: post} do
|
|
|
|
res = assert_raise RuntimeError, fn -> Api.get(Post, post.id, [1]) end
|
2022-09-21 11:44:04 +12:00
|
|
|
assert res.message =~ ~r/Ash.Test.AnyApi.get\/3/
|
2021-04-19 11:45:38 +12:00
|
|
|
assert res.message =~ ~r/expected a keyword list, but instead got \[1\]/
|
2019-12-08 10:33:31 +13:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-10-20 12:10:05 +13:00
|
|
|
describe "Api.read/2 with no records" do
|
2019-11-28 18:24:29 +13:00
|
|
|
test "returns an empty result" do
|
2020-05-10 14:23:23 +12:00
|
|
|
assert {:ok, []} = Api.read(Post)
|
2019-11-28 18:24:29 +13:00
|
|
|
end
|
2021-04-19 11:45:38 +12:00
|
|
|
|
|
|
|
test "raises an error when the first argument is not a module" do
|
|
|
|
res = assert_raise RuntimeError, fn -> Api.read("bogus", []) end
|
2022-09-21 11:44:04 +12:00
|
|
|
assert res.message =~ ~r/Ash.Test.AnyApi.read\/2/
|
2021-04-19 11:45:38 +12:00
|
|
|
|
|
|
|
assert res.message =~
|
|
|
|
~r/expected an %Ash.Query{} or an Ash Resource but instead got "bogus"/
|
|
|
|
end
|
|
|
|
|
|
|
|
test "raises an error when the first argument is a module that is not an ash resource" do
|
|
|
|
res = assert_raise RuntimeError, fn -> Api.read(BadModuleName, []) end
|
2022-09-21 11:44:04 +12:00
|
|
|
assert res.message =~ ~r/Ash.Test.AnyApi.read\/2/
|
2021-04-19 11:45:38 +12:00
|
|
|
|
|
|
|
assert res.message =~
|
|
|
|
~r/expected an %Ash.Query{} or an Ash Resource but instead got BadModuleName/
|
|
|
|
end
|
|
|
|
|
|
|
|
test "raises an error when the second argument is not a list" do
|
|
|
|
res = assert_raise RuntimeError, fn -> Api.read(Post, 1) end
|
2022-09-21 11:44:04 +12:00
|
|
|
assert res.message =~ ~r/Ash.Test.AnyApi.read\/2/
|
2021-04-19 11:45:38 +12:00
|
|
|
assert res.message =~ ~r/expected a keyword list, but instead got 1/
|
|
|
|
end
|
|
|
|
|
|
|
|
test "raises an error when the second argument is not a valid keyword list" do
|
|
|
|
res = assert_raise RuntimeError, fn -> Api.read(Post, [1]) end
|
2022-09-21 11:44:04 +12:00
|
|
|
assert res.message =~ ~r/Ash.Test.AnyApi.read\/2/
|
2021-04-19 11:45:38 +12:00
|
|
|
assert res.message =~ ~r/expected a keyword list, but instead got \[1\]/
|
|
|
|
end
|
2019-11-28 18:24:29 +13:00
|
|
|
end
|
|
|
|
|
2020-10-20 12:10:05 +13:00
|
|
|
describe "Api.read!/2 with no records" do
|
2019-12-08 10:33:31 +13:00
|
|
|
test "returns an empty result" do
|
2020-05-10 14:23:23 +12:00
|
|
|
assert [] = Api.read!(Post)
|
2019-12-08 10:33:31 +13:00
|
|
|
end
|
2021-04-19 11:45:38 +12:00
|
|
|
|
|
|
|
test "raises an error when the first argument is not a module" do
|
|
|
|
res = assert_raise RuntimeError, fn -> Api.read!("bogus", []) end
|
2022-09-21 11:44:04 +12:00
|
|
|
assert res.message =~ ~r/Ash.Test.AnyApi.read!\/2/
|
2021-04-19 11:45:38 +12:00
|
|
|
|
|
|
|
assert res.message =~
|
|
|
|
~r/expected an %Ash.Query{} or an Ash Resource but instead got "bogus"/
|
|
|
|
end
|
|
|
|
|
|
|
|
test "raises an error when the first argument is a module that is not an ash resource" do
|
|
|
|
res = assert_raise RuntimeError, fn -> Api.read!(BadModuleName, []) end
|
2022-09-21 11:44:04 +12:00
|
|
|
assert res.message =~ ~r/Ash.Test.AnyApi.read!\/2/
|
2021-04-19 11:45:38 +12:00
|
|
|
|
|
|
|
assert res.message =~
|
|
|
|
~r/expected an %Ash.Query{} or an Ash Resource but instead got BadModuleName/
|
|
|
|
end
|
|
|
|
|
|
|
|
test "raises an error when the second argument is not a list" do
|
|
|
|
res = assert_raise RuntimeError, fn -> Api.read!(Post, 1) end
|
2022-09-21 11:44:04 +12:00
|
|
|
assert res.message =~ ~r/Ash.Test.AnyApi.read!\/2/
|
2021-04-19 11:45:38 +12:00
|
|
|
assert res.message =~ ~r/expected a keyword list, but instead got 1/
|
|
|
|
end
|
|
|
|
|
|
|
|
test "raises an error when the second argument is not a valid keyword list" do
|
|
|
|
res = assert_raise RuntimeError, fn -> Api.read!(Post, [1]) end
|
2022-09-21 11:44:04 +12:00
|
|
|
assert res.message =~ ~r/Ash.Test.AnyApi.read!\/2/
|
2021-04-19 11:45:38 +12:00
|
|
|
assert res.message =~ ~r/expected a keyword list, but instead got \[1\]/
|
|
|
|
end
|
2023-02-14 17:20:40 +13:00
|
|
|
|
|
|
|
test "raises an error when page is sent but pagination is not enabled on a resource" do
|
|
|
|
res =
|
|
|
|
assert_raise Ash.Error.Invalid, fn ->
|
2023-02-14 17:44:58 +13:00
|
|
|
Api.read!(Post, page: [limit: 10])
|
2023-02-14 17:20:40 +13:00
|
|
|
end
|
|
|
|
|
|
|
|
assert %Ash.Error.Invalid.PageRequiresPagination{resource: Post, action: _} = hd(res.errors)
|
|
|
|
end
|
2019-12-08 10:33:31 +13:00
|
|
|
end
|
|
|
|
|
2020-10-20 12:10:05 +13:00
|
|
|
describe "Api.read/2" do
|
2019-11-28 18:24:29 +13:00
|
|
|
setup do
|
2020-07-12 18:25:53 +12:00
|
|
|
post1 =
|
|
|
|
Post
|
2020-07-15 17:38:42 +12:00
|
|
|
|> new(%{title: "test", contents: "yeet"})
|
2020-07-12 18:25:53 +12:00
|
|
|
|> Api.create!()
|
|
|
|
|
|
|
|
post2 =
|
|
|
|
Post
|
2020-07-15 17:38:42 +12:00
|
|
|
|> new(%{title: "test1", contents: "yeet2"})
|
2020-07-12 18:25:53 +12:00
|
|
|
|> Api.create!()
|
2019-11-28 18:24:29 +13:00
|
|
|
|
|
|
|
%{post1: post1, post2: post2}
|
|
|
|
end
|
|
|
|
|
2020-05-10 14:23:23 +12:00
|
|
|
test "with a limit of 1, returns only 1 record" do
|
2020-05-14 03:54:44 +12:00
|
|
|
assert {:ok, [_post]} =
|
|
|
|
Post
|
|
|
|
|> Ash.Query.limit(1)
|
|
|
|
|> Api.read()
|
2019-11-28 18:24:29 +13:00
|
|
|
end
|
|
|
|
|
2021-05-15 03:25:42 +12:00
|
|
|
test "after action hooks are run" do
|
|
|
|
assert [%{__metadata__: %{prepared?: true}}, %{__metadata__: %{prepared?: true}}] =
|
|
|
|
Api.read!(Post, action: :read_with_after_action)
|
|
|
|
end
|
|
|
|
|
2020-05-10 14:23:23 +12:00
|
|
|
test "with a limit size of 2, returns 2 records" do
|
2020-05-14 03:54:44 +12:00
|
|
|
assert {:ok, [_, _]} =
|
|
|
|
Post
|
|
|
|
|> Ash.Query.limit(2)
|
|
|
|
|> Api.read()
|
2019-11-28 18:24:29 +13:00
|
|
|
end
|
|
|
|
|
2020-05-10 14:23:23 +12:00
|
|
|
test "with a limit of 1 and an offset of 1, it returns 1 record" do
|
2020-05-14 03:54:44 +12:00
|
|
|
assert {:ok, [_]} =
|
|
|
|
Post
|
|
|
|
|> Ash.Query.limit(1)
|
|
|
|
|> Ash.Query.offset(1)
|
|
|
|
|> Api.read()
|
2019-11-28 18:24:29 +13:00
|
|
|
end
|
|
|
|
end
|
2019-11-29 19:54:11 +13:00
|
|
|
|
2020-10-20 12:10:05 +13:00
|
|
|
describe "Api.read!/2" do
|
2019-12-08 10:33:31 +13:00
|
|
|
setup do
|
2020-07-12 18:25:53 +12:00
|
|
|
post1 =
|
|
|
|
Post
|
2020-07-15 17:38:42 +12:00
|
|
|
|> new(%{title: "test", contents: "yeet"})
|
2020-07-12 18:25:53 +12:00
|
|
|
|> Api.create!()
|
|
|
|
|
|
|
|
post2 =
|
|
|
|
Post
|
2020-07-15 17:38:42 +12:00
|
|
|
|> new(%{title: "test1", contents: "yeet2"})
|
2020-07-12 18:25:53 +12:00
|
|
|
|> Api.create!()
|
2019-12-08 10:33:31 +13:00
|
|
|
|
|
|
|
%{post1: post1, post2: post2}
|
|
|
|
end
|
|
|
|
|
|
|
|
test "it returns the records not in a tuple" do
|
2020-05-10 14:23:23 +12:00
|
|
|
assert [_, _] = Api.read!(Post)
|
2019-12-08 10:33:31 +13:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-04-19 11:45:38 +12:00
|
|
|
describe "Api.read_one/2" do
|
|
|
|
test "raises an error when the first argument is not a module" do
|
|
|
|
res = assert_raise RuntimeError, fn -> Api.read_one("bogus", []) end
|
2022-09-21 11:44:04 +12:00
|
|
|
assert res.message =~ ~r/Ash.Test.AnyApi.read_one\/2/
|
2021-04-19 11:45:38 +12:00
|
|
|
|
|
|
|
assert res.message =~
|
|
|
|
~r/expected an %Ash.Query{} or an Ash Resource but instead got "bogus"/
|
|
|
|
end
|
|
|
|
|
|
|
|
test "raises an error when the first argument is a module that is not an ash resource" do
|
|
|
|
res = assert_raise RuntimeError, fn -> Api.read_one(BadModuleName, []) end
|
2022-09-21 11:44:04 +12:00
|
|
|
assert res.message =~ ~r/Ash.Test.AnyApi.read_one\/2/
|
2021-04-19 11:45:38 +12:00
|
|
|
|
|
|
|
assert res.message =~
|
|
|
|
~r/expected an %Ash.Query{} or an Ash Resource but instead got BadModuleName/
|
|
|
|
end
|
|
|
|
|
|
|
|
test "raises an error when the second argument is not a list" do
|
|
|
|
res = assert_raise RuntimeError, fn -> Api.read_one(Post, 1) end
|
2022-09-21 11:44:04 +12:00
|
|
|
assert res.message =~ ~r/Ash.Test.AnyApi.read_one\/2/
|
2021-04-19 11:45:38 +12:00
|
|
|
assert res.message =~ ~r/expected a keyword list, but instead got 1/
|
|
|
|
end
|
|
|
|
|
|
|
|
test "raises an error when the second argument is not a valid keyword list" do
|
|
|
|
res = assert_raise RuntimeError, fn -> Api.read_one(Post, [1]) end
|
2022-09-21 11:44:04 +12:00
|
|
|
assert res.message =~ ~r/Ash.Test.AnyApi.read_one\/2/
|
2021-04-19 11:45:38 +12:00
|
|
|
assert res.message =~ ~r/expected a keyword list, but instead got \[1\]/
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "Api.read_one!/2" do
|
|
|
|
test "raises an error when the first argument is not a module" do
|
|
|
|
res = assert_raise RuntimeError, fn -> Api.read_one!("bogus", []) end
|
2022-09-21 11:44:04 +12:00
|
|
|
assert res.message =~ ~r/Ash.Test.AnyApi.read_one!\/2/
|
2021-04-19 11:45:38 +12:00
|
|
|
|
|
|
|
assert res.message =~
|
|
|
|
~r/expected an %Ash.Query{} or an Ash Resource but instead got "bogus"/
|
|
|
|
end
|
|
|
|
|
|
|
|
test "raises an error when the first argument is a module that is not an ash resource" do
|
|
|
|
res = assert_raise RuntimeError, fn -> Api.read_one!(BadModuleName, []) end
|
2022-09-21 11:44:04 +12:00
|
|
|
assert res.message =~ ~r/Ash.Test.AnyApi.read_one!\/2/
|
2021-04-19 11:45:38 +12:00
|
|
|
|
|
|
|
assert res.message =~
|
|
|
|
~r/expected an %Ash.Query{} or an Ash Resource but instead got BadModuleName/
|
|
|
|
end
|
|
|
|
|
|
|
|
test "raises an error when the second argument is not a list" do
|
|
|
|
res = assert_raise RuntimeError, fn -> Api.read_one!(Post, 1) end
|
2022-09-21 11:44:04 +12:00
|
|
|
assert res.message =~ ~r/Ash.Test.AnyApi.read_one!\/2/
|
2021-04-19 11:45:38 +12:00
|
|
|
assert res.message =~ ~r/expected a keyword list, but instead got 1/
|
|
|
|
end
|
|
|
|
|
|
|
|
test "raises an error when the second argument is not a valid keyword list" do
|
|
|
|
res = assert_raise RuntimeError, fn -> Api.read_one!(Post, [1]) end
|
2022-09-21 11:44:04 +12:00
|
|
|
assert res.message =~ ~r/Ash.Test.AnyApi.read_one!\/2/
|
2021-04-19 11:45:38 +12:00
|
|
|
assert res.message =~ ~r/expected a keyword list, but instead got \[1\]/
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-11-29 19:54:11 +13:00
|
|
|
describe "filters" do
|
|
|
|
setup do
|
2020-07-12 18:25:53 +12:00
|
|
|
post1 =
|
|
|
|
Post
|
2020-07-15 17:38:42 +12:00
|
|
|
|> new(%{title: "test", contents: "yeet"})
|
2020-07-12 18:25:53 +12:00
|
|
|
|> Api.create!()
|
2022-06-22 13:00:47 +12:00
|
|
|
|> strip_metadata()
|
2020-07-12 18:25:53 +12:00
|
|
|
|
|
|
|
post2 =
|
|
|
|
Post
|
2020-07-15 17:38:42 +12:00
|
|
|
|> new(%{title: "test1", contents: "yeet"})
|
2020-07-12 18:25:53 +12:00
|
|
|
|> Api.create!()
|
2022-06-22 13:00:47 +12:00
|
|
|
|> strip_metadata()
|
2019-11-29 19:54:11 +13:00
|
|
|
|
|
|
|
%{post1: post1, post2: post2}
|
|
|
|
end
|
|
|
|
|
|
|
|
test "a filter that matches nothing returns no results" do
|
2020-05-14 03:54:44 +12:00
|
|
|
assert {:ok, []} =
|
|
|
|
Post
|
2020-10-08 18:22:55 +13:00
|
|
|
|> Ash.Query.filter(contents == "not_yeet")
|
2020-05-14 03:54:44 +12:00
|
|
|
|> Api.read()
|
2019-11-29 19:54:11 +13:00
|
|
|
end
|
|
|
|
|
|
|
|
test "a filter returns only matching records", %{post1: post1} do
|
2020-05-14 03:54:44 +12:00
|
|
|
assert {:ok, [^post1]} =
|
|
|
|
Post
|
2020-10-08 18:22:55 +13:00
|
|
|
|> Ash.Query.filter(title == ^post1.title)
|
2020-05-14 03:54:44 +12:00
|
|
|
|> Api.read()
|
2022-06-22 13:00:47 +12:00
|
|
|
|> strip_metadata()
|
2019-11-29 19:54:11 +13:00
|
|
|
end
|
|
|
|
|
|
|
|
test "a filter returns multiple records if they match", %{post1: post1, post2: post2} do
|
2020-05-14 03:54:44 +12:00
|
|
|
assert {:ok, [_, _] = results} =
|
|
|
|
Post
|
2020-10-08 18:22:55 +13:00
|
|
|
|> Ash.Query.filter(contents == "yeet")
|
2020-05-14 03:54:44 +12:00
|
|
|
|> Api.read()
|
2022-06-22 13:00:47 +12:00
|
|
|
|> strip_metadata()
|
2019-11-29 19:54:11 +13:00
|
|
|
|
|
|
|
assert post1 in results
|
|
|
|
assert post2 in results
|
|
|
|
end
|
|
|
|
end
|
2019-11-30 08:40:19 +13:00
|
|
|
|
2021-03-08 08:09:51 +13:00
|
|
|
describe "select" do
|
|
|
|
test "it automatically selects all fields" do
|
|
|
|
author =
|
|
|
|
Author
|
|
|
|
|> new(%{name: "bruh"})
|
|
|
|
|> Api.create!()
|
|
|
|
|
|
|
|
assert author.name
|
|
|
|
assert author.id
|
|
|
|
end
|
|
|
|
|
|
|
|
test "you can deselect a field" do
|
|
|
|
Author
|
|
|
|
|> new(%{name: "bruh"})
|
|
|
|
|> Api.create!()
|
|
|
|
|
|
|
|
assert [%{name: "bruh"}] = Api.read!(Author)
|
|
|
|
assert [%{name: nil}] = Api.read!(Ash.Query.deselect(Author, :name))
|
|
|
|
end
|
|
|
|
|
|
|
|
test "you can select fields, but the primary key is always present" do
|
|
|
|
Author
|
|
|
|
|> new(%{name: "bruh"})
|
|
|
|
|> Api.create!()
|
|
|
|
|
|
|
|
assert [%{name: "bruh", id: id}] = Api.read!(Ash.Query.select(Author, :name))
|
|
|
|
assert id
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-12-12 11:45:59 +13:00
|
|
|
describe "relationship filters" do
|
|
|
|
setup do
|
2020-07-12 18:25:53 +12:00
|
|
|
author1 =
|
|
|
|
Author
|
2020-07-15 17:38:42 +12:00
|
|
|
|> new(%{name: "bruh"})
|
2020-07-12 18:25:53 +12:00
|
|
|
|> Api.create!()
|
|
|
|
|
|
|
|
author2 =
|
|
|
|
Author
|
2020-07-15 17:38:42 +12:00
|
|
|
|> new(%{name: "bruh"})
|
2020-07-12 18:25:53 +12:00
|
|
|
|> Api.create!()
|
|
|
|
|
|
|
|
post =
|
|
|
|
Post
|
2020-07-15 17:38:42 +12:00
|
|
|
|> new(%{title: "test", contents: "yeet"})
|
2022-09-20 07:44:06 +12:00
|
|
|
|> manage_relationship(:author1, author1, type: :append_and_remove)
|
|
|
|
|> manage_relationship(:author2, author2, type: :append_and_remove)
|
2020-07-12 18:25:53 +12:00
|
|
|
|> Api.create!()
|
2019-12-12 11:45:59 +13:00
|
|
|
|
|
|
|
%{post: post, author1: author1, author2: author2}
|
|
|
|
end
|
|
|
|
|
|
|
|
test "you can filter on a related value", %{author1: author1} do
|
2020-05-14 03:54:44 +12:00
|
|
|
assert [_] =
|
|
|
|
Post
|
|
|
|
|> Ash.Query.filter(author1: author1.id)
|
|
|
|
|> Api.read!()
|
2019-12-12 11:45:59 +13:00
|
|
|
end
|
|
|
|
|
|
|
|
test "you can filter on multiple related values", %{author1: author1, author2: author2} do
|
2020-05-14 03:54:44 +12:00
|
|
|
assert [_] =
|
|
|
|
Post
|
|
|
|
|> Ash.Query.filter(author1: author1.id, author2: author2.id)
|
|
|
|
|> Api.read!()
|
2019-12-12 11:45:59 +13:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-11-30 08:40:19 +13:00
|
|
|
describe "sort" do
|
|
|
|
setup do
|
2020-07-12 18:25:53 +12:00
|
|
|
post1 =
|
|
|
|
Post
|
2020-07-15 17:38:42 +12:00
|
|
|
|> new(%{title: "abc", contents: "abc"})
|
2020-07-12 18:25:53 +12:00
|
|
|
|> Api.create!()
|
2022-06-22 13:00:47 +12:00
|
|
|
|> strip_metadata()
|
2020-07-12 18:25:53 +12:00
|
|
|
|
|
|
|
post2 =
|
|
|
|
Post
|
2020-07-15 17:38:42 +12:00
|
|
|
|> new(%{title: "xyz", contents: "abc"})
|
2020-07-12 18:25:53 +12:00
|
|
|
|> Api.create!()
|
2022-06-22 13:00:47 +12:00
|
|
|
|> strip_metadata()
|
2019-11-30 08:40:19 +13:00
|
|
|
|
|
|
|
%{post1: post1, post2: post2}
|
|
|
|
end
|
|
|
|
|
|
|
|
test "a sort will sort the rows accordingly when ascending", %{
|
|
|
|
post1: post1,
|
|
|
|
post2: post2
|
|
|
|
} do
|
2020-05-14 03:54:44 +12:00
|
|
|
assert {:ok, [^post1, ^post2]} =
|
|
|
|
Post
|
|
|
|
|> Ash.Query.sort(title: :asc)
|
|
|
|
|> Api.read()
|
2022-06-22 13:00:47 +12:00
|
|
|
|> strip_metadata()
|
2019-11-30 08:40:19 +13:00
|
|
|
end
|
|
|
|
|
|
|
|
test "a sort will sor rows accordingly when descending", %{
|
|
|
|
post1: post1,
|
|
|
|
post2: post2
|
|
|
|
} do
|
2020-05-14 03:54:44 +12:00
|
|
|
assert {:ok, [^post2, ^post1]} =
|
|
|
|
Post
|
|
|
|
|> Ash.Query.sort(title: :desc)
|
|
|
|
|> Api.read()
|
2022-06-22 13:00:47 +12:00
|
|
|
|> strip_metadata()
|
2019-11-30 08:40:19 +13:00
|
|
|
end
|
|
|
|
|
|
|
|
test "a nested sort sorts accordingly", %{post1: post1, post2: post2} do
|
2020-07-12 18:25:53 +12:00
|
|
|
middle_post =
|
|
|
|
Post
|
2020-07-15 17:38:42 +12:00
|
|
|
|> new(%{title: "abc", contents: "xyz"})
|
2020-07-12 18:25:53 +12:00
|
|
|
|> Api.create!()
|
2022-06-22 13:00:47 +12:00
|
|
|
|> strip_metadata()
|
2019-11-30 08:40:19 +13:00
|
|
|
|
2020-05-10 14:23:23 +12:00
|
|
|
assert {:ok, [^post1, ^middle_post, ^post2]} =
|
2020-05-14 03:54:44 +12:00
|
|
|
Post
|
|
|
|
|> Ash.Query.sort(title: :asc, contents: :asc)
|
|
|
|
|> Api.read()
|
2022-06-22 13:00:47 +12:00
|
|
|
|> strip_metadata()
|
2019-11-30 08:40:19 +13:00
|
|
|
end
|
|
|
|
end
|
2023-03-21 18:03:06 +13:00
|
|
|
|
|
|
|
describe "get_by with only a single field" do
|
|
|
|
setup do
|
|
|
|
post =
|
|
|
|
Enum.map(0..2, fn _ ->
|
|
|
|
Post
|
|
|
|
|> new(%{title: "test", contents: "yeet"})
|
|
|
|
|> Api.create!()
|
|
|
|
|> strip_metadata()
|
|
|
|
end)
|
|
|
|
|> Enum.random()
|
|
|
|
|
|
|
|
%{post_id: post.id}
|
|
|
|
end
|
|
|
|
|
|
|
|
test "it succeeds when the record exists", %{post_id: post_id} do
|
|
|
|
assert {:ok, %{id: ^post_id}} =
|
|
|
|
Post |> Ash.Query.for_read(:get_by_id, %{id: post_id}) |> Api.read_one()
|
|
|
|
end
|
|
|
|
|
|
|
|
test "it fails when the record does not exist" do
|
|
|
|
assert {:ok, nil} =
|
|
|
|
Post
|
|
|
|
|> Ash.Query.for_read(:get_by_id, %{id: Ash.UUID.generate()})
|
|
|
|
|> Api.read_one()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "get_by with multiple fields" do
|
|
|
|
setup do
|
|
|
|
post =
|
|
|
|
Enum.map(0..2, fn _ ->
|
|
|
|
Post
|
|
|
|
|> new(%{title: "test", contents: "yeet"})
|
|
|
|
|> Api.create!()
|
|
|
|
|> strip_metadata()
|
|
|
|
end)
|
|
|
|
|> Enum.random()
|
|
|
|
|
|
|
|
%{post_id: post.id, post_uuid: post.uuid}
|
|
|
|
end
|
|
|
|
|
|
|
|
test "it succeeds when the record exists", %{post_id: post_id, post_uuid: post_uuid} do
|
|
|
|
assert {:ok, %{id: ^post_id, uuid: ^post_uuid}} =
|
|
|
|
Post
|
|
|
|
|> Ash.Query.for_read(:get_by_id_and_uuid, %{id: post_id, uuid: post_uuid})
|
|
|
|
|> Api.read_one()
|
|
|
|
end
|
|
|
|
|
|
|
|
test "it fails when the record does not exist" do
|
|
|
|
assert {:ok, nil} =
|
|
|
|
Post
|
|
|
|
|> Ash.Query.for_read(:get_by_id_and_uuid, %{
|
|
|
|
id: Ash.UUID.generate(),
|
|
|
|
uuid: Ash.UUID.generate()
|
|
|
|
})
|
|
|
|
|> Api.read_one()
|
|
|
|
end
|
|
|
|
end
|
2019-11-28 18:24:29 +13:00
|
|
|
end
|