2019-12-02 17:04:16 +13:00
|
|
|
defmodule Ash.Test.Actions.ReadTest do
|
2019-11-28 18:24:29 +13:00
|
|
|
use ExUnit.Case, async: true
|
|
|
|
|
2019-12-02 17:04:16 +13:00
|
|
|
defmodule Post do
|
|
|
|
use Ash.Resource, name: "posts", type: "post"
|
|
|
|
use Ash.DataLayer.Ets, private?: true
|
2019-11-28 18:24:29 +13:00
|
|
|
|
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 :title, :string
|
|
|
|
attribute :contents, :string
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defmodule Api do
|
|
|
|
use Ash.Api
|
|
|
|
|
2019-12-06 05:50:30 +13:00
|
|
|
resources [Post]
|
2019-12-02 17:04:16 +13:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "api.get/3" do
|
2019-11-28 18:24:29 +13:00
|
|
|
setup do
|
2019-12-02 17:04:16 +13:00
|
|
|
{:ok, post} = Api.create(Post, %{attributes: %{title: "test", contents: "yeet"}})
|
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
|
|
|
|
|
|
|
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())
|
2019-11-28 18:24:29 +13:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-12-08 10:33:31 +13:00
|
|
|
describe "api.get!/3" do
|
|
|
|
setup do
|
|
|
|
{:ok, post} = Api.create(Post, %{attributes: %{title: "test", contents: "yeet"}})
|
|
|
|
%{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.FrameworkError, "no such resource", fn ->
|
|
|
|
Api.get!(Something, post.id)
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "api.read/2 with no records" do
|
2019-11-28 18:24:29 +13:00
|
|
|
test "returns an empty result" do
|
2019-12-02 17:04:16 +13:00
|
|
|
assert {:ok, %{results: []}} = Api.read(Post)
|
2019-11-28 18:24:29 +13:00
|
|
|
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 %{results: []} = Api.read!(Post)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "api.read/2" do
|
2019-11-28 18:24:29 +13:00
|
|
|
setup do
|
2019-12-02 17:04:16 +13:00
|
|
|
{:ok, post1} = Api.create(Post, %{attributes: %{title: "test", contents: "yeet"}})
|
|
|
|
{:ok, post2} = Api.create(Post, %{attributes: %{title: "test1", contents: "yeet2"}})
|
2019-11-28 18:24:29 +13:00
|
|
|
|
|
|
|
%{post1: post1, post2: post2}
|
|
|
|
end
|
|
|
|
|
|
|
|
test "with page size of 1, returns only 1 record" do
|
2019-12-02 17:04:16 +13:00
|
|
|
assert {:ok, %{results: [_post]}} = Api.read(Post, %{page: %{limit: 1}})
|
2019-11-28 18:24:29 +13:00
|
|
|
end
|
|
|
|
|
|
|
|
test "with page size of 2, returns 2 records" do
|
2019-12-02 17:04:16 +13:00
|
|
|
assert {:ok, %{results: [_, _]}} = Api.read(Post, %{page: %{limit: 2}})
|
2019-11-28 18:24:29 +13:00
|
|
|
end
|
|
|
|
|
|
|
|
test "with page size of 1 and an offset of 1, it returns 1 record" do
|
2019-12-02 17:04:16 +13:00
|
|
|
assert {:ok, %{results: [_]}} = Api.read(Post, %{page: %{limit: 1, offset: 1}})
|
2019-11-28 18:24:29 +13:00
|
|
|
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
|
|
|
|
{:ok, post1} = Api.create(Post, %{attributes: %{title: "test", contents: "yeet"}})
|
|
|
|
{:ok, post2} = Api.create(Post, %{attributes: %{title: "test1", contents: "yeet2"}})
|
|
|
|
|
|
|
|
%{post1: post1, post2: post2}
|
|
|
|
end
|
|
|
|
|
|
|
|
test "it returns the records not in a tuple" do
|
|
|
|
assert %{results: [_, _]} = Api.read!(Post)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "it raises on an error" do
|
|
|
|
assert_raise(Ash.Error.FrameworkError, "Invalid value: 10 for :title", fn ->
|
|
|
|
Api.read!(Post, %{filter: %{title: 10}})
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-11-29 19:54:11 +13:00
|
|
|
describe "filters" do
|
|
|
|
setup do
|
2019-12-02 17:04:16 +13:00
|
|
|
{:ok, post1} = Api.create(Post, %{attributes: %{title: "test", contents: "yeet"}})
|
|
|
|
{:ok, post2} = Api.create(Post, %{attributes: %{title: "test1", contents: "yeet"}})
|
2019-11-29 19:54:11 +13:00
|
|
|
|
|
|
|
%{post1: post1, post2: post2}
|
|
|
|
end
|
|
|
|
|
|
|
|
test "a filter that matches nothing returns no results" do
|
2019-12-02 17:04:16 +13:00
|
|
|
assert {:ok, %{results: []}} = Api.read(Post, %{filter: %{contents: "not_yeet"}})
|
2019-11-29 19:54:11 +13:00
|
|
|
end
|
|
|
|
|
|
|
|
test "a filter returns only matching records", %{post1: post1} do
|
2019-12-02 17:04:16 +13:00
|
|
|
assert {:ok, %{results: [^post1]}} = Api.read(Post, %{filter: %{title: post1.title}})
|
2019-11-29 19:54:11 +13:00
|
|
|
end
|
|
|
|
|
|
|
|
test "a filter returns multiple records if they match", %{post1: post1, post2: post2} do
|
2019-12-02 17:04:16 +13:00
|
|
|
assert {:ok, %{results: [_, _] = results}} = Api.read(Post, %{filter: %{contents: "yeet"}})
|
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
|
|
|
|
|
|
|
describe "sort" do
|
|
|
|
setup do
|
2019-12-02 17:04:16 +13:00
|
|
|
{:ok, post1} = Api.create(Post, %{attributes: %{title: "abc", contents: "abc"}})
|
|
|
|
{:ok, post2} = Api.create(Post, %{attributes: %{title: "xyz", contents: "abc"}})
|
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
|
2019-12-02 17:04:16 +13:00
|
|
|
assert {:ok, %{results: [^post1, ^post2]}} = Api.read(Post, %{sort: [asc: :title]})
|
2019-11-30 08:40:19 +13:00
|
|
|
end
|
|
|
|
|
|
|
|
test "a sort will sor rows accordingly when descending", %{
|
|
|
|
post1: post1,
|
|
|
|
post2: post2
|
|
|
|
} do
|
2019-12-02 17:04:16 +13:00
|
|
|
assert {:ok, %{results: [^post2, ^post1]}} = Api.read(Post, %{sort: [desc: :title]})
|
2019-11-30 08:40:19 +13:00
|
|
|
end
|
|
|
|
|
|
|
|
test "a nested sort sorts accordingly", %{post1: post1, post2: post2} do
|
2019-12-02 17:04:16 +13:00
|
|
|
{:ok, middle_post} = Api.create(Post, %{attributes: %{title: "abc", contents: "xyz"}})
|
2019-11-30 08:40:19 +13:00
|
|
|
|
|
|
|
assert {:ok, %{results: [^post1, ^middle_post, ^post2]}} =
|
2019-12-02 17:04:16 +13:00
|
|
|
Api.read(Post, %{sort: [asc: :title, asc: :contents]})
|
2019-11-30 08:40:19 +13:00
|
|
|
end
|
|
|
|
end
|
2019-11-28 18:24:29 +13:00
|
|
|
end
|