Skip to main content

A package to test our databases

Project description

database-testing-tools

Database Testing Tools comprises a suite of generalised tests to run on your database, and produce reports for a notebook. It is designed for pre-production checks for curated databases, but can also be useful for monitoring changes over time. It can be particularly useful when combined with papermill, which can be used to parameterize, execute and store notebooks as part of your pipeline.

Installation

pip install database_testing_tools

Usage

The package implements a Tester class, from which you can invoke a number of built-in tests and optional plotting features.

from database_testing_tools import Tester
test = Tester()
database = "my_database_name"
out = test.check_all_tables_return_data(db_name=database)

Tests return two outputs: the result of the test; and some nice graphical representation of the test - either a markdown table, or a plot using the Altair plotting library.

The class also collects the results of the tests for an overall report, which you can access with the Tester.get_results_table() function.

You can find a notebook which demonstrates the functions available in the package.

Curated database assumptions - SCD2 vs Snapshotting

The tests in this package assume the database being tested is versioned in one of two ways:

  1. Slowly Changing Dimensions type 2 (SCD2): where the database includes records of inserts and updates to records. In curated databases a start time and end time will be added to each record. The start time is when the record is either created or last updated; the end time is either when the record was next updated, or a date arbitrarily far in the future to signify that it is the current version of the record. By filtering on these dates, it is possible to view the state of the database at a given point in time.
  2. Snapshots: when SCD2 isn't possible or is impractical, versions of the database are recorded with an associated snapshot date based on date of extraction.

These methods require different methods to compare versions of the database over time, so there are some separate functions to handle them in their own way.

Query engines

By default, the package uses pydbtools to run the queries in the package on a given Amazon Athena database. However, it is possible to connect to your own database by making an extension of the BaseQueryEngine class defined in utils.py.

An engine for DuckDB is included, and is used for the demo and unit tests in this package. A basic use of the DuckDB engine looks like this:

import duckdb
from database_testing_tools.utils import DuckDbEngine
from database_testing_tools import Tester

conn = duckdb.connect()
query_engine = DuckDbEngine(conn)
tester = Tester(query_engine=query_engine)
database = "some_database"

tester.check_all_tables_return_data(db_name=database)

Alternative database engines should follow a similar pattern to the DuckDB engine - an initialiser that includes the database connection object, and an implementation of the run_query function that runs a given SQL query and returns the result as a pandas dataframe. As the built-in tests use the Trino SQL dialect, it is recommended to transpile the SQL with sqlglot to ensure the tests are executed correctly.

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

database_testing_tools-1.0.3.tar.gz (14.4 kB view details)

Uploaded Source

Built Distribution

database_testing_tools-1.0.3-py3-none-any.whl (13.6 kB view details)

Uploaded Python 3

File details

Details for the file database_testing_tools-1.0.3.tar.gz.

File metadata

  • Download URL: database_testing_tools-1.0.3.tar.gz
  • Upload date:
  • Size: 14.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.0 CPython/3.9.18 Linux/6.2.0-1015-azure

File hashes

Hashes for database_testing_tools-1.0.3.tar.gz
Algorithm Hash digest
SHA256 4211b92f343dc7d38a45734281dd894e49e530d07012d0a6b4ca0fb735de2f8a
MD5 afe5a1e6c4532ea5848f93a7a5f49672
BLAKE2b-256 73c6f1c017d99367f41093e2be15a539013ead30efd4234db02e8f655eef6803

See more details on using hashes here.

File details

Details for the file database_testing_tools-1.0.3-py3-none-any.whl.

File metadata

File hashes

Hashes for database_testing_tools-1.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 393528e3d56ae81214c461c6c9ddbcb05f386cb113d4384ac47c53177651d03c
MD5 e70d7e75728a2f9562e7f81ce2fabd97
BLAKE2b-256 63edc43bdb233f541f48f82a0ab9311febe3a46d520bf4fc923999c515f0678f

See more details on using hashes here.

Supported by

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