fireproofsocks

fireproofsocks

Test data is persisting across tests

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!

Marked As Solved

NobbZ

NobbZ

Have you sandboxing enabled for ecto?

Also Liked

fireproofsocks

fireproofsocks

AHA. I Found it. I had copied and pasted the WRONG repo in my test:

setup do
  :ok = Ecto.Adapters.SQL.Sandbox.checkout(WrongThing.Repo)
end

Should have been

setup do
  :ok = Ecto.Adapters.SQL.Sandbox.checkout(Thing.Repo)
end
axelson

axelson

Scenic Core Team

Also I’m pretty sure that a :pool config on the repo doesn’t do anything you should probably drop the pool: Ecto.Adapters.SQL.Sandbox option from there.

NobbZ

NobbZ

AFAIR this is how it falls out of the generators…

Last Post!

axelson

axelson

Scenic Core Team

Ah, I see. :pool is configuration for Ecto.Adapters.Postgres: Ecto.Adapters.Postgres — Ecto SQL v3.14.0

Where Next?

Popular in Questions Top

vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New

Other popular topics Top

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement