hurty

hurty

Inserting fixtures in a Phoenix app with contexts

Hi! I’m building a Fixture module for testing in a Phoenix 1.3 app with contexts.

How do you generally handle fixture insertion in this case? :

  • Through contexts functions as much as possible, as they’re probably already there (Accounts.create_user, Accounts.create_team, etc)?
  • Directly with schemas changesets and bang Repo functions (Repo.insert!, etc)?
  • Without changesets / pure SQL?

I like having fixture insertion to raise if something goes wrong (with Repo.insert! with the bang).
If you prefer inserting fixture through context functions, do you duplicate Accounts.create_team and create a bang version Accounts.create_team!?
There could be a lot of duplicated code just to have the bang version.

Small example:

defmodule MyApp.Accounts do
  def create_organization(attrs \\ %{}) do
    %Organization{}
    |> Organization.changeset(attrs)
    |> Repo.insert()
  end

  # Bang version, duplicated code
  def create_organization!(attrs \\ %{}) do
    %Organization{}
    |> Organization.changeset(attrs)
    |> Repo.insert!()
  end
end
defmodule MyApp.Fixture do
  @organization %{
    name: "Pied Piper"
  }

  def insert_organization(attrs \\ %{}) do
    Map.merge(@organization, attrs)
    |> MyApp.Accounts.create_organization!() # bang version used here
  end
end

Marked As Solved

mbuhot

mbuhot

Context functions usually contain business logic, so I don’t use them for text fixtures.

There’s a simple fixture pattern described in chapter 7 of whats new in Ecto 2

Also Liked

dli

dli

Plataformatec’s website is down these days. Fortunately, I found a version of “What’s new in Ecto 2.1” as PDF uploaded somewhere.

https://github.com/dwyl/learn-elixir/files/1285008/whats-new-in-ecto-2-1.pdf

dli

dli

Right! Found the section on factories: Test factories — Ecto v3.14.0

Where Next?

Popular in Questions Top

Kurisu
For example for a current url like http://localhost:4000/cosmetic/products?_utf8=✓&query=perfume&page=2, I would like to get: ...
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
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
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
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
earth10
Hi, I’m just starting to build a side-project with Elixir and Phoenix and doing some basic test with Elixir alone. What strikes me is th...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
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
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New

Other popular topics Top

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
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
Nvim
Anybody knows a comprehensive comparison of Django and Phoenix, thanks for the help. Where are they similar? Where do they differ the m...
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
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 53690 245
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement