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…

Where Next?

Popular in Questions Top

sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lists...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
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
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
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
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New

Other popular topics Top

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
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
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
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
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
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

We're in Beta

About us Mission Statement