Skip to content

Commit be244ae

Browse files
committed
test: add tests for after_transaction hook batch change callbacks
1 parent ef2a4c6 commit be244ae

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

lib/resource/changes/create_new_version.ex

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,10 +280,12 @@ defmodule AshPaperTrail.Resource.Changes.CreateNewVersion do
280280
domain: changeset.domain,
281281
stop_on_error?: true,
282282
return_errors?: true,
283+
return_records?: true,
283284
skip_unknown_inputs: Enum.flat_map(inputs, &Map.keys(&1))
284285
]
285286

286-
Ash.bulk_create!(inputs, version_changeset.resource, :create, opts)
287+
inputs
288+
|> Ash.bulk_create!(version_changeset.resource, :create, opts)
287289
|> Map.get(:notifications)
288290
end
289291

test/ash_paper_trail_test.exs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,35 @@ defmodule AshPaperTrailTest do
168168
|> Enum.sort_by(& &1.version_action_type)
169169
end
170170

171+
test "a new version is created on a bulk update with enumerable and after_transaction" do
172+
%{subject: "subject", body: "body", id: post_id} =
173+
post = Posts.Post.create!(@valid_attrs, tenant: "acme")
174+
175+
%Ash.BulkResult{
176+
status: :success
177+
} =
178+
Ash.bulk_update!([post], :publish, %{},
179+
tenant: "acme",
180+
strategy: :stream,
181+
return_records?: true,
182+
return_errors?: true
183+
)
184+
185+
assert [
186+
_,
187+
%{
188+
changes: %{
189+
published: true
190+
},
191+
version_action_type: :update,
192+
version_action_name: :publish,
193+
version_source_id: ^post_id
194+
}
195+
] =
196+
Ash.read!(Posts.Post.Version, tenant: "acme")
197+
|> Enum.sort_by(& &1.version_action_type)
198+
end
199+
171200
test "a new version is created on a bulk update with query" do
172201
%{subject: "subject", body: "body", id: post_id} =
173202
Posts.Post.create!(@valid_attrs, tenant: "acme")

test/support/posts/post.ex

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ defmodule AshPaperTrail.Test.Posts.Post do
4444
require_atomic? false
4545
accept []
4646
change set_attribute(:published, true)
47+
48+
change after_transaction(fn _changeset, res, _ ->
49+
res
50+
end)
4751
end
4852
end
4953

0 commit comments

Comments
 (0)