Skip to content

Commit

Permalink
test: add some explicit destroy/soft destroy tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zachdaniel committed Apr 21, 2024
1 parent a66a0f6 commit 8f1ae86
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
43 changes: 43 additions & 0 deletions test/destroy_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
defmodule AshPostgres.DestroyTest do
use AshPostgres.RepoCase, async: false
alias AshPostgres.Test.Post

test "destroy action destroys the record" do
post =
Post
|> Ash.Changeset.for_create(:create, %{})
|> Ash.create!()

post
|> Ash.Changeset.for_destroy(:destroy, %{})
|> Ash.destroy!()

assert [] = Ash.read!(Post)
end

test "before action hooks are honored" do
post =
Post
|> Ash.Changeset.for_create(:create, %{})
|> Ash.create!()

assert_raise Ash.Error.Invalid, ~r/must type CONFIRM/, fn ->
post
|> Ash.Changeset.for_destroy(:destroy_with_confirm, %{confirm: "NOT CONFIRM"})
|> Ash.destroy!()
end
end

test "before action hooks are honored, for soft destroys as well" do
post =
Post
|> Ash.Changeset.for_create(:create, %{})
|> Ash.create!()

assert_raise Ash.Error.Invalid, ~r/must type CONFIRM/, fn ->
post
|> Ash.Changeset.for_destroy(:soft_destroy_with_confirm, %{confirm: "NOT CONFIRM"})
|> Ash.destroy!()
end
end
end
28 changes: 28 additions & 0 deletions test/support/resources/post.ex
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,34 @@ defmodule AshPostgres.Test.Post do

defaults([:destroy])

destroy :destroy_with_confirm do
argument(:confirm, :string, allow_nil?: false)

change(fn changeset, _ ->
Ash.Changeset.before_action(changeset, fn changeset ->
if changeset.arguments.confirm == "CONFIRM" do
changeset
else
Ash.Changeset.add_error(changeset, field: :confirm, message: "must type CONFIRM")
end
end)
end)
end

destroy :soft_destroy_with_confirm do
argument(:confirm, :string, allow_nil?: false)

change(fn changeset, _ ->
Ash.Changeset.before_action(changeset, fn changeset ->
if changeset.arguments.confirm == "CONFIRM" do
changeset
else
Ash.Changeset.add_error(changeset, field: :confirm, message: "must type CONFIRM")
end
end)
end)
end

update :update do
primary?(true)
require_atomic?(false)
Expand Down

0 comments on commit 8f1ae86

Please sign in to comment.