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

SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
dokuzbir
Hello, I am trying to convert my lists to string without losing brackets.For start i have 3 map. They look like these buyer = %{ id: ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
Phillipp
Hey, I have a NanoPi-M3 and try to install Elixir on their Ubuntu image. I followed the Raspberry Pi installation instructions from the ...
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
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
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
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
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New

Other popular topics 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
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
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 27727 240
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
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
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
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
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
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

We're in Beta

About us Mission Statement