Skip to main content

unit-db-test · PyPI Release License

Is your tool hard to debug? Would it be easier to check it at the DB level?

This module is for you! unit-db-test is a Python tool for conducting database tests. It is a unittest wrapper focussed on making DB integrity tests. As it is an extension over the unittest.TestCase it is easy to configure and easy to integrate with GitHub actions.

Features

  • It has an integrated connection to a Postgres Database: easily configurable using the .env file.
  • Fully compatible with SQLAlchemy and Pandas: get a pandas.Dataframe out of any SQL query you want to test.
  • Unit-test oriented: Check if the pandas.Dataframe output matches the expected values, or catch it when the test fails.

Installation

You can install dbtests using pip:

pip install unit-db-test

Usage

Here's how you can use dbtests in your projects:

  1. Create a ./test folder as with any other kind of unittests (it is in fact compatible with other unittests just make there is a connection to a Postgres DB), and create a test-file.
my-tool/
├── my-tool/
│   ├── __init__.py
│   ├── script_1.py
└── tests/
    ├── __init__.py
    └── test_script_1.py
  1. Once the script is created, we need to import the dependencies:
# test_script_1.py
# Dependencies
import unittest
from unit_db_test.testcase import DBintegrityTest
  1. Create a DBintegrityTest as if it was a unittest.TestCase. It is important to define the path to the .env that keeps the Postgres DB credentials:
class TestDBTestModule(DBintegrityTest):
    db_config_file = '.postgresql.env'
  1. Create as many test functions as pleased:
    def test_not_null_items_in_column(self):
        # the query that SHOULDN'T create an assertion
        sql_query = """
        SELECT 
            id
        FROM test_table;        
        """
        df = self.db.get_df_from_sql_query(sql_query)
        self.assertNotNullItemsInColumn(df, 'id')

if __name__ == '__main__':
    unittest.main()
  1. Run it as you please:
python -m unittest tests/test_script_1.py

The tests can be run locally or integrated with Github Actions. For more examples, please check:

Custom Assert

The dbtest module contemplates new assert functions over pandas.Dataframe objects. This way the result of a simple query can be easily checked with the standard unittest nomenclature.

The list of current Asserts is the following:

  • assertNotNullItemsInColumn(self, df, column)
  • assertCustomNullItemsInColumn(self, df, column, target)
  • assertNoRows(self, df)
  • assertNRows(self, df, target_rows)

There will be more to come (under demand most probably). Feel free to suggest new ones though!

Contributing

If you want to contribute to this project, please follow these guidelines:

  • Fork the repository
  • Create a new branch
  • Make your changes
  • Submit a pull request
  • Bugs and Issues

If you encounter any bugs or issues, please report them here.

Contact

Author: Mikel Cortes (@cortze)

Feel free to reach out if you have any questions or suggestions!

License

This project is licensed under the MIT License - see the LICENSE file for details.

Metadata

Release files for unit-db-test 0.1.13

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for unit-db-test 0.1.13
File Size Uploaded Provenance Actions
unit-db-test-0.1.13.tar.gz 11.5 kB Without provenance Details for unit-db-test-0.1.13.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for unit-db-test 0.1.13
File Interpreter ABI Platform Actions
unit_db_test-0.1.13-py3-none-any.whl Python 3 none any Details for unit_db_test-0.1.13-py3-none-any.whl

Total release size: 18.6 kB

Release files / unit-db-test-0.1.13.tar.gz

Download URL unit-db-test-0.1.13.tar.gz
Size 11.5 kB
Tags Source
SHA-256 checksum
How to use checksums
9b7ad7f8ffaebc67d0153a6be042a76cd4491f907abde9c7ed71626bf7b85f8d
BLAKE2b-256 checksum
How to use checksums
982ba439d2e7ca954472f14838cc4287da5df1a56df35aabd93106481571b2bd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.12.1

Release files / unit_db_test-0.1.13-py3-none-any.whl

Download URL unit_db_test-0.1.13-py3-none-any.whl
Size 7.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
233c9f13c6daf7a3fd105c2fd24b1aa7c232db33155ac28c4de210bd1a7f2709
BLAKE2b-256 checksum
How to use checksums
0bfd5fb311416f2762c7a4684b80b5e9bb82db94f2b48f59789527082e8bca10
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.12.1

Security for unit-db-test 0.1.13

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page