Why isn't ecto not deleting data from setup_all block after tests?

I have the following in my setup_all

  setup_all do
    :ok = Ecto.Adapters.SQL.Sandbox.checkout(Repo)
    Ecto.Adapters.SQL.Sandbox.mode(Repo, :auto)
    locales = [%{id: "en_US"}, %{id: "en_CA"}]
    Repo.insert_all(Locale, locales)
    :ok
  end

I expected that after the tests the test database would be empty, but it is not. I thought ecto automatically deleted the data after tests? But when I check the test database the data is still there?

Does ecto not delete data in a setup_all block?

You cannot run the sandbox with setup_all because its functionality works by being run in the same process as the individual tests. setup_all does run in a separate process.

3 Likes

You can use on_exit/2 in a setup_all to do clean-up tasks.

ah i see, guess i need to spend a bit more time reading about Sandbox as it’s clear i haven’t fully grasped whats going on there!

thanks!

i tried that but it didn’t work unfortunately

Hmm, so a Repo.delete_all(...) inside the on_exit callback doesn’t do it?
I’m wondering now why Repo.insert_all(Local, locales) doesn’t throw a unique constraint error. Since the locales must exist from a previous run, you should get an exception from your database, no?

It did throw a unique constraint error the second time i ran the tests.

I had to keep resetting the DB after the test.