dbt Functional Testing
Relevant source files
The following files were used as context for generating this wiki page:
dbt Functional Testing within the flext-dbt-ldif project refers to the systematic validation of data transformations, macro logic, and model integrity. This testing framework ensures that LDIF data processed through dbt adheres to business rules and structural expectations across various adapter targets including Postgres, Redshift, BigQuery, and Snowflake. Sources: README.md:1-10, dbt_project.yml:1-10
The testing architecture is divided into two primary layers: integration tests for dbt packages (dbt_utils, codegen) and unit/workflow tests for the Python-based dbt client. These tests validate the full lifecycle from raw LDIF parsing to final analytics model generation. Sources: tests/unit/test_dbt_client.py:90-100, dbt_packages/dbt_utils/run_test.sh:1-12
Testing Architecture and Workflow
The testing workflow is primarily driven by shell scripts that orchestrate dbt commands within dedicated integration_tests directories. These scripts manage environment setup, dependency installation, and execution of the dbt build cycle.
Execution Flow
The standard execution flow for functional tests involves a sequential series of dbt operations to prepare the environment and validate results.
This diagram illustrates the lifecycle of a dbt functional test run as defined in project shell scripts.
Sources: dbt_packages/dbt_utils/run_test.sh:5-12, dbt_packages/codegen/run_test.sh:5-10
Key Components
| Component | Responsibility | Relevant Files |
|---|---|---|
| Integration Test Runner | Orchestrates deps, seed, run, and test commands. | run_test.sh |
| Profiles Configuration | Defines database connections for different environments (Postgres, Snowflake, etc.). | profiles.yml |
| Project Configuration | Sets up test-specific schemas, seeds, and dispatch logic. | dbt_project.yml |
| Data Quality Tests | Asserts data equality, null checks, and uniqueness. | schema.yml |
Sources: dbt_packages/dbt_utils/run_test.sh, dbt_packages/codegen/integration_tests/profiles.yml, dbt_packages/dbt_utils/integration_tests/models/sql/schema.yml
Test Types and Implementation
1. Data Assertion Tests
Functional tests utilize dbt_utils to perform complex data assertions. These tests compare actual model output against expected results stored in seed files.
- Equality Tests: Compares the content of a generated model against a reference model or seed. Sources: dbt_packages/dbt_utils/integration_tests/models/sql/schema.yml:45-48
- Single Value Assertions: Validates specific values (date, float, int, string) within a result set. Sources: dbt_packages/dbt_utils/integration_tests/models/sql/schema.yml:4-15
- Constraints: Validates
not_null,unique, andaccepted_values. Sources: dbt_packages/dbt_utils/integration_tests/models/sql/schema.yml:100-110
2. Python Client Integration Tests
The FlextDbtLdifClient provides a Python interface to run the full dbt pipeline, which is validated through unit tests.
Sequence of operations during a full pipeline integration test.
Sources: tests/unit/test_dbt_client.py:90-100
3. Macro Validation
Macros such as generate_source and table relation fetchers are tested by running dbt operations that trigger them during the build process.
- Source Generation: Validates that YAML source definitions are correctly generated from database schemas. Sources: dbt_packages/codegen/macros/generate_source.sql:1-15
- Table Discovery: Tests the ability to find tables within specific schemas using patterns. Sources: dbt_packages/codegen/macros/generate_source.sql:1-10
Configuration for Multi-Target Testing
The functional testing framework supports multiple database backends by utilizing environment variables in the profiles.yml file. This allows the same test suite to run against different adapters by passing a target argument to the test script.
integration_tests:
target: postgres
outputs:
postgres:
type: "postgres"
host: "{{ env_var('POSTGRES_HOST') }}"
user: "{{ env_var('POSTGRES_USER') }}"
# ... other config
snowflake:
type: "snowflake"
account: "{{ env_var('SNOWFLAKE_ACCOUNT') }}"
# ... other config
Sources: dbt_packages/codegen/integration_tests/profiles.yml:6-18, dbt_packages/codegen/integration_tests/profiles.yml:41-48
Conclusion
dbt Functional Testing in this repository ensures the reliability of LDIF transformations through a combination of dbt-native schema tests and Python-driven pipeline validation. By utilizing dbt_utils for assertions and shell-driven integration tests, the project maintains high data quality standards across multiple enterprise database platforms.