fuelen / ecto_dev_logger

An alternative logger for Ecto queries
Apache License 2.0
147 stars 14 forks source link

Running in Test Mode? #23

Closed lauraannwilliams closed 10 months ago

lauraannwilliams commented 11 months ago

This is so useful, thank you for writing it.

Is there a way to run this when using mix test? It'd be so helpful to get this kind of display on a failing test, but I'm getting errors when i try


    ** (EXIT) no process: the process is not alive or there's no process currently associated with the given name, 
possibly because its application isn't started
    (stdlib 5.0.1) gen_server.erl:386: :gen_server.call/2
    lib/myapp/application.ex:7: (module`)`` 

I tried adding `Application.ensure_all_started(:telemetry)` to my test_helper but that doesn't change anything. 

Even if this can't be done, it's still an awesome library :)
fuelen commented 11 months ago

Hi Laura

Could you provide the code from lib/myapp/application.ex:7?

fuelen commented 10 months ago

@lauraannwilliams is the issue still relevant?

lauraannwilliams commented 10 months ago

My apologies! I thought I'd posted.

Here's my application.ex

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

  use Application
  Ecto.DevLogger.install(MyApp.Repo)

  @impl true
  def start(_type, _args) do
    children = [
      # Start the Telemetry supervisor
      MyAppWeb.Telemetry,
      # Start the Ecto repository
      MyApp.Repo,
      # Start the PubSub system
      {Phoenix.PubSub, name: MyApp.PubSub},
      # Start Finch
      {Finch, name: MyApp.Finch},
      # Start the Endpoint (http/https)
      MyAppWeb.Endpoint
      # Start a worker by calling: MyApp.Worker.start_link(arg)
      # {MyApp.Worker, arg}
    ]

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

  # Tell Phoenix to update the endpoint configuration
  # whenever the application is updated.
  @impl true
  def config_change(changed, _new, removed) do
    MyAppWeb.Endpoint.config_change(changed, removed)
    :ok
  end
end

my test.exe

import Config

# Configure your database
#
# The MIX_TEST_PARTITION environment variable can be used
# to provide built-in test partitioning in CI environment.
# Run `mix help test` for more information.
config :myapp, MyApp.Repo,
  username: System.get_env("POSTGRES_TEST_USERNAME") || "postgres",
  password: System.get_env("POSTGRES_TEST_PASSWORD") || "postgres",
  hostname: System.get_env("POSTGRES_TEST_HOST") || "postgres",
  database: "myapp_test",
  timeout: 180_000,
  connect_timeout: 180_000,
  pool: Ecto.Adapters.SQL.Sandbox,
  pool_size: 10

if config_env() == :test do
  config :myapp, MyApp.Repo, log: false
end

# We don't run a server during test. If one is required,
# you can enable the server option below.
config :myapp, MyAppWeb.Endpoint,
  http: [ip: {127, 0, 0, 1}, port: 4002],
  secret_key_base: "keybase",
  server: false

# In test we don't send emails.
config :myapp, MyApp.Mailer, adapter: Swoosh.Adapters.Test

# Disable swoosh api client as it is only required for production adapters.
config :swoosh, :api_client, false

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

# Initialize plugs at runtime for faster test compilation
config :phoenix, :plug_init_mode, :runtime

and my test_helper.ex

ExUnit.configure(exclude: [pending: true])

Application.ensure_all_started(:telemetry)

Faker.start()

ExUnit.start()

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

And finally the error message when i run mix.test:

== Compilation error in file lib/myapp/application.ex ==
** (exit) exited in: :gen_server.call(:telemetry_handler_table, {:insert, [:ecto_dev_logger, :myapp, :repo], [[:myapp, :repo, :query]], &Ecto.DevLogger.telemetry_handler/4, []})
    ** (EXIT) no process: the process is not alive or there's no process currently associated with the given name, possibly because its application isn't started
    (stdlib 5.0.1) gen_server.erl:386: :gen_server.call/2
    lib/myapp/application.ex:7: (module)

finally..

elixir --version
Erlang/OTP 26 [erts-14.0.1] [source] [64-bit] [smp:16:16] [ds:16:16:10] [async-threads:1] [jit:ns]

Elixir 1.15.4 (compiled with Erlang/OTP 26)

Thank you very much for your time

fuelen commented 10 months ago
  Ecto.DevLogger.install(MyApp.Repo)

must be called inside def start(...) do block

lauraannwilliams commented 10 months ago

thanks, that should have been very obvious