curioustolearn

curioustolearn

Are changes to database within a test automatically reverted when the test is completed?

Hi,

I want some help in understanding where the following magic is coming from. Please see the code below. In the second test below, the line Gamestatus.change_newplayersallowed(false, "game-1234") correctly changes the value of newplayersallowed to false. I know this because, one, the tests passes and, two, the output of IO.inspect is false. However, after the test is completed, when I check the value of newplayersallowed using psql, the value of newplayersallowed is true. So it appears that the change made by function Gamestatus.change_newplayersallowed(false, "game-1234") is reverted to the value before that change. How does Elixir/Phoenix know which changes to revert? Where can I read about which changes to the database within tests will be reverted?

Thank you.

defmodule Games.GamestatusTest do
  use GamesWeb.ConnCase
  # use ExUnit.Case

  alias Games.Gamestatus


  test "Returns the correct newplayerallowed status" do
    assert Gamestatus.newplayersallowed?("game-1234") == true
  end

  test "Updates the newplayersallowed status" do
    Gamestatus.change_newplayersallowed(false, "game-1234")
    {status, _msg} = Gamestatus.newplayersallowed?("game-1234")
    IO.inspect(status, label: "newplayersallowed")
    assert status == false
  end


end ## end module

The following is the code for the function Gamestatus.change_newplayersallowed.

  @doc"""
  Change newplayersallowed to `set_to_val` for the given gameid
  """
  def change_newplayersallowed(set_to_val, gameid) do
    sql = "UPDATE gamesstatus SET newplayersallowed = $1" <>
            " WHERE gameid = $2;"

    Ecto.Adapters.SQL.query(
      Games.Repo,
      sql,
      [set_to_val, gameid]
    )

  end

Marked As Solved

sbuttgereit

sbuttgereit

The short answer is: it depends.

It’s not uncommon to use the Ecto.Adapters.SQL.Sandbox adapter (Ecto.Adapters.SQL.Sandbox — Ecto SQL v3.14.0) for testing, part of what it does is wrap the tests in a big database transaction so it can be rolled back when done. Also, the internal transactional management of the database calls will play a factor, too… if they don’t commit, they’ll not be visible outside the transaction or be rolledback, etc.

I can tell you that without doing anything special such as using the Sandbox adapter that the database access in tests are just normal database accesses and will persist. In my testing setup, especially around integration tests, I expect database changes to be persistent and I have no trouble with getting that.

I’d probably need to see more about the configuration or whatnot to know why it might be going south for you.

Also Liked

al2o3cr

al2o3cr

The machinery that makes this work is the Ecto.Adapters.SQL.Sandbox

The short short version: there’s a SQL BEGIN before every test, and a ROLLBACK afterwards.

al2o3cr

al2o3cr

Possibly - you can check by looking for code similar to this setup block from the documentation:

  setup do
    # Explicitly get a connection before each test
    :ok = Ecto.Adapters.SQL.Sandbox.checkout(Repo)
  end

checkout is what actually opens the transaction.

Where Next?

Popular in Questions Top

sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
JulienCorb
I am trying to implement my new.html.eex file to create new posts on my website. new.html.eex: &lt;h1&gt;Create Post&lt;/h1&gt; &lt;%= ...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

Other popular topics Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39297 209
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36128 110
New
boundedvariable
I am going through the kafka architecture. All the features what the kafka is providing are already in Erlang. I would like hear your opi...
New

We're in Beta

About us Mission Statement