Search code examples
elixirphoenix-frameworkecto

mix test does not insert data?


I wrote a test code for insertion schema.

defmodule DbServerWeb.GameSchemaTest do
  use ExUnit.Case, async: true
  use DbServer.DataCase

  alias DbServer.Games
  alias DbServer.Schema.Game

  describe "insertion" do
    @valid_params %{game_name: "test_name"}
    @invalid_params %{game_name: nil}

    test "insertion with valid data" do
      assert {:ok, %Game{} = game} = Games.create_game(@valid_params)
      assert game.game_name == "test_name"
    end

    test "insertion with invalid data" do
      assert {:error, %Ecto.Changeset{}} = Games.create_game(@invalid_params)
    end
  end
end

then terminal outputs:

.....

Finished in 0.1 seconds
5 tests, 0 failures

Randomized with seed 820825

But there is nothing in my games table.

 id | game_name | inserted_at | updated_at 
----+-----------+-------------+------------
(0 rows)

Doesn't the test code insert data through schema?


Solution

  • Your repo is set to Sandbox mode. This means that after transaction ends (wrapped unit test) it will rollback transaction for you. Often this configuration is in test.exs file but also can be set in test/test_helper.exs