test validation aggregates individually as well (#360)

* test aggregates individually as well

* test with atomic and non atomic actions

* test atomic and non atomic actions

* use message in context
This commit is contained in:
Barnabas Jovanovics 2024-07-23 12:55:44 +02:00 committed by GitHub
parent 9e4efa7b9b
commit 306dc84ac2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 93 additions and 29 deletions

View file

@ -294,26 +294,56 @@ defmodule AshPostgres.AtomicsTest do
|> Map.get(:records) |> Map.get(:records)
end end
test "can use aggregates in validation" do Enum.each(
post = [
Post :exists,
|> Ash.Changeset.for_create(:create, %{title: "foo", price: 1}) :list,
|> Ash.create!() :count,
:combined
],
fn aggregate ->
test "can use #{aggregate} in validation" do
post =
Post
|> Ash.Changeset.for_create(:create, %{title: "foo", price: 1})
|> Ash.create!()
Comment Comment
|> Ash.Changeset.for_create(:create, %{post_id: post.id, title: "foo"}) |> Ash.Changeset.for_create(:create, %{post_id: post.id, title: "foo"})
|> Ash.create!() |> Ash.create!()
assert_raise Ash.Error.Invalid, ~r/Can only delete if Post has no comments/, fn -> assert_raise Ash.Error.Invalid, ~r/Can only update if Post has no comments/, fn ->
post post
|> Ash.Changeset.for_update(:update_if_no_comments, %{title: "bar"}) |> Ash.Changeset.new()
|> Ash.update!() |> Ash.Changeset.put_context(:aggregate, unquote(aggregate))
|> Ash.Changeset.for_update(:update_if_no_comments, %{title: "bar"})
|> Ash.update!()
end
assert_raise Ash.Error.Invalid, ~r/Can only update if Post has no comments/, fn ->
post
|> Ash.Changeset.new()
|> Ash.Changeset.put_context(:aggregate, unquote(aggregate))
|> Ash.Changeset.for_update(:update_if_no_comments_non_atomic, %{title: "bar"})
|> Ash.update!()
end
assert_raise Ash.Error.Invalid, ~r/Can only delete if Post has no comments/, fn ->
post
|> Ash.Changeset.new()
|> Ash.Changeset.put_context(:aggregate, unquote(aggregate))
|> Ash.Changeset.for_destroy(:destroy_if_no_comments_non_atomic, %{})
|> Ash.destroy!()
end
assert_raise Ash.Error.Invalid, ~r/Can only delete if Post has no comments/, fn ->
post
|> Ash.Changeset.new()
|> Ash.Changeset.put_context(:aggregate, unquote(aggregate))
|> Ash.Changeset.for_destroy(:destroy_if_no_comments, %{})
|> Ash.destroy!()
end
end
end end
)
assert_raise Ash.Error.Invalid, ~r/Can only delete if Post has no comments/, fn ->
post
|> Ash.Changeset.for_destroy(:destroy_if_no_comments, %{})
|> Ash.destroy!()
end
end
end end

View file

@ -17,18 +17,32 @@ defmodule HasNoComments do
@moduledoc false @moduledoc false
use Ash.Resource.Validation use Ash.Resource.Validation
def atomic(_changeset, _opts, _context) do def atomic(changeset, _opts, context) do
# Test multiple types of aggregates in a single validation # Test multiple types of aggregates in a single validation
condition =
case changeset.context.aggregate do
:exists ->
expr(exists(comments, true))
:list ->
expr(length(list(comments, field: :id)) > 0)
:count ->
expr(count(comments) > 0)
:combined ->
expr(
exists(comments, true) and
length(list(comments, field: :id)) > 0 and
count(comments) > 0
)
end
[ [
{:atomic, [], {:atomic, [], condition,
expr(
length(list(comments, field: :id)) > 0 or
count(comments) > 0 or
exists(comments, true)
),
expr( expr(
error(^Ash.Error.Changes.InvalidChanges, %{ error(^Ash.Error.Changes.InvalidChanges, %{
message: "Can only delete if Post has no comments" message: ^context.message || "Post has comments"
}) })
)} )}
] ]
@ -115,11 +129,31 @@ defmodule AshPostgres.Test.Post do
end end
destroy :destroy_if_no_comments do destroy :destroy_if_no_comments do
validate(HasNoComments) validate HasNoComments do
message "Can only delete if Post has no comments"
end
end end
update :update_if_no_comments do update :update_if_no_comments do
validate(HasNoComments) validate HasNoComments do
message "Can only update if Post has no comments"
end
end
destroy :destroy_if_no_comments_non_atomic do
require_atomic?(false)
validate HasNoComments do
message "Can only delete if Post has no comments"
end
end
update :update_if_no_comments_non_atomic do
require_atomic?(false)
validate HasNoComments do
message "Can only update if Post has no comments"
end
end end
update :update_only_freds do update :update_only_freds do