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

shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
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
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 record...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
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
Mooodi
Given a string, how can I get access to its character by index? Enum.at("my_string", 2) doesn't work. Or rather, not char, but a substr...
New
wernerlaude
In AR this is so simple @articles = current_user.articles How to do in Ecto? def index(conn, _params) do current_user = conn.assig...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New

Other popular topics Top

yurko
Here are few pieces of (common) Linux knowledge that we use for reasonably small one server apps. We use Ubuntu but this should work for ...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 45766 226
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 49522 488
New
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
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

We're in Beta

About us Mission Statement