RoboZoom

RoboZoom

I am trying to configure a Phoenix Ecto app for testing using the example from the docs.

Problem: Each time I run mix test, even when localized to a test that does not require DB access, I receive an error:

16:47:43.083 [error] Postgrex.Protocol (#PID<0.312.0>) failed to connect: ** (DBConnection.ConnectionError) tcp connect (localhost:5432): connection refused - :econnrefused
** (Mix) The database for MyApp.Repo couldn't be created: connection not available and request was dropped from queue after 2000ms. This means requests are coming in and your connection pool cannot serve them fast enough. You can address this by:

Following this error, it appears as if no unit tests are run.

What I’ve done: In my config/test.exs file, I have the following:

config :my_app, MyApp.Repo,
  username: "postgres",
  password: "password",
  hostname: "localhost",
  database: "daxta_server_test#{System.get_env("MIX_TEST_PARTITION")}",
  pool: Ecto.Adapters.SQL.Sandbox,
  pool_size: 10

In my mix.exs file I have:

use Mix.Project

  def project do
    [
      app: :daxta_server,
      version: "0.1.0",
      elixir: "~> 1.14",
      elixirc_paths: elixirc_paths(Mix.env()),
      start_permanent: Mix.env() == :prod,
      aliases: aliases(),
      deps: deps()
    ]
  end
  def application do
    [
      mod: {MyApp.Application, []},
      extra_applications: [:logger, :runtime_tools]
    ]
  end

  # Specifies which paths to compile per environment.
  defp elixirc_paths(:test), do: ["lib", "test/support"]
  defp elixirc_paths(_), do: ["lib"]

Within test/support I have a file repo_case.ex where I checkout the SQL sandbox, but for the test I’m running I don’t call this, as I am not testing anything Ecto related.

I received a suggestion to add the --no_start tag to the mix test command, but that sounds hacky. I’m sure that there’s a better way to set this up, and I’m just missing some fundamental configuration setting.

Any help is appreciated!

Showing Posts 1 to 5

al2o3cr

al2o3cr

The default mix.exs created by the generators has this task alias:

test: ["ecto.create --quiet", "ecto.migrate --quiet", "test"]

The message you’re getting suggests that the ecto.create step is failing before any tests even get a chance to run.

jarlah

jarlah

Or, if you need database and dont want to mess around with scripts or commands, you can use Testcontainers for Elixir. Until you start adding tests with db access this will just work in the background. Its a bit rough around the edges still, but its getting there. I’m currently working on it and any suggestions are appreciated.

RoboZoom

RoboZoom OP

This is indeed what the generator auto-created for me.

Which leads me to question - what is the purpose of ecto.create or ecto.migrate in the test context? Ecto’s own instructions indicate that you can mock the database via the Sandbox.

It makes sense that the migrate command requires a connection - is it normal to test without some sort of test database setup?

al2o3cr

al2o3cr

The Ecto sandbox doesn’t mock the database, it wraps each test in a transaction so that they don’t interfere with each other. You still need a database underneath.

jarlah

jarlah

that’s also why I suggest using testcontainers when making Phoenix apps. It’s less of a chore to add a couple of lines at the top of the start function in application.ex, than playing with scripts or manual docker commands, and you don’t even need to have docker installed, because Testcontainers for Desktop now has embedded runtime. The test container also shuts down if tests are completed naturally, due to use of System.at_exit. If tests are aborted, it’s cleaned up by a reaper process in docker (started by testcontainers). Ill shut up now :wink:

— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
Onor.io
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
jaybe78
Hello, I’m developing a online persistent chat system (what’s app) like using elixir/dynamodb/aws for a mobile app(flutter). The diffic...
New
Trolleger
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New

Other Trending Topics Top

garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New
aseigo
ICal is a library for interacting with iCalendar data. It parses iCalendars into typed Elixir structs via ICal.from_ics, and can prepare ...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews