wolf4earth

wolf4earth

How to fail tests when accidentially inserting into a non-sandboxed Repo?

To make a long story short: it keeps happening that I sometimes forget to put an Ecto.Repo into sandbox mode. And this then leads to issues with OTHER tests when the inserted data collides with the entries inserted for this test. Afterwards I have to go for a hunt for the offending test.

What I’d like to do is put my Repo into a “fail any write unless it’s sandboxed”-mode. This way any “rogue” test would immediately break when I try to insert something after forgetting to sandbox.

I’ve looked at

  • Postgres and whether or not I can enforce any writes to happen inside of transactions (not even sure if that’d do the trick)
  • Ecto.Repo callbacks and whether there is one we can use to check if the connection is in sandbox mode

So far I’ve found nothing that fits the bill. How would I go about this?

Note: Please refrain from telling me I should define a DataCase and use that for my tests. This is all the case. But not all tests require DB access so some use “straight-forward” ExUnit.Cases, and occasionally a mix up happens. We’re all human. And I’d like the machine to tell me when I screw up.

Marked As Solved

LostKobrakai

LostKobrakai

The “sandbox” is actually always running. It’s defined globally to be your repos pool implementation. It does have multiple modes though, where the default :auto means you can just write to the db as normal. this is generally the default as it allows e.g. your app to boot up as normal. You could look into changing the mode you run in by default (e.g. in test_helpers.exs) to be more conservative.

Where Next?

Popular in Questions Top

nobody
How to bind a phoenix app to a specific ip address? could not find anything about that, nowhere, unfortunately, but for me this is quite...
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> somethi...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call t...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
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

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
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement