Inconsistent record creation in tests

I have the following code in my test:

 for i <- 1..15 do
        Ash.create(Project, %{owner_id: user.id, name: "Project #{i}"}, authorize?: false)
 end

and this assertion:

assert {:ok, paginated_results} = Project.list_paginated(page: [offset: 10, count: true])

Now, these assertions should always pass, but they fail and pass intermittently:

assert length(paginated_result.results) == 5
assert paginated_result.count == 15

Sometimes, the total_count is 13, sometimes 14, and then 15. Tried using Enum functions, but got to the same issue and I can’t figure it out.
I am using the default setup for the create action. I am also using offset pagination, and this is what I have in the list_paginated action:

read :list_paginated do
      pagination offset?: true, default_limit:  10
      prepare build(sort: :created_at)
end

I have added this to my config/test.exs file:

config :ash, :disable_async?, true

but I still get to the same issue.

How can I resolve this?

Can you try using Ash.create! instead of Ash.create? My suspicion is that some of your creates are failing for some reason. This will cause them to raise errors instead of returning error tuples.

1 Like

You’re right, some of my creates were failing “silently”! Using Ash.create! revealed them.
Thanks @zachdaniel.

1 Like