Chapter 5 Testing Ecto Queries

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

The Pragmatic Programmers
The Pragmatic Programmers

--

👈 Wrapping Up | TOC | Creating a Factory to Help with Setup 👉

Testing Ecto queries is fairly straightforward, especially if you’re coming from testing database code in other languages. Some basic rules will help you make sure that your coverage is effective. In this chapter, we’ll explore some additional tooling that can help make long-term ownership of your test suite easier, and then we’ll write tests for Ecto queries. We’ll build on concepts from the previous chapter, Chapter 4, Testing Ecto Schemas. While it isn’t strictly necessary to have read that chapter, without it you may miss some references to code or concepts we’ve already covered. We’ve provided complete examples of the previous chapter’s code with the book, so when needed, you can use that for reference.

For this chapter, we’re going to work with a schema called testing_ecto/lib/schemas/user.ex, which is provided in the copy of testing_ecto. It’s an evolution of the schema we ended the last chapter with, with two notable changes: now that we’re done changing the schema, we’re calling it User and it has the changeset functions, create_changeset/1 and update_changeset/2, instead of a single, multi-use function. We’ve also included an updated test file, testing_ecto/test/schemas/user_test.exs. Copy both of those files into your local testing_ecto project. The test file assumes that you have a copy of testing_ecto/test/schema_case.ex as…

--

--

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.