ash_postgres/test/error_expr_test.exs
Zach Daniel 2a2fd30e33 improvement: support new return_query/2 callback
improvement: support new `:no_rollback` error signal
2023-12-29 21:49:34 -05:00

57 lines
1.5 KiB
Elixir

defmodule AshPostgres.ErrorExprTest do
use AshPostgres.RepoCase, async: false
alias AshPostgres.Test.{Api, Post}
require Ash.Query
import Ash.Expr
test "exceptions in filters are treated as regular Ash exceptions" do
Post
|> Ash.Changeset.new(%{title: "title"})
|> Api.create!()
assert_raise Ash.Error.Invalid, ~r/this is bad!/, fn ->
Post
|> Ash.Query.filter(
error(Ash.Error.Query.InvalidFilterValue, message: "this is bad!", value: 10)
)
|> Api.read!()
end
end
test "exceptions in calculations are treated as regular Ash exceptions" do
Post
|> Ash.Changeset.new(%{title: "title"})
|> Api.create!()
assert_raise Ash.Error.Invalid, ~r/this is bad!/, fn ->
Post
|> Ash.Query.calculate(
:test,
expr(error(Ash.Error.Query.InvalidFilterValue, message: "this is bad!", value: 10)),
:string
)
|> Api.read!()
|> Enum.map(& &1.calculations)
end
end
test "exceptions in calculations are treated as regular Ash exceptions in transactions" do
Post
|> Ash.Changeset.new(%{title: "title"})
|> Api.create!()
assert_raise Ash.Error.Invalid, ~r/this is bad!/, fn ->
AshPostgres.TestRepo.transaction!(fn ->
Post
|> Ash.Query.calculate(
:test,
expr(error(Ash.Error.Query.InvalidFilterValue, message: "this is bad!", value: 10)),
:string
)
|> Api.read!()
|> Enum.map(& &1.calculations)
end)
end
end
end