2021-02-06 12:59:33 +13:00
|
|
|
defmodule AshPostgres.PolymorphismTest do
|
2021-01-29 13:42:55 +13:00
|
|
|
use AshPostgres.RepoCase, async: false
|
2021-02-06 12:59:33 +13:00
|
|
|
alias AshPostgres.Test.{Api, Post, Rating}
|
2021-01-29 13:42:55 +13:00
|
|
|
|
|
|
|
require Ash.Query
|
|
|
|
|
2021-02-06 12:59:33 +13:00
|
|
|
test "you can create related data" do
|
|
|
|
Post
|
|
|
|
|> Ash.Changeset.for_create(:create, rating: %{score: 10})
|
2021-06-06 10:13:20 +12:00
|
|
|
|> Api.create!()
|
2021-01-29 13:42:55 +13:00
|
|
|
|
2021-02-06 12:59:33 +13:00
|
|
|
assert [%{score: 10}] =
|
|
|
|
Rating
|
|
|
|
|> Ash.Query.set_context(%{data_layer: %{table: "post_ratings"}})
|
2021-06-06 10:13:20 +12:00
|
|
|
|> Api.read!()
|
2021-01-29 13:42:55 +13:00
|
|
|
end
|
|
|
|
|
2021-02-06 12:59:33 +13:00
|
|
|
test "you can read related data" do
|
|
|
|
Post
|
|
|
|
|> Ash.Changeset.for_create(:create, rating: %{score: 10})
|
2021-06-06 10:13:20 +12:00
|
|
|
|> Api.create!()
|
2021-01-29 13:42:55 +13:00
|
|
|
|
2021-02-06 12:59:33 +13:00
|
|
|
assert [%{score: 10}] =
|
|
|
|
Post
|
|
|
|
|> Ash.Query.load(:ratings)
|
|
|
|
|> Api.read_one!()
|
|
|
|
|> Map.get(:ratings)
|
2021-01-29 13:42:55 +13:00
|
|
|
end
|
|
|
|
end
|