Giter Club home page Giter Club logo

Comments (2)

wojtekmach avatar wojtekmach commented on June 5, 2024

Hey! the solution is likely to checkout the Auth.Repo connection in all
Bank tests (Bank.Case); I believe we do a similar thing for bank_web
integration tests. I'll investigate it more this evening, thanks for
reporting it!

2016-10-13 13:49 GMT+02:00 Mark Ericksen [email protected]:

Thanks for creating this as an example of doing an Umbrella project. It's
been very helpful as a resource. :)

The problem I have is around transactions and tests when testing a feature
like CustomerRegistration that spans Repos (Bank and Auth).

If you create a new test file called customer_registration_test.exs and
add the following contents you can see the problem I'm having...

defmodule Bank.CustomerRegistrationTest do
use Bank.Case # doctest Bank

alias Bank.CustomerRegistration

@moduletag isolation: :serializable

describe "create/3" do
test "creates auth and user records" do # Ecto.Adapters.SQL.Sandbox.mode(Repo, {:shared, self()})
{:ok, %{update: customer}} = CustomerRegistration.create("name", "[email protected]", "asdfasdf1")
assert customer.auth_account_id != nil
end
endend

If you run mix test at the umbrella root, it fails with this error...

  1. test create/3 creates auth and user records (Bank.CustomerRegistrationTest)
    test/bank/customer_registration_test.exs:10
    ** (DBConnection.OwnershipError) cannot find ownership process for #PID<0.805.0>.
 When using ownership, you must manage connections in one
 of the three ways:

   * By explicitly checking out a connection
   * By explicitly allowing a spawned process
   * By running the pool in shared mode

 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.

 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.
 stacktrace:

[...]

If I run the tests from the Bank application (cd apps/bank) it will pass
the first time and fail the second.

$ mix test
................

Finished in 0.2 seconds
16 tests, 0 failures

Randomized with seed 284417

$ mix test
...............

  1. test create/3 creates auth and user records (Bank.CustomerRegistrationTest)
    test/bank/customer_registration_test.exs:10
    ** (MatchError) no match of right hand side value: {:error, :account, #Ecto.Changeset<action: :insert, changes: %{email: "[email protected]", password: "asdfasdf1", password_hash: "$2b$04$SXRoZna6IjYLIQeo1VdsFeXiadeR2hq3Sd1t5g48v2qyRtJpFekIG"}, errors: [email: {"has already been taken", []}], data: #Auth.Account<>, valid?: false>, %{customer: %Bank.Customer{meta: #Ecto.Schema.Metadata<:loaded, "bank_customers">, auth_account_id: nil, email: "[email protected]", id: 46, inserted_at: #Ecto.DateTime<2016-10-13 11:44:10>, updated_at: #Ecto.DateTime<2016-10-13 11:44:10>, username: "name", wallet: %Bank.Ledger.Account{meta: #Ecto.Schema.Metadata<:loaded, "bank_accounts">, currency: "USD", id: 103, inserted_at: #Ecto.DateTime<2016-10-13 11:44:10>, name: "Wallet: name", type: "liability", updated_at: #Ecto.DateTime<2016-10-13 11:44:10>}, wallet_id: 103}}}
    stacktrace:
    test/bank/customer_registration_test.exs:12: (test)

The problem here is that the created Auth.Account isn't cleaned up after
the first test run and we have left-over data that creates a conflict.

One solution I had was to delete the Auth Repo data in the setup of the
test, but then you'd have to run async: false and it felt messy.

Have you solved any of these problems yet?


You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/wojtekmach/acme_bank/issues/8, or mute the thread
https://github.com/notifications/unsubscribe-auth/AAEpJ68y8AlBfAvQGJ6SOZdslXtvJkYnks5qzhrAgaJpZM4KVy2p
.

Wojtek Mach

from acme_bank.

brainlid avatar brainlid commented on June 5, 2024

You are correct... just updating bank/test/support/case.ex's setup function as follows fixed it.

  setup tags do
    opts = tags |> Map.take([:isolation]) |> Enum.to_list()
    :ok = Ecto.Adapters.SQL.Sandbox.checkout(Bank.Repo, opts)
    :ok = Ecto.Adapters.SQL.Sandbox.checkout(Auth.Repo, opts)

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

    :ok
  end

Thanks!

from acme_bank.

Related Issues (16)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.