zzibert

zzibert

Im doing integration tests in my test database if my logic saves the correct records. Although im running pool: Ecto.Adapters.SQL.Sandbox the records are not flushed after each mix test

Showing Posts 10 to 1

shahryarjb

shahryarjb

What is your approach for umbrella project? Because if I do like this for one or two projects, the first one works but the another projects have many errors. Especially when your Ecto is in a project under umbrella.

kokolegorille

kokolegorille

This is a standard data_case file (I did not edit after project creation)

defmodule Rbax.DataCase do
  @moduledoc """
  This module defines the setup for tests requiring
  access to the application's data layer.

  You may define functions here to be used as helpers in
  your tests.

  Finally, if the test case interacts with the database,
  it cannot be async. For this reason, every test runs
  inside a transaction which is reset at the beginning
  of the test unless the test case is marked as async.
  """

  use ExUnit.CaseTemplate

  using do
    quote do
      alias Rbax.Repo

      import Ecto
      import Ecto.Changeset
      import Ecto.Query
      import Rbax.DataCase
    end
  end

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

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

    :ok
  end

  @doc """
  A helper that transforms changeset errors into a map of messages.

      assert {:error, changeset} = Accounts.create_user(%{password: "short"})
      assert "password is too short" in errors_on(changeset).password
      assert %{password: ["password is too short"]} = errors_on(changeset)

  """
  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

zzibert

zzibert OP

ok, can you show me your data_case.ex file

kokolegorille

kokolegorille

This is normally set in data_case.ex

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

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

    :ok
  end

But it’s nice You find a solution :slight_smile:

zzibert

zzibert OP

Hey, i found the solution, I had to Explicitly get a connection before each test , so i did setup do
# Explicitly get a connection before each test
:ok = Ecto.Adapters.SQL.Sandbox.checkout(DB.Repo) end

Thank you for your help!

kokolegorille

kokolegorille

Also important is this file

test/support/data_case.ex

It would be helpful to know

  • What Phoenix version?
  • Did You edit test config yourself?
  • What happens on a new project?
zzibert

zzibert OP

hey,
If i add the line Ecto.Adapters.SQL.Sandbox.mode(DB.Repo, :manual)
I get this error:
6) Logic.VinWorkerTest: failure on setup_all callback, all tests have been invalidated
** (DBConnection.OwnershipError) cannot find ownership process for pid<0.597.0>.

 When using ownership, you must manage connections in one
 of the four ways:

 * By explicitly checking out a connection
 * By explicitly allowing a spawned process
 * By running the pool in shared mode
 * By using :caller option with allowed process

 The first two options require every new process to explicitly
 check a connection out or be allowed by calling checkout or
 allow respectively.

 The third option requires a {:shared, pid} mode to be set.
 If using shared mode in tests, make sure your tests are not
 async.

 The fourth option requires [caller: pid] to be used when
 checking out a connection from the pool. The caller process
 should already be allowed on a connection.

 If you are reading this error, it means you have not done one
 of the steps above or that the owner process has crashed.

 See Ecto.Adapters.SQL.Sandbox docs for more information.
kokolegorille

kokolegorille

This is mine…

ExUnit.start()
Ecto.Adapters.SQL.Sandbox.mode(Caissa.Repo, :manual)

BTW if I create a new project…

$ mix phx.new koko
$ cat koko/test/test_helper.exs 
ExUnit.start()
Ecto.Adapters.SQL.Sandbox.mode(Koko.Repo, :manual)
zzibert

zzibert OP

Hey there
No its not a legacy project.

test "if compatibility fails, make a record with status: :not_available" do
      VinWorker.request_data_clearance("WBY2Z2205EX7GB012", "O00O010O184TA")
      details = Bmw.get_clearance_by_cid("11111111-1111-1111-1111-111111111111")
      assert "not_available" == details.status
    end
    
    test "if compatibility succeeds, make a record with :pending" do
      VinWorker.request_data_clearance("WBAVB71470VOTP000", "S00I000M001OK")
      details = Bmw.get_clearance_by_cid("11111111-1111-1111-1111-111111111112")
      assert "pending" == details.status
    end

These two records persist in the test database.

My test helper file is just ExUnit.start(exclude: [:pending])

kokolegorille

kokolegorille

Hello, You don’t give a lot of details, so it’s hard to give You an answer…

What do You have in the file test/test_helper.exs?

Is that some legacy project?

— 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
water
I’m aware of 2 such projects, are there others? git: GitHub - team-alembic/realworld: A fullstack Phoenix LiveView application with back...
New
itzmidinesh
Hi all, I’m working on a Phoenix project where I need to make external HTTP requests and I’m trying to choose between Req and HTTPoison....
New
peck
Hi everyone. My team and I have been working on a fairly modest app based around video streaming and chat, but we’ve landed a customer t...
New
leoschweizer
Hi, we run different LLM models across a cluster, with each node loading a configurable subset. Model-specific queues let nodes consume ...
New
csmr
Hi all, I’m new here and would like to hear peoples views on my Elixir/Phoenix based game-engine! :smiley: Mutonex gamedesign was finish...
New
perzanko
Do we have maintainer of absinthe-graphql/dataloader here? We have a PR that we’d like to merge to the library. Relatively small additive...
New

Other Trending Topics Top

dominicletz
Hey there, I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first. ...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New
OndrejValenta
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly) AI dream has many faces, but general...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews