Now is time for quality and integrity testing with dbt.

Published: 2025-11-24

URL: https://thepipeandtheline.substack.com/p/sql-to-dbt-guide-hands-on-data-quality


Hi there! Alejandro here 😊 Suscribe if you like to read about technical data & AI learnings, deep dives! Enjoy the reading and let me know in the comments what you think about it 👨🏻‍💻 Subscribe now 📝 TL;DR • Data integrity keeps pipelines running, data quality prevents bad business decisions • Data contracts fail fast during compilation to catch schema issues early • Generic tests provide reusable quality checks (unique, not_null, custom validations) • Singular tests validate specific business logic that only domain experts know • dbt-expectations adds statistical anomaly detection for advanced quality monitoring • Test failures require different responses: contracts need schema fixes, generics need upstream investigation, singulars need business review Following up with our SQL to dbt series, now is the turn of talking about data quality on dbt.

You can know more about the series starting here: We started this dbt series without a perfect dataset, on purpose. Now we are going to cover how to catch problems on that messy data and take action. Table of Contents 📚 Data Integrity vs Data Quality Data Contracts: Schema enforcement during compilation Generic Tests: Reusable quality checks across models Singular Tests: Business logic validation dbt-expectations: Statistical anomaly detection ⚠️ This project was designed to fail on purpose. That's why dbt_project.yml has data contract enforcement lines commented out.

If it were active, the project wouldn't run. The idea of this article is to know about the different dbt testing capabilities, but also understand some fundamental concepts. Let's get started. Data Integrity vs Data Quality 🔒 Data integrity ensures that data is accurate, complete, consistent, and secure throughout its lifecycle. Data quality measures how well data meets the criteria for its intended purpose. You need both, but they solve different problems. Nice questions to ask yourself: Can you run your analysis without this data?

If no, integrity Does wrong data lead to bad decisions? If yes, quality The best approach: Start with integrity using constraints to prevent system failures. Add quality tests for business-critical calculations and relationships. Work closely with whoever produces the raw data: backend teams, backoffice users, and others. They’ll help you understand what must never fail and what needs testing to avoid erratic decisions. Data integrity keeps your pipeline running, quality keeps your business running.

The Multi-Layer Defense Strategy 📚 In dbt, data contracts are proactive—they block your models from building if criteria aren't met. Generic and singular tests are reactive—they let models build but warn you when issues appear. Let's see each layer in action using real synthetic marketing data. There will be hands-on example with the project so make sure to follow the sql-to-dbt series repo closely. Data Contracts Data contracts validate during model compilation, failing fast if schema violations occur.

Here's your staging model definition: Contracts stop bad data from entering your transformation pipeline. If campaign_id isn't unique, a column's data type doesn't match, or budget is negative, the model won't compile.

This catches schema problems before they propagate downstream. More info on dbt docs. Hands-on demo: ⚠️ For this section to work you need to go to uncomment the contract enforcement lines before executing the commands.

In this video I executed dbt run and noticed stg_sessions was missing some columns in the contract, so I look for them on staging/schema.yml and I added those columns with the corresponding data types.

That error is gone, but then we unlock another. I told you it was messy 😂 Thanks for reading The Pipe & The Line! This post is public so feel free to share it. Share Generic Tests dbt docs: Out of the box, dbt ships with four generic data tests already defined: unique, notnull, acceptedvalues and relationships.

But you can customise something for your use case. In this case, we have multiple columns that can't be negative, so these are great! Here's a budget numeric validation test: Apply these test via YAML configuration on the column level: Generic tests are reusable across models and catch universal data quality issues. More info on dbt docs. Hands-on demo: ⚠️ If you haven’t fix the errors of the previous data contracts, comment again the contract enforcement lines before executing the commands.

In this video I executed dbt test on intermediate layer model intcampaignsads.sql. First I ran all the models so they exist, and then I did an individual test on that model. We found out there are empty campaignid values in the dataset! Singular Tests dbt docs: Singular data tests placed in the tests directory are automatically executed when running dbt test. Don't reference singular tests in modelname.yml, as they are not treated as generic tests or macros, and doing so will result in an error.

Singular tests validate specific business rules that generic tests can't handle. Here's a campaign date logic test: These tests catch logical inconsistencies specific to marketing analytics that only domain experts would know to check. More info on dbt docs. You can execute all of those by running: dbt test --select test_type:singular Leave a comment dbt-expectations You can use it for similar use cases as mentioned before, but its really good at statistical validation or anomaly detection: This provides the statistical rigor of Great Expectations within your dbt workflow, catching anomalies that simple range checks might miss.

More info on dbt_expectations Execution Flow ⚒️ I did targeted tests on my examples, but you can run dbt build to make ALL the tests happen. If you use dbt build, your quality checks happen in layers: Contract Validation: Ensures schema compliance during model compilation Model Creation: Builds staging views with enforced contracts Test Execution: Runs all configured tests (generic, singular, and expectations) Failure Handling: Stops the pipeline or warns on critical test failures Note that using dbt run with contracts enforced will also apply them.

You can find all relevant test execution commands in this documentation on the series repo. What Does A Test Failure Means 🤝 Test failures warn you when something's wrong, and how serious they are depends on the test type. Depending on the context, tests require different responses and involve different stakeholders.

Here's how to handle them: Contract: Fix schema issues at the source or adjust model expectations Generic: Investigate data quality issues in upstream systems Singular: Business logic violations require domain expert review Expectations: Statistical anomalies may indicate data pipeline issues There’s more than just checking not nulls and unique rows—dbt offers a whole layer of tests to help you tackle your business requirements. If you enjoyed the content, hit the like ❤️ button, share, comment, repost, and all those nice things people do when like stuff these days.

Glad to know you made it to this part! Hi, I am Alejandro Aboy. I am currently working as a Data Engineer. I started in digital marketing at 19. I gained experience in website tracking, advertising, and analytics. I also founded my agency.

In 2021, I found my passion for data engineering. So, I shifted my career focus, despite lacking a CS degree. I'm now pursuing this path, leveraging my diverse experience and willingness to learn. Follow me on Linkedin