Search code examples
postgresqlelixirphoenix-frameworkecto

"Could not lookup Ecto repo" error during tests


I've been getting the following exception when trying to setup a new ecto repo. Currently only using it for testing (since i am just setting this up).

** (RuntimeError) could not lookup Ecto repo Lipwig.AnnotatedUnit.Repo because it was not started or it does not exist

This happens when my tests use Lipwig.AnnotatedUnit.DataCase. However, when i use Lipwig.DataCase (which references the Lipwig.Repo`), the tests run fine.

I have the following setup in the project.

defmodule Lipwig.Repo do
  use Ecto.Repo,
    otp_app: :lipwig,
    adapter: Ecto.Adapters.Postgres
end

defmodule Lipwig.AnnotatedUnit.Repo do
  use Ecto.Repo,
    otp_app: :lipwig,
    adapter: Ecto.Adapters.Postgres
end

This here is my test setup

defmodule Lipwig.DataCase do
  use ExUnit.CaseTemplate

  using do
    quote do
      alias Lipwig.Repo

      import Ecto
      import Ecto.Changeset
      import Ecto.Query
      import Lipwig.DataCase
    end
  end

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

    unless tags[:async] do
      Ecto.Adapters.SQL.Sandbox.mode(Lipwig.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

defmodule Lipwig.AnnotatedUnit.DataCase do
  use ExUnit.CaseTemplate

  using do
    quote do
      alias Lipwig.AnnotatedUnit.Repo

      import Ecto
      import Ecto.Changeset
      import Ecto.Query
      import Lipwig.AnnotatedUnit.DataCase
    end
  end

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

    unless tags[:async] do
      Ecto.Adapters.SQL.Sandbox.mode(Lipwig.AnnotatedUnit.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 other datacase for Lipwig.Repo is almost identical, just referencing the other repo.

My config.exs config defines

config :lipwig,
  ecto_repos: [Lipwig.Repo, Lipwig.AnnotatedUnit.Repo]

My test.exs config is

config :lipwig, Lipwig.Repo,
  username: "postgres",
  password: "postgres",
  database: "lipwig_test",
  hostname: "localhost",
  pool: Ecto.Adapters.SQL.Sandbox

config :lipwig, Lipwig.AnnotatedUnit.Repo,
  username: "postgres",
  password: "postgres",
  database: "lipwig_test",
  hostname: "localhost",
  pool: Ecto.Adapters.SQL.Sandbox

Solution

  • As per my knowledge, the configurations seem fine. As the error suggests, I think the issue might be because you are not starting the Lipwig.AnnotatedUnit.Repo in with you application.

    Look for the application.ex file in the /lib/lipwig_web folder and make sure that Lipwig.AnnotatedUnit.Repo is added in the children array.

    Hope this resolves 🥂