bottlenecked

bottlenecked

Do you test many_to_many Ecto assocs?

Hi all, I’m thinking about creating a many-to-many association on one of my schemas like this

    many_to_many :active_foos, MyApp.Foo,
      join_through: MyApp.FooJoinTable,
      join_where: [state: :current],
      where: [state: :active],
      preload_order: [asc: :name]

The above reads like configuration - but it feels complex enough that it might merit writing a couple of sanity tests. However there’s also advice that when it comes to tests, we should be testing our own code, not the library/framework code - and it would seem that a test like this

test "correctly loads active_foos" do
   ...
end

would be testing that many_to_many works as advertised as opposed to testing that some business logic is correct.

What have others done here? Did/would you write tests like that? Or would you rather indirectly test at the call site instead (e.g. in a test for the page that renders the active_foos).

Thanks for your thoughts!

Marked As Solved

dimitarvp

dimitarvp

I would be the first to tell you to not test Ecto itself, though in your case I believe it’s fair to test if those m2m records are inserted properly if f.ex. you also have metadata fields inside them, want to check if the preload order is indeed respected etc.

There’s a fine line between unfounded paranoia and simply making sure your stuff works. I’d say in this case it’s a bit of both but I’d still add tests.

Also Liked

LostKobrakai

LostKobrakai

I’d say you don’t want to test the “definition”, but you want to test the code making use of the definition according to how that powers actual usecases of your application. Tests should not break if you switch to e.g. has_many/through, if there’s no user facing behaviour changes. But things should break if you mess something up in that definition, which would result in user facing behaviour changes.

bottlenecked

bottlenecked

Thank you both for your thoughtful input.

In the end I decided to to test both that the relation returns the expected records in the order expected while leaving out unwanted ones, and also at the call site to make sure that it’s correctly used.

Where Next?

Popular in Questions Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
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
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
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
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
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

Other popular topics Top

danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 29377 241
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
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
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
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
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
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 47930 226
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

Latest on Elixir Forum

We're in Beta

About us Mission Statement