Mix test does not insert data into table?

I wrote a test 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 the test outputs:

.....

Finished in 0.1 seconds
5 tests, 0 failures

Randomized with seed 820825

But there is no data in my games table.

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

Doesn’t the test insert data into the table?

How did you check the test datababse? All changes are isolated since each test is wrapped with transaction

See also

2 Likes

In test DB the inserted data will be cleared after all tests ran.

1 Like

Thanks.

Not all, each. So there will be no flaky tests that relies on other tests.

1 Like

Also, you should take in account that your tests probably operate on another database. You can check it out on your config/test.exs file.

2 Likes

In dev.exs: db_server_dev
In test.exs: db_server_test

Thanks for the advice.