test more aggregates

This commit is contained in:
Barnabas Jovanovics 2024-07-22 16:43:37 +02:00
parent ece56c2bc9
commit e3ac63f5d6
2 changed files with 9 additions and 5 deletions

View file

@ -303,8 +303,6 @@ defmodule AshPostgres.AtomicsTest do
|> Ash.Changeset.for_create(:create, %{post_id: post.id, title: "foo"})
|> Ash.create!()
Logger.configure(level: :debug)
assert_raise Ash.Error.Invalid, ~r/Can only delete if Post has no comments/, fn ->
post
|> Ash.Changeset.for_destroy(:destroy_if_no_comments, %{})

View file

@ -18,9 +18,15 @@ defmodule HasNoComments do
use Ash.Resource.Validation
def atomic(_changeset, _opts, _context) do
# This uses the list aggregate because we want to specifically test this aggregate
{:atomic, [], expr(list(comments, field: :id) > 0),
expr(error(^Invalid, %{message: "Can only delete if Post has no comments"}))}
# Test multiple types of aggregates in a single validation
[
{:atomic, [],
expr(
list(comments, field: :id) > 0 or
count(comments) > 0 or
exists(comments, true)
), expr(error(^Invalid, %{message: "Can only delete if Post has no comments"}))}
]
end
end