Chapter 4 Testing Ecto Schemas

Testing Elixir — by Andrea Leopardi, Jeffrey Matthias (37 / 80)

The Pragmatic Programmers
The Pragmatic Programmers

--

👈 Wrapping Up | TOC | Testing Your Schema Through Changesets 👉

As Elixir matures, the community is settling on certain libraries as the libraries for certain use cases. Both because of the involvement of José Valim and due to its inclusion in Phoenix, the de facto Elixir web framework, Ecto has found itself positioned as the relational database library for Elixir. Due to Ecto’s composition of a few discreet parts, Ecto.Schema has found a second purpose, that of a casting and validation library separate from database-related needs. This leaves us with the two most common use cases of Ecto: validation and database interactions. Both uses have their own sets of tests, and we’ll cover both in this chapter.

The common saying “Don’t test your framework” often leads people to undertest their database code. Before we begin testing our Ecto code, let’s examine this saying and identify what practical implications it brings. Ecto is one of the most used libraries in the Elixir community and has a great team behind it who are putting out very solid work. The library “just works.” If there are bugs, they’re discovered very quickly. When we write our tests, we should not test the actual functionality of the library, but we should test that our code uses that library’s functionality correctly.

This can be confusing, but we can provide you a heuristic that’ll help you know whether or not to…

--

--

The Pragmatic Programmers
The Pragmatic Programmers

We create timely, practical books and learning resources on classic and cutting-edge topics to help you practice your craft and accelerate your career.