简体   繁体   中英

New users being created in test environment have their IDs continually incrementing using Wallaby/Phoenix

I'm new to Elixir/Phoenix so I'm not sure if this is working as expected or an issue I should address.

I have a Phoenix app that I just started adding integration tests to. Having done this for a few days of setting up and testing the User registration feature, I'm now onto testing the login.

I'm using Wallaby:

test.exs:

config :happy_app, :sql_sandbox, true
config :wallaby,
  driver: Wallaby.Experimental.Chrome,
  chrome: [headless: true],
  screenshot_on_failure: true

test_helper.exs:

ExUnit.start()
Ecto.Adapters.SQL.Sandbox.mode(HappyApp.Repo, :manual)
{:ok, _} = Application.ensure_all_started(:wallaby)
Application.put_env(:wallaby, :base_url, HappyAppWeb.Endpoint.url())

My test looks like this:

defmodule HappyAppWeb.UserLoginTest do
  use HappyAppWeb.IntegrationCase, async: true

  alias HappyApp.Accounts
  import HappyAppWeb.IntegrationSteps

  @tag integration: true
  test "user can login", %{session: session} do
    email = "test-user@example.com"
    password = "12345678"

    # seed user into db
    user = Accounts.create_user(%{email: email, password: password})
    IO.inspect user

    # logging in with the user's email and password

    # asserting the view is correct
  end
end

When I inspect the user: IO.inspect user I get:

IO.inspect user


=>
{:ok,
 %HappyApp.Accounts.User{
   __meta__: #Ecto.Schema.Metadata<:loaded, "users">,
   email: "test-user@example.com",
   id: 449,
   inserted_at: ~N[2019-11-24 13:20:33],
   password: nil,
   password_hash: "shhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh",
   updated_at: ~N[2019-11-24 13:20:33]
 }}

Notice the id: 449, . Is that correct? Shouldn't that reset between tests back to 1?

Manually looking into postgres I see that happy_app_test db is indeed empty. Is the data elsewhere?

Take a look on Phoenix Testing Document . The reason happy_app_test db is empty:

The default test helper file, test/test_helper.exs, creates and migrates our test database for us. It also starts a transaction for each test to run in. This will "clean" the database by rolling back the transaction as each test completes.

And Why the id doesn't start from 1 is the database keep AUTO_INCREMENT the id unless you reset the database or specify postgres database .

Update: check out mix.exs file

  defp aliases do
    [
      "ecto.setup": ["ecto.create", "ecto.migrate", "run priv/repo/seeds.exs"],
      "ecto.reset": ["ecto.drop", "ecto.setup"],
      test: ["ecto.create --quiet", "ecto.migrate", "test"]
    ]
  end

When you run mix test , it actually calls 3 commands: "ecto.create --quiet", "ecto.migrate", "test" . This why database is clean after each test.

This thing is related to the database engine. I cannot find information that confirms it for sure, however I suspect that rollback is deleting the rows that were previously inserted.

When you delete rows, the id counter is not reset, it continues to increment. Basically you need to delete/update the table to reset the counter, or you can reset it manually if you really need to.

If you restart the tests, you will notice that the first test that inserts the record will start from 1 and others will continue the counter, since before the tests you run migration and create a new table, with a new serial counter.

If you are interested to find more about auto increment of the id you can read here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM