Skip to content

Commit

Permalink
fix: Fix issues in Ash.Generator.generate_many/2 (#1703)
Browse files Browse the repository at this point in the history
* docs(`Ash.Generator`): Fix example code blocks to render // correctly

* fix(`Ash.Generator`): Don't error if no `after_action` is provided to `generate_many`

* fix(`Ash.Generator`): Reuse the changeset actor when calling `bulk_create`

* chore(`Ash.Generator`): Read and send notifications created when generating many records

This prevents warnings in tests when using `generate_many` -

`[warning] Missed <x> notifications in action <action>`

* chore(`Ash.Generator`): Raise an error if the `bulk_create` command in `generate_many` wasn't a complete success

A partial success would mean less records were inserted than were requested,
definitely leading to buggy/flaky tests
  • Loading branch information
sevenseacat authored Jan 9, 2025
1 parent 946b3c2 commit 0d49b61
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 additions & 7 deletions lib/ash/generator/generator.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ defmodule Ash.Generator do
use Ash.Generator
# using `seed_generator`, bypasses the action and saves directly to the data layer
def blog_post(opts \\ []) do
def blog_post(opts \\\\ []) do
seed_generator(
%MyApp.Blog.Post{
name: sequence(:title, &"My Blog Post \#{&1}")
Expand All @@ -23,7 +23,7 @@ defmodule Ash.Generator do
end
# using `changeset_generator`, calls the action when passed to `generate`
def blog_post_comment(opts \\ []) do
def blog_post_comment(opts \\\\ []) do
blog_post_id = opts[:blog_post_id] || once(:default_blog_post_id, fn -> generate(blog_post()).id end)
changeset_generator(
Expand Down Expand Up @@ -505,12 +505,27 @@ defmodule Ash.Generator do
fn _changeset, record ->
{:ok, after_action.(record)}
end
else
# Do nothing
fn _changeset, record -> {:ok, record} end
end

Ash.bulk_create!(Enum.map(batch, & &1.params), first.resource, first.action,
after_action: after_action,
return_records?: true
).records || []
result =
Ash.bulk_create!(Enum.map(batch, & &1.params), first.resource, first.action.name,
after_action: after_action,
return_records?: true,
return_errors?: true,
return_notifications?: true,
actor: first.context[:private][:actor]
)

if result.status != :success do
raise Ash.Error.to_error_class(result.errors)
end

Ash.Notifier.notify(result.notifications)

result.records || []

batch ->
Enum.map(batch, fn record ->
Expand Down Expand Up @@ -641,7 +656,7 @@ defmodule Ash.Generator do
Generate input meant to be passed into `Ash.Seed.seed!/2`.
A map of custom `StreamData` generators can be provided to add to or overwrite the generated input,
for example: `Ash.Generator.for_seed(Post, %{text: StreamData.constant("Post")})`
for example: `Ash.Generator.seed_input(Post, %{text: StreamData.constant("Post")})`
"""
@spec seed_input(Ash.Resource.t(), map()) :: StreamData.t(map())
def seed_input(resource, generators \\ %{}) do
Expand Down

0 comments on commit 0d49b61

Please sign in to comment.