Skip to main content

Pytest tooling to unittest DBT & Postgres models

Project description

PyTest DBT Postgres

This project aims to provide an easy way to validate a DBT project by enabling the unit-testing capability.

Dependencies

This project is based on the pytest-postgresql library, so we can validate our DBT postgres project without relying on an existing database.

Usage

The initial idea resides on pytest-postgres providing us an empty and isolated postgres where we can validate different use-cases with our DBT code.

If your project uses external sources, you will need to make sure they are properly defined in the yml files, since the project generates the DDLs from there.

import unittest

from pytest_dbt_postgres import auto_inject_fixtures
from pytest_dbt_postgres.dbt_executor import DbtExecutor
from pytest_dbt_postgres.dbt_validator import DbtValidator
from pytest_dbt_postgres.postgres.connector import PostgresConnector
from pytest_dbt_postgres.postgres.sql_methods import export_credentials_to_env, get_credentials

dbt_project_dir = '...'
resources_folder = '...'


@auto_inject_fixtures("postgresql")
class TestDbtValidator(unittest.TestCase):
    def setUp(self) -> None:
        credentials = get_credentials(self.postgresql)
        self.connector = PostgresConnector(credentials)

        # SETUP
        export_credentials_to_env(credentials)
        executor = DbtExecutor(dbt_project_dir=dbt_project_dir, profiles_dir=resources_folder)
        self.validator = DbtValidator(connector=self.connector, executor=executor, resources_folder=resources_folder)

    def test_orders(self) -> None:
        # GIVEN
        sources_to_load = [
            ("external.customers", "csv/input/customers.csv"),
            ("external.orders", "csv/input/orders.csv"),
        ]
        selector = "orders"
        outputs_to_validate = [
            ("dbt_unittest.orders", "csv/output/orders.csv"),
        ]
        # THEN
        self.validator.validate(sources_to_load, selector, outputs_to_validate)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytest_dbt_postgres-0.2.0.tar.gz (5.1 kB view details)

Uploaded Source

File details

Details for the file pytest_dbt_postgres-0.2.0.tar.gz.

File metadata

  • Download URL: pytest_dbt_postgres-0.2.0.tar.gz
  • Upload date:
  • Size: 5.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.10.14 Linux/6.5.0-1025-azure

File hashes

Hashes for pytest_dbt_postgres-0.2.0.tar.gz
Algorithm Hash digest
SHA256 94368c9386386fbbb4f7174688a3860e7b69ae4a36ba22b12f7f7d782e7a8693
MD5 9b3194a9d0c6d59ac33b373a2a2e9c2e
BLAKE2b-256 742280afb039f489fce47ba9dc9f262b9d16c1a24d0596d1db103659ecf658e6

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page