2023-12-15 11:10:11 +13:00
|
|
|
defmodule AshPostgres.ErrorExprTest do
|
|
|
|
use AshPostgres.RepoCase, async: false
|
2023-12-16 02:48:20 +13:00
|
|
|
alias AshPostgres.Test.{Api, Post}
|
2023-12-15 11:10:11 +13:00
|
|
|
|
|
|
|
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
|
2023-12-22 10:56:57 +13:00
|
|
|
|
|
|
|
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 ->
|
2023-12-30 15:49:34 +13:00
|
|
|
AshPostgres.TestRepo.transaction!(fn ->
|
2023-12-22 10:56:57 +13:00
|
|
|
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
|
2023-12-15 11:10:11 +13:00
|
|
|
end
|