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

Hi, I’m working on a simple app following along with Ecto guides and I’m having the following error when I try to run my test:

could not lookup Ecto repo CsvTest.Repo because it was not started or it does not exist

my application.ex:

defmodule CsvTest.Application do
  # See https://hexdocs.pm/elixir/Application.html
  # for more information on OTP Applications
  @moduledoc false

  use Application

  @impl true
  def start(_type, _args) do
    children = [
      CsvTest.Repo
    ]

    # See https://hexdocs.pm/elixir/Supervisor.html
    # for other strategies and supported options
    opts = [strategy: :one_for_one, name: CsvTest.Supervisor]
    Supervisor.start_link(children, opts)
  end
end

config.exs

import Config

config :csv_test, CsvTest.Repo,
  database: "csv_test_repo",
  username: "postgres",
  password: "postgres",
  hostname: "localhost"

config :csv_test, ecto_repos: [CsvTest.Repo]

import_config "#{Mix.env()}.exs"

test.exs

use Mix.Config

config :csv_test, CsvTest.Repo,
  username: "postgres",
  password: "postgres",
  database: "CsvTest_test",
  hostname: "localhost",
  pool: Ecto.Adapters.SQL.Sandbox

mix.exs

defmodule CsvTest.MixProject do
  use Mix.Project

  def project do
    [
      app: :csv_test,
      version: "0.1.0",
      elixir: "~> 1.12",
      start_permanent: Mix.env() == :prod,
      deps: deps(),
      elixirc_paths: elixirc_paths(Mix.env()),
      aliases: aliases()
    ]
  end

  # Run "mix help compile.app" to learn about applications.
  def application do
    [
      extra_applications: [:logger]
    ]
  end

  # Run "mix help deps" to learn about dependencies.
  defp deps do
    [
      # {:dep_from_hexpm, "~> 0.3.0"},
      # {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"}
      {:credo, "~> 1.6.2", only: [:dev, :test], runtime: false},
      {:dialyxir, "~> 1.1.0", only: [:dev, :test], runtime: false},
      {:ecto_sql, "~> 3.0"},
      {:postgrex, ">= 0.0.0"}
    ]
  end

  defp aliases do
    [test: ["ecto.create --quiet", "ecto.migrate", "test"]]
  end

  defp elixirc_paths(:test), do: ["lib", "test/support"]
  defp elixirc_paths(_), do: ["lib"]
end

repo.ex

defmodule CsvTest.Repo do
  use Ecto.Repo,
    otp_app: :csv_test,
    adapter: Ecto.Adapters.Postgres
end

repo_case.ex

defmodule CsvTest.RepoCase do
  use ExUnit.CaseTemplate

  using do
    quote do
      alias CsvTest.Repo

      import Ecto
      import Ecto.Query
      import CsvTest.RepoCase
    end
  end

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

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

    :ok
  end
end

test_helper.exs

ExUnit.start()

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

and finally my test:

defmodule CsvTest.PeopleTest do
  use CsvTest.RepoCase
  alias CsvTest.People

  describe "populate_people_table_from_csv(file)" do
    test "when all entries in csv_files are valid, should populate successfully" do
      assert People.populate_people_table_from_csv("valid_csv.csv") == :ok
    end
  end
end

let me know if I forgot something that might be causing the issue.

1 Like

Hi @moonlunatik welcome to elixir forum.

You are missing entry for CsvTest.Application in application ?

  def application do
    [
      mod: {CsvTest.Application, []}, # <- missing this line
      extra_applications: [:logger]
    ]
  end