halostatue

halostatue

Ecto: Unit Test for Validating Association Declarations

I’m sharing this because it may be of general interest. I was doing some refactoring of code and removed some automatic aliases when doing use MyApp.Schema, which broke some of my unit tests…but not all of them (I don’t have 100% coverage; imagine that).

The error raised was UndefinedFunctionError for __schema__. After some thinking, this is the unit test I came up with to verify this. The only thing to maintain in it is the list of @relations, which are the application Ecto schemas that have belongs_to, has_many, or many_to_many relationships listed in them.

This will ensure that the relationships are not referring to the wrong aliased module, since Ecto does not (cannot?) verify the module specifications a compile-time. Example:

defmodule MyApp.Business do
  use MyApp.Schema

  schema "businesses" do
    has_many :users, User
  end
end

The error is fairly visible, but unless you have a test that exercises the association (via join(Business, :left, [b], assoc(b, :user)), there will be nothing that exposes the missing alias MyApp.User from the module during compilation.

Adding a test like this will help:

defmodule MyApp.ValidEctoRelationsTest do
  @moduledoc """
  Test that belongs_to, has_many, and many_to_many relationships are valid,
  since these things cannot be determined at compile time.
  """

  use ExUnit.Case

  alias MyApp.Repo

  import Ecto.Query

  setup do
    :ok = Ecto.Adapters.SQL.Sandbox.checkout(Repo)
  end

  @relations [
    MyApp.Business,
    MyApp.User
  ]

  for schema <- @relations, association <- schema.__schema__(:associations) do
    test "#{inspect(schema)} has a valid association for #{inspect(association)}" do
      assert_valid_relationship(unquote(schema), unquote(association))
    end
  end

  defp assert_valid_relationship(schema, association) do
    schema
    |> join(:left, [s], assoc(s, ^association))
    |> where(false)
    |> Repo.all()

    assert true
  rescue
    UndefinedFunctionError ->
      %{queryable: module} = schema.__schema__(:association, association)

      flunk("""
      Schema #{inspect(schema)} association #{inspect(association)} is invalid.
      The associated module #{inspect(module)} does not appear to be an Ecto
      schema. Is #{inspect(schema)} missing an alias?
      """)
  end
end

Where Next?

Popular in Discussions Top

vans163
So useless benchmarks aside, Its possible to write a webserver that can serve 300k requests per second (perhaps more with optimizations)....
New
owaisqayum
I have a sample string sentence = "Hello, world ... 123 *** ^%&amp;*())^% %%:&gt;" From this string, I want to only keep the integers, ...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39297 209
New
PragTob
Hey everyone, this has been brewing in my head some time and it came up again while reading Adopting Elixir. GenServers, supervisors et...
New
jer
I’ve been using umbrellas for a while, and generally started off (on greenfield projects at least) by isolating subapps based on clearly ...
New
PragTob
Hey everyone, this has been on my mind for some time and I’d love your input on it! TLDR: I feel like maps are superioer for storing and...
New
jesse
Hi everyone, I hesitated to post this here because I don’t want you to think I’m spamming, but I’ve been working on a Platform-as-a-Serv...
New
scouten
I’m looking for a host for the server part of a small (personal) side project that I’m working on. It’s currently written in Node.js and ...
New
griffinbyatt
Sobelow Sobelow is a security-focused static analysis tool for the Phoenix framework. For security researchers, it is a useful tool for g...
New
Markusxmr
Since Drab has been developed for a while in the open, introducing the Liveview functionality in a way it happend appears to undermine th...
New

Other popular topics Top

malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 41539 114
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
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
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
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
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
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