pinksynth
Whenever tests have to interact with an Ecto.Repo, sometimes it’s necessary to test a few different branches or paths that data can take. This can be tedious with a database and may result in “resetting” records so they may be re-tested. Here’s a crude example of “resetting” some data to continue with a test:
test "vaccinate_dog/1 vaccinates dog and creates vax record with the corresponding name" do
dog = dog_fixture()
# Perform assertions about vaccinations
assert :ok = vaccinate_dog(dog, "bordetella")
assert %{id: dog_id, vaccinated: true} = dog = Repo.one!(Dog)
assert %{dog_id: ^dog_id, type: "bordetella"} = Repo.one!(Vax)
# Reset state for the dog
Repo.delete_all(Vax)
dog |> Ecto.Changeset.change(%{vaccinated: false}) |> Repo.update!()
# Performmore assertions about vaccinations
assert :ok = vaccinate_dog(dog, "rabies")
assert %{id: dog_id, vaccinated: true} = dog = Repo.one!(Dog)
assert %{dog_id: ^dog_id, type: "rabies"} = Repo.one!(Vax)
end
By using Repo.transaction/2, it’s easy to simply roll back a transaction rather than “resetting” anything (assuming that side-effects are not a concern for the purposes of the test.
test "vaccinate_dog/1 vaccinates dog and creates vax record with the corresponding name" do
dog = dog_fixture()
for vaccine <- ["rabies", "bordetella"] do
in_transaction(fn ->
assert :ok = vaccinate_dog(dog, vaccine)
assert %{id: dog_id, vaccinated: true} = dog = Repo.one!(Dog)
assert %{dog_id: ^dog_id, type: vaccine} = Repo.one!(Vax)
end)
end
defp in_transaction(fun) when is_function(fun, 0) do
Repo.transaction(fn ->
fun.()
Repo.rollback(:ok)
end)
end
end
I know that it’s generally best to decouple the DB and the business logic as much as possible, but I have found this to be a useful way to succinctly cover more ground within Repo-related tests.
Trending in Guides/Tuts
I’ve spent some time understanding how to do hot code reloading with releases built using mix release, and here I’d like to detail the st...
New
# ~/src/livebook/.iex.exs
System.cmd("xdg-open", [ LivebookWeb.Endpoint.access_url() ] )
Because Livebook requires a unique passcode on ...
New
Other Trending Topics
Edit: 2026 May 15 - This post is archived.
Mob is alive!!
Main docs: mob v0.7.11 — Documentation
A bit of explanation for the slightly c...
New
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
A little off-topic, but I feel like people here have a good head on their shoulders.
I used to be quite good at making software. Was luc...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #ai
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #metaprogramming
- #hex










Showing Posts 1 to 2- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
al2o3cr
This is usually where I’d stop this test and write another one, TBH. Unless
dog_fixtureis very expensive to compute, I’d prefer to see duplication if testing with"rabies"vs"bordetella"exposes different behavior.Also note there’s no support AFAIK for nested transactions, so this won’t work as expected if used with the sandbox-enabled test harness that’s generated by Phoenix.
pinksynth
Yeah, admittedly this is not the best example. My actual use case is a module which does some pretty complex query composition using “filters” that can be manipulated recursively by the end user and is generally in the “hard to test” world of high-CC code. I don’t recall the specifics of how our
Repois set up in tests but we use transactions like this quite a bit and they all seem to work fine