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 1 to 10

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?

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

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

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

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

ok, can you show me your data_case.ex file

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

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.

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
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
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
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

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews