fireproofsocks

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!

Showing Posts 1 to 8

NobbZ

NobbZ

Have you sandboxing enabled for ecto?

fireproofsocks

fireproofsocks OP

I think so. This is my test config:

use Mix.Config

config :myapp,
  MyApp.Repo,
  username: System.get_env("DB_USER") || "postgres",
  password: System.get_env("DB_PASS") || "",
  database: System.get_env("DB") || "myapp_test",
  hostname: System.get_env("DB_HOST") || "127.0.0.1",
  pool: Ecto.Adapters.SQL.Sandbox
fireproofsocks

fireproofsocks OP

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…

axelson

axelson

Scenic Core Team
NobbZ

NobbZ

Its from the generator, but as it seems not from the templates:

$ mix phx.new foo
$ tail -6 foo/config/test.exs
config :foo, Foo.Repo,
  username: "postgres",
  password: "postgres",
  database: "foo_test",
  hostname: "localhost",
  pool: Ecto.Adapters.SQL.Sandbox
axelson

axelson

Scenic Core Team

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

— All posts loaded —

Where Next? Top

Trending in Questions Top

Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
Onor.io
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
Trolleger
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New
matt-savvy
Anyone here using Honeybadger? My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of Bandit.HTTPError...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews