fireproofsocks
I have discovered something curious: when one test macro creates a row, that row is still visible to another test. I only noticed this occurring because this particular schema is dealing with user-generated primary keys (sort of an ersatz enum – it’s a very small table).
I’m doing a full drop and create before my test runs, so I know that the data isn’t stuck in there from some other process. Here’s my alias from mix.exs:
defp aliases do
[
test: ["ecto.drop", "ecto.create", "ecto.migrate", "run priv/repo/seeds.exs", "test"]
]
end
I feel like this might be something like threads getting crossed… e.g. if test1 creates a record with primary key “X”, then test2 attempts to create a record with primary key “X”, there’s a problem.
1) test update/2 returns :ok when updating an existing thing (MyApp.Contexts.ThingContextTest)
test/contexts/thing_context_test.exs:53
** (Ecto.ConstraintError) constraint error when attempting to insert struct:
* thing_pkey (unique_constraint)
If you would like to stop this constraint violation from raising an
exception and instead add it as an error to your changeset, please
call `unique_constraint/3` on your changeset with the constraint
`:name` as an option.
The changeset defined the following constraints:
* thing_id_index (unique_constraint)
code: status_fixture(%{
stacktrace:
(ecto) lib/ecto/repo/schema.ex:689: anonymous fn/4 in Ecto.Repo.Schema.constraints_to_errors/3
(elixir) lib/enum.ex:1327: Enum."-map/2-lists^map/1-0-"/2
(ecto) lib/ecto/repo/schema.ex:674: Ecto.Repo.Schema.constraints_to_errors/3
(ecto) lib/ecto/repo/schema.ex:276: anonymous fn/15 in Ecto.Repo.Schema.do_insert/3
test/contexts/wholesale_partner_status_context_test.exs:55: (test)
I haven’t run up against this exact behavior even though I’ve done the same type of thing for other modules (i.e. with string primary keys), so I thought maybe it had something to do with how the application was started? My test helper is simply doing ExUnit.start() and I’m doing a simple use ExUnit.Case (no async).
If I run the tests in the module 1 at a time, they all pass, e.g. mix test test/contexts/thing_test.exs:66
Any ideas?
Many thanks for any pointers!
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixir-ls
- #ai
- #elixirconf-us
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 8- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
NobbZ
Have you sandboxing enabled for ecto?
fireproofsocks
I think so. This is my test config:
use Mix.Config
fireproofsocks
AHA. I Found it. I had copied and pasted the WRONG repo in my test:
Should have been
axelson
Also I’m pretty sure that a
:poolconfig on the repo doesn’t do anything you should probably drop thepool: Ecto.Adapters.SQL.Sandboxoption from there.NobbZ
AFAIR this is how it falls out of the generators…
axelson
No, a
:poolconfig is in neither the currentconfig/test.exsorconfig/config.exs:Current master
config/test.exs:https://github.com/phoenixframework/phoenix/blob/2c586b7d45e0d7fa5cb1b640dc2b624b04afbff9/installer/templates/phx_single/config/test.exs
Current master
config/config.exs:https://github.com/phoenixframework/phoenix/blob/master/installer/templates/phx_single/config/config.exs
NobbZ
Its from the generator, but as it seems not from the templates:
axelson
Ah, I see.
:poolis configuration forEcto.Adapters.Postgres: Ecto.Adapters.Postgres — Ecto SQL v3.14.0