Umbrella project clear database one each test run config

I would like to be able to clear my data on each test run. How should I structure my config files to achieve this within an umbrella project?

1 Like

I assume you’re using Ecto; from my experience, Ecto doesn’t care if your application is a standard app, or an app within an umbrella project. Are you running into any specific errors?

2 Likes

No errors, I just find myself having to truncate my db on each test run to clear values from the previous run. Is there a way in which I can run a script to empty particular tables on each test run? Maybe have a migration script that does that?

I have that same issue, some of my tests in my umbrella app are failing because of stale test data. I’ve never had that issue before (this is my first serious umbrella app, with separate apps for api, business logic and database). I suppose there is some missing bit of automation compared to a normal phoenix app? I will investigate, how did you end up dealing with this?

Make sure that an app using ecto has sandbox setup, so that it isolates tests (e.g. using transaction).

e.g. apps/my_app/test/support/test_case.ex

defmodule MyApp.TestCase do
  @moduledoc false

  use ExUnit.CaseTemplate

  using do
    quote do
      import MyApp.TestCase
    end
  end

  setup tags do
    :ok = Ecto.Adapters.SQL.Sandbox.checkout(MyEcto.Repo)

    unless tags[:async] do
      Ecto.Adapters.SQL.Sandbox.mode(MyEcto.Repo, {:shared, self()})
    end

    :ok
  end
end

Thanks, I had all that but I had made the mistake of having some records created at compile time. Fixed now.