Getting error: could not lookup Ecto repo Raven.Repo because it was not started or it does not exist

Hello everybody, I’m currently working on a project that has around 200+ test files. I’m running the tests using the following command:

mix test <path_to_folder> --seed=0

I’m using --seed=0 to ensure that the order of all executions remains the same.

Now, my problem is quite strange. When I run a test by specifying a single .exs file, it works perfectly. However, when I run the command on the entire folder, it fails.

To be more specific, I’ve done some testing and confirmed that the issue is not caused by the other tests themselves. When I run the command on the folder, the number of failing tests changes—sometimes it’s 50, other times it’s 70. But when I run those same tests individually, they all pass.

I’ve already tried running mix ecto.reset, but that didn’t resolve the problem (and the command itself runs without any issues).

Does anyone have an idea of what could be causing this?

This is my test_helper.ex

ExUnit.start()

{:ok, _} = Application.ensure_all_started(:ex_machina)
{:ok, _} = Application.ensure_all_started(:raven)

Ecto.Adapters.SQL.Sandbox.mode(Raven.Repo, :manual)
Ecto.Adapters.SQL.Sandbox.mode(Raven.Repo.ReadReplica, :manual)

-configs related to the Raven.Repo in the config.ex

config :raven,
  ecto_repos: [Raven.Repo, Raven.Repo.ReadReplica],
  whitelabel_lambda_url: "url"

config :raven, Oban,
  repo: Raven.Repo,
  plugins: [
    {Oban.Plugins.Pruner, max_age: 60 * 60 * 24 * 7},
    {Oban.Plugins.Lifeline, rescue_after: :timer.minutes(30)},
    {Oban.Plugins.Cron,
     crontab: [
       {"0 4 * * *", Timeline.FutureTransaction.Worker, max_attempts: 3},
       {"0 5 * * *", Raven.Workers.InsightDeactivationWorker, max_attempts: 3}
     ]}
  ],
  queues: [
    default: 10,
    sms_notifications: 10,
    email_notifications: 10,
    verify_user_configs: 10,
    push_notifications: 10,
    routine_jobs: 1
  ]

config :raven, Raven.Repo,
  database: "database",
  username: "username",
  password: "password",
  hostname: "localhost"

config :turbo_ecto, Turbo.Ecto,
  repo: Raven.Repo,
  per_page: 10
  • I have some auxiliary classes, and one of them is Raven.DataCase. This is the file responsible for handling setting up the repo, which I believe is correct.

  use ExUnit.CaseTemplate, async: false

  using do
    quote do
      alias Raven.Repo

      import Ecto
      import Ecto.Changeset
      import Ecto.Query
      import Raven.DataCase
      import Factory
    end
  end

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

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

    :ok
  end

  def errors_on(changeset) do
    Ecto.Changeset.traverse_errors(changeset, fn {message, opts} ->
      Regex.replace(~r"%{(\w+)}", message, fn _, key ->
        opts |> Keyword.get(String.to_existing_atom(key), key) |> to_string()
      end)
    end)
  end
end

My environment is set to test, which I believe is correct. I’m using a local database. This is the file:

import Config

# Configure your databases
repos = [
  Raven.Repo,
  Raven.Repo.ReadReplica
]

for repo <- repos do
  config :raven, repo,
    username: "username",
    password: "password",
    database: "database",
    hostname: "localhost",
    pool: Ecto.Adapters.SQL.Sandbox,
    queue_interval: 1_000,
    parameters: [
      tcp_keepalives_idle: "60",
      tcp_keepalives_interval: "5",
      tcp_keepalives_count: "3"
    ],
    socket_options: [keepalive: true]
end

config :junit_formatter,
  report_file: "report_file_test.xml",
  report_dir: Path.absname("test_result"),
  print_report_file: true,
  prepend_project_name?: true,
  include_filename?: true

# We don't run a server during test. If one is required,
# you can enable the server option below.
config :raven, RavenWeb.Endpoint,
  http: [port: 4002, compress: true],
  server: false

config :raven, Raven.Tracer, disabled?: true

## Print only warnings and errors during test
config :logger, level: :warning

config :daily_balance,
  supress_consumers: true

config :timeline,
  supress_consumers: true

config :raven,
  supress_consumers: true,
  supress_tracking: true,
  supress_fcm: true,
  supress_oban: true

# config :raven, :repo_api, Raven.Utils.MockRepo
config :raven, :repo_api, Raven.Repo

config :unleash_fresha, Unleash,
  url: "http://localhost/api/",
  appname: "raven",
  features_period: 60 * 60 * 1000,
  strategies: Unleash.Strategies,
  disable_client: true,
  disable_metrics: true,
  retries: -1,
  app_env: :dev,
  custom_http_headers: [
    {"authorization", ""}
  ]

Aren’t the repos started in your supervision tree?

I think a snippet of your code in the application.ex could help here.