Testing an Ecto Schema for Database Interactions

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

The Pragmatic Programmers
The Pragmatic Programmers

--

👈 Testing an Ecto Schema as a Data Validator | TOC | Testing Your Schema Through Database Calls 👉

The most common use case for Ecto schemas is to enable easy interaction with a database. While we keep our schemas to a minimum of logic — just the schema definition and changeset function or functions — we still need to make some changes in order to use our schema with a database. We’ll update the basic schema we wrote (skipping the changes we made for using it as a validator) and then update our tests. Once we’ve covered the schema aspects, we’ll move on to cover testing queries that use the schema.

Adding a Database into Our Application

Before we can make the necessary changes, we need to make sure that we have a local running database. Given that every reader will have a different local setup, we’re going to leverage Docker to reduce the chance issues due to local environment differences. We have to make the assumption that you already have or are able to set up Docker on your computer.[31] We’ve provided, with the testing_ecto application code, two files that you should copy into your application directory: testing_ecto/docker-compose.yml and testing_ecto/Makefile. Keep them at the root level of your local testing_ecto application.

--

--

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.