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.

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.

1 Like