ash_postgres/test/schema_test.exs
Zach Daniel e8643d4aa0 fix: properly type cast top level fragments
chore: update tests not to use deprecated helper
2022-09-21 13:36:18 -04:00

62 lines
1.9 KiB
Elixir

defmodule AshPostgres.SchemaTest do
@moduledoc false
use AshPostgres.RepoCase, async: false
alias AshPostgres.Test.{Api, Author, Profile}
require Ash.Query
setup do
[author: Api.create!(Ash.Changeset.for_create(Author, :create, %{}))]
end
test "data can be created", %{author: author} do
assert %{description: "foo"} =
Profile
|> Ash.Changeset.for_create(:create, %{description: "foo"})
|> Ash.Changeset.manage_relationship(:author, author, type: :append_and_remove)
|> Api.create!()
end
test "data can be read", %{author: author} do
Profile
|> Ash.Changeset.for_create(:create, %{description: "foo"})
|> Ash.Changeset.manage_relationship(:author, author, type: :append_and_remove)
|> Api.create!()
assert [%{description: "foo"}] = Profile |> Api.read!()
end
test "they can be filtered across", %{author: author} do
profile =
Profile
|> Ash.Changeset.for_create(:create, %{description: "foo"})
|> Ash.Changeset.manage_relationship(:author, author, type: :append_and_remove)
|> Api.create!()
Api.create!(Ash.Changeset.for_create(Author, :create, %{}))
assert [_] =
Author
|> Ash.Query.filter(profile.id == ^profile.id)
|> Api.read!()
assert [_] =
Profile
|> Ash.Query.filter(author.id == ^author.id)
|> Api.read!()
end
test "aggregates work across schemas", %{author: author} do
Profile
|> Ash.Changeset.for_create(:create, %{description: "foo"})
|> Ash.Changeset.manage_relationship(:author, author, type: :append_and_remove)
|> Api.create!()
assert [%{profile_description: "foo"}] =
Author
|> Ash.Query.filter(profile_description == "foo")
|> Api.read!()
assert %{profile_description: "foo"} = Api.load!(author, :profile_description)
end
end