ash/test/actions/read_test.exs

327 lines
7.4 KiB
Elixir
Raw Normal View History

2019-12-02 17:04:16 +13:00
defmodule Ash.Test.Actions.ReadTest do
2020-06-02 17:47:25 +12:00
@moduledoc false
use ExUnit.Case, async: true
2019-12-12 11:45:59 +13:00
defmodule Author do
2020-06-02 17:47:25 +12:00
@moduledoc false
use Ash.Resource, data_layer: Ash.DataLayer.Ets
ets do
private?(true)
end
2019-12-12 11:45:59 +13:00
actions do
read :default
create :default
end
attributes do
attribute :id, :uuid, primary_key?: true, default: &Ecto.UUID.generate/0
2019-12-12 11:45:59 +13:00
attribute :name, :string
end
relationships do
2020-07-12 18:25:53 +12:00
has_many :posts, Ash.Test.Actions.ReadTest.Post, destination_field: :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
use Ash.Resource, data_layer: Ash.DataLayer.Ets
resource do
identities do
identity :backup_id, [:uuid]
end
end
ets do
private? true
end
2019-12-02 17:04:16 +13:00
actions do
2019-12-06 20:06:34 +13:00
read :default
create :default
2019-12-02 17:04:16 +13:00
end
attributes do
attribute :id, :uuid, primary_key?: true, default: &Ecto.UUID.generate/0
attribute :uuid, :uuid, default: &Ecto.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
defmodule Api do
2020-06-02 17:47:25 +12:00
@moduledoc false
2019-12-02 17:04:16 +13:00
use Ash.Api
resources do
resource Post
resource Author
end
2019-12-02 17:04:16 +13:00
end
2020-07-12 18:25:53 +12:00
import Ash.Changeset
2019-12-02 17:04:16 +13:00
describe "api.get/3" do
setup do
2020-07-12 18:25:53 +12:00
post =
Post
|> new(%{title: "test", contents: "yeet"})
2020-07-12 18:25:53 +12:00
|> Api.create!()
%{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)
assert fetched_post == post
end
test "it returns nil when there is no matching record" do
2019-12-03 19:47:49 +13:00
assert {:ok, nil} = Api.get(Post, Ecto.UUID.generate())
end
test "it uses identities if they exist", %{post: post} do
assert {:ok, fetched_post} = Api.get(Post, uuid: post.uuid)
assert fetched_post == post
end
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
|> new(%{title: "test", contents: "yeet"})
2020-07-12 18:25:53 +12:00
|> Api.create!()
2019-12-08 10:33:31 +13:00
%{post: post}
end
test "it returns a matching record", %{post: post} do
assert ^post = Api.get!(Post, post.id)
end
test "it raises on an error", %{post: post} do
assert_raise(Ash.Error.Invalid.NoSuchResource, ~r/\No such resource Something/, fn ->
2019-12-08 10:33:31 +13:00
Api.get!(Something, post.id)
end)
end
end
describe "api.read/2 with no records" do
test "returns an empty result" do
assert {:ok, []} = Api.read(Post)
end
end
2019-12-08 10:33:31 +13:00
describe "Ash.read!/2 with no records" do
test "returns an empty result" do
assert [] = Api.read!(Post)
2019-12-08 10:33:31 +13:00
end
end
describe "api.read/2" do
setup do
2020-07-12 18:25:53 +12:00
post1 =
Post
|> new(%{title: "test", contents: "yeet"})
2020-07-12 18:25:53 +12:00
|> Api.create!()
post2 =
Post
|> new(%{title: "test1", contents: "yeet2"})
2020-07-12 18:25:53 +12:00
|> Api.create!()
%{post1: post1, post2: post2}
end
test "with a limit of 1, returns only 1 record" do
assert {:ok, [_post]} =
Post
|> Ash.Query.limit(1)
|> Api.read()
end
test "with a limit size of 2, returns 2 records" do
assert {:ok, [_, _]} =
Post
|> Ash.Query.limit(2)
|> Api.read()
end
test "with a limit of 1 and an offset of 1, it returns 1 record" do
assert {:ok, [_]} =
Post
|> Ash.Query.limit(1)
|> Ash.Query.offset(1)
|> Api.read()
end
end
2019-11-29 19:54:11 +13:00
2019-12-08 10:33:31 +13:00
describe "api.read!/2" do
setup do
2020-07-12 18:25:53 +12:00
post1 =
Post
|> new(%{title: "test", contents: "yeet"})
2020-07-12 18:25:53 +12:00
|> Api.create!()
post2 =
Post
|> 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
assert [_, _] = Api.read!(Post)
2019-12-08 10:33:31 +13:00
end
test "it raises on an error" do
assert_raise(
Ash.Error.Invalid,
2020-07-16 09:06:27 +12:00
~r/Invalid filter value `10` supplied in `title == 10`: Could not be casted to type Ash.Type.String/,
fn ->
Post
|> Ash.Query.filter(title: 10)
|> Api.read!()
end
)
2019-12-08 10:33:31 +13:00
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
|> new(%{title: "test", contents: "yeet"})
2020-07-12 18:25:53 +12:00
|> Api.create!()
post2 =
Post
|> new(%{title: "test1", contents: "yeet"})
2020-07-12 18:25:53 +12:00
|> Api.create!()
2019-11-29 19:54:11 +13:00
%{post1: post1, post2: post2}
end
test "a filter that matches nothing returns no results" do
assert {:ok, []} =
Post
|> Ash.Query.filter(contents: "not_yeet")
|> Api.read()
2019-11-29 19:54:11 +13:00
end
test "a filter returns only matching records", %{post1: post1} do
assert {:ok, [^post1]} =
Post
|> Ash.Query.filter(title: post1.title)
|> Api.read()
2019-11-29 19:54:11 +13:00
end
test "a filter returns multiple records if they match", %{post1: post1, post2: post2} do
assert {:ok, [_, _] = results} =
Post
|> Ash.Query.filter(contents: "yeet")
|> Api.read()
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
2019-12-12 11:45:59 +13:00
describe "relationship filters" do
setup do
2020-07-12 18:25:53 +12:00
author1 =
Author
|> new(%{name: "bruh"})
2020-07-12 18:25:53 +12:00
|> Api.create!()
author2 =
Author
|> new(%{name: "bruh"})
2020-07-12 18:25:53 +12:00
|> Api.create!()
post =
Post
|> new(%{title: "test", contents: "yeet"})
2020-07-12 18:25:53 +12:00
|> replace_relationship(:author1, author1)
|> replace_relationship(:author2, author2)
|> 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
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
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
|> new(%{title: "abc", contents: "abc"})
2020-07-12 18:25:53 +12:00
|> Api.create!()
post2 =
Post
|> new(%{title: "xyz", contents: "abc"})
2020-07-12 18:25:53 +12:00
|> Api.create!()
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
assert {:ok, [^post1, ^post2]} =
Post
|> Ash.Query.sort(title: :asc)
|> Api.read()
2019-11-30 08:40:19 +13:00
end
test "a sort will sor rows accordingly when descending", %{
post1: post1,
post2: post2
} do
assert {:ok, [^post2, ^post1]} =
Post
|> Ash.Query.sort(title: :desc)
|> Api.read()
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
|> new(%{title: "abc", contents: "xyz"})
2020-07-12 18:25:53 +12:00
|> Api.create!()
2019-11-30 08:40:19 +13:00
assert {:ok, [^post1, ^middle_post, ^post2]} =
Post
|> Ash.Query.sort(title: :asc, contents: :asc)
|> Api.read()
2019-11-30 08:40:19 +13:00
end
end
end