Skip to content

Commit

Permalink
test validation aggregates individually as well (#360)
Browse files Browse the repository at this point in the history
* test aggregates individually as well

* test with atomic and non atomic actions

* test atomic and non atomic actions

* use message in context
  • Loading branch information
barnabasJ committed Jul 23, 2024
1 parent 9e4efa7 commit 306dc84
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 31 deletions.
72 changes: 51 additions & 21 deletions test/atomics_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -294,26 +294,56 @@ defmodule AshPostgres.AtomicsTest do
|> Map.get(:records)
end

test "can use aggregates in validation" do
post =
Post
|> Ash.Changeset.for_create(:create, %{title: "foo", price: 1})
|> Ash.create!()

Comment
|> Ash.Changeset.for_create(:create, %{post_id: post.id, title: "foo"})
|> Ash.create!()

assert_raise Ash.Error.Invalid, ~r/Can only delete if Post has no comments/, fn ->
post
|> Ash.Changeset.for_update(:update_if_no_comments, %{title: "bar"})
|> Ash.update!()
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!()
Enum.each(
[
:exists,
:list,
:count,
:combined
],
fn aggregate ->
test "can use #{aggregate} in validation" do

Check failure on line 305 in test/atomics_test.exs

View workflow job for this annotation

GitHub Actions / ash-ci (15) / mix test

test can use list in validation (AshPostgres.AtomicsTest)

Check failure on line 305 in test/atomics_test.exs

View workflow job for this annotation

GitHub Actions / ash-ci (15) / mix test

test can use count in validation (AshPostgres.AtomicsTest)

Check failure on line 305 in test/atomics_test.exs

View workflow job for this annotation

GitHub Actions / ash-ci (15) / mix test

test can use exists in validation (AshPostgres.AtomicsTest)

Check failure on line 305 in test/atomics_test.exs

View workflow job for this annotation

GitHub Actions / ash-ci (15) / mix test

test can use combined in validation (AshPostgres.AtomicsTest)

Check failure on line 305 in test/atomics_test.exs

View workflow job for this annotation

GitHub Actions / ash-ci (14) / mix test

test can use list in validation (AshPostgres.AtomicsTest)

Check failure on line 305 in test/atomics_test.exs

View workflow job for this annotation

GitHub Actions / ash-ci (14) / mix test

test can use exists in validation (AshPostgres.AtomicsTest)

Check failure on line 305 in test/atomics_test.exs

View workflow job for this annotation

GitHub Actions / ash-ci (14) / mix test

test can use count in validation (AshPostgres.AtomicsTest)

Check failure on line 305 in test/atomics_test.exs

View workflow job for this annotation

GitHub Actions / ash-ci (14) / mix test

test can use combined in validation (AshPostgres.AtomicsTest)

Check failure on line 305 in test/atomics_test.exs

View workflow job for this annotation

GitHub Actions / ash-ci (16) / mix test

test can use combined in validation (AshPostgres.AtomicsTest)

Check failure on line 305 in test/atomics_test.exs

View workflow job for this annotation

GitHub Actions / ash-ci (16) / mix test

test can use exists in validation (AshPostgres.AtomicsTest)

Check failure on line 305 in test/atomics_test.exs

View workflow job for this annotation

GitHub Actions / ash-ci (16) / mix test

test can use count in validation (AshPostgres.AtomicsTest)

Check failure on line 305 in test/atomics_test.exs

View workflow job for this annotation

GitHub Actions / ash-ci (16) / mix test

test can use list in validation (AshPostgres.AtomicsTest)
post =
Post
|> Ash.Changeset.for_create(:create, %{title: "foo", price: 1})
|> Ash.create!()

Comment
|> Ash.Changeset.for_create(:create, %{post_id: post.id, title: "foo"})
|> Ash.create!()

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, %{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
)
end
54 changes: 44 additions & 10 deletions test/support/resources/post.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,32 @@ defmodule HasNoComments do
@moduledoc false
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
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, [],
expr(
length(list(comments, field: :id)) > 0 or
count(comments) > 0 or
exists(comments, true)
),
{:atomic, [], condition,
expr(
error(^Ash.Error.Changes.InvalidChanges, %{
message: "Can only delete if Post has no comments"
message: ^context.message || "Post has comments"
})
)}
]
Expand Down Expand Up @@ -115,11 +129,31 @@ defmodule AshPostgres.Test.Post do
end

destroy :destroy_if_no_comments do
validate(HasNoComments)
validate HasNoComments do
message "Can only delete if Post has no comments"
end
end

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

update :update_only_freds do
Expand Down

0 comments on commit 306dc84

Please sign in to comment.