...

/

Testing Schema Integrations with Database

Testing Schema Integrations with Database

Learn how to test schema through database calls.

While we have a decent basic schema defined, we haven’t covered a subset of the kinds of logic that we’ll find in changeset functions (code that requires interaction with the database to verify that it works).

The validations we’ve tested so far have laid out a pattern for us that can be used with other validations beyond cast/3 and validate_required/2. Functions your code can call that can be tested in similar ways include (but are not limited to) validate_inclusion/4, validate_length/3, and validate_subset/4.

A very common case where things are easier to test with a database is when there is a unique constraint on a column in our database.

Testing schema through database calls

When we created our migration, we added a unique constraint on the email field. We also updated our changeset/1 function to include logic when we added unique_constraint/2.

The unique_constraint/2 function is what we’ll focus on next. Before we test it, we should talk about what it does.

The unique constraint function

The unique constraint is handled by the database, not by our ...