add error test case for create

This commit is contained in:
Barnabas Jovanovics 2024-07-23 16:23:44 +02:00
parent 8400c3b2e2
commit ae4b459a65
3 changed files with 47 additions and 9 deletions

View file

@ -0,0 +1,14 @@
defmodule Ash.Error.Framework.CanNotBeAtomic do
@moduledoc "Used when a change that is only atomic cannot be done atomically"
use Ash.Error.Exception
use Splode.Error, fields: [:resource, :change, :reason], class: :framework
def message(error) do
"""
#{inspect(error.resource)} #{error.change} only has an atomic/3 callback, but cannot be performed atomically
Reason: #{error.reason}
"""
end
end

View file

@ -164,11 +164,6 @@ defmodule Ash.Test.Actions.BulkCreateTest do
global? true
end
validations do
validate AtomicOnlyValidation,
on: [:create]
end
calculations do
calculate :hidden_calc, :string, expr("something") do
public?(true)
@ -243,6 +238,10 @@ defmodule Ash.Test.Actions.BulkCreateTest do
change set_context(%{authorize?: arg(:authorize?)})
end
create :create_with_atomic_only_validation do
validate AtomicOnlyValidation
end
end
identities do
@ -1289,4 +1288,19 @@ defmodule Ash.Test.Actions.BulkCreateTest do
} = tag.related_tags
end
end
test "shows an error if an atomic only validation is used in a create" do
assert_raise Ash.Error.Framework.CanNotBeAtomic,
~r/Post AtomicOnlyValidation only has an atomic\/3 callback, but cannot be performed atomically/,
fn ->
[%{title: "title1", authorize?: true}, %{title: "title2", authorize?: true}]
|> Ash.bulk_create!(
Post,
:create_with_atomic_only_validation,
authorize?: true,
return_stream?: true,
return_records?: true
)
end
end
end

View file

@ -255,10 +255,6 @@ defmodule Ash.Test.Actions.CreateTest do
private?(true)
end
validations do
validate AtomicOnlyValidation
end
actions do
default_accept :*
defaults [:read, :destroy, create: :*, update: :*]
@ -270,6 +266,10 @@ defmodule Ash.Test.Actions.CreateTest do
create :create_with_nested_array_argument do
argument :array_of_names, {:array, {:array, :string}}
end
create :with_atomic_only_validations do
validate AtomicOnlyValidation
end
end
changes do
@ -1425,4 +1425,14 @@ defmodule Ash.Test.Actions.CreateTest do
assert_receive {:error, _error}
end
test "shows an error if an atomic only validation is used in a create" do
assert_raise Ash.Error.Framework.CanNotBeAtomic,
~r/Post AtomicOnlyValidation only has an atomic\/3 callback, but cannot be performed atomically/,
fn ->
Post
|> Ash.Changeset.for_create(:with_atomic_only_validations, %{title: "foo"})
|> Ash.create!()
end
end
end