2019-12-12 11:45:59 +13:00
|
|
|
defmodule Ash.Test.Actions.SideLoadTest do
|
2020-06-02 17:47:25 +12:00
|
|
|
@moduledoc false
|
2019-12-12 11:45:59 +13:00
|
|
|
use ExUnit.Case, async: true
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
actions do
|
|
|
|
read :default
|
|
|
|
create :default
|
|
|
|
end
|
|
|
|
|
|
|
|
attributes do
|
2020-06-01 17:14:23 +12:00
|
|
|
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-06-05 15:34:44 +12:00
|
|
|
has_many :posts, Ash.Test.Actions.SideLoadTest.Post,
|
|
|
|
reverse_relationship: :author,
|
|
|
|
destination_field: :author_id
|
2019-12-12 11:45:59 +13:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
ets do
|
|
|
|
private?(true)
|
|
|
|
end
|
2019-12-12 11:45:59 +13:00
|
|
|
|
|
|
|
actions do
|
|
|
|
read :default
|
|
|
|
create :default
|
|
|
|
end
|
|
|
|
|
|
|
|
attributes do
|
2020-06-01 17:14:23 +12:00
|
|
|
attribute :id, :uuid, primary_key?: true, default: &Ecto.UUID.generate/0
|
2019-12-12 11:45:59 +13:00
|
|
|
attribute :title, :string
|
|
|
|
attribute :contents, :string
|
|
|
|
end
|
|
|
|
|
|
|
|
relationships do
|
2019-12-16 13:20:44 +13:00
|
|
|
belongs_to :author, Author, reverse_relationship: :posts
|
2019-12-12 11:45:59 +13:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defmodule Api do
|
2020-06-02 17:47:25 +12:00
|
|
|
@moduledoc false
|
2019-12-12 11:45:59 +13:00
|
|
|
use Ash.Api
|
|
|
|
|
2020-06-14 18:39:11 +12:00
|
|
|
resources do
|
|
|
|
resource(Author)
|
|
|
|
resource(Post)
|
|
|
|
end
|
2019-12-12 11:45:59 +13:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "side_loads" do
|
2019-12-20 18:30:27 +13:00
|
|
|
setup do
|
|
|
|
author = Api.create!(Author, attributes: %{name: "zerg"})
|
2019-12-12 11:45:59 +13:00
|
|
|
|
2019-12-20 18:30:27 +13:00
|
|
|
post1 =
|
|
|
|
Api.create!(Post, attributes: %{title: "post1"}, relationships: %{author: author.id})
|
2019-12-12 11:45:59 +13:00
|
|
|
|
2019-12-20 18:30:27 +13:00
|
|
|
post2 =
|
|
|
|
Api.create!(Post, attributes: %{title: "post2"}, relationships: %{author: author.id})
|
2019-12-12 11:45:59 +13:00
|
|
|
|
2019-12-20 18:30:27 +13:00
|
|
|
%{post1: post1, post2: post2}
|
2019-12-12 11:45:59 +13:00
|
|
|
end
|
|
|
|
|
2019-12-20 18:30:27 +13:00
|
|
|
test "it allows sideloading related data", %{post1: post1, post2: post2} do
|
2020-05-14 03:54:44 +12:00
|
|
|
[author] =
|
|
|
|
Author
|
|
|
|
|> Api.query()
|
|
|
|
|> Ash.Query.side_load(posts: [:author])
|
|
|
|
|> Ash.Query.filter(posts: [id: post1.id])
|
|
|
|
|> Api.read!()
|
2019-12-12 11:45:59 +13:00
|
|
|
|
2019-12-20 18:30:27 +13:00
|
|
|
assert Enum.sort(Enum.map(author.posts, &Map.get(&1, :id))) ==
|
|
|
|
Enum.sort([post1.id, post2.id])
|
2019-12-20 17:19:34 +13:00
|
|
|
|
2019-12-20 18:30:27 +13:00
|
|
|
for post <- author.posts do
|
|
|
|
assert post.author.id == author.id
|
|
|
|
end
|
|
|
|
end
|
2019-12-12 11:45:59 +13:00
|
|
|
end
|
|
|
|
end
|