|
1 | | -# dbtest |
2 | | -Unittest wrapper focussed on DB integrity tests. Is your tool hard to debug? is it easier if you check it at the DB level? this module is for you |
| 1 | +# dbtest ·  [](https://opensource.org/licenses/MIT) |
| 2 | + |
| 3 | +Is your tool hard to debug? Is it easier if you check it at the DB level? |
| 4 | + |
| 5 | +This module is for you! **dbtests** is a Python tool for conducting database tests. It is a unittest wrapper focussed on making DB integrity tests. |
| 6 | +As it is an extension over the `unittest.TestCase` it is easy to configure and easy to integrate with GitHub actions. |
| 7 | + |
| 8 | + |
| 9 | + |
| 10 | +## Features |
| 11 | + |
| 12 | +- It has an integrated connection to a Postgres Database: easily configurable using the `.env` file. |
| 13 | +- Fully compatible with `SQLAlchemy` and `Pandas`: get a `panda.Dataframe` out of any SQL query you want to test. |
| 14 | +- Unit-test oriented: Check if the `panda.Dataframe` output matches the expected values, or catch it when the test fails. |
| 15 | + |
| 16 | +## Installation |
| 17 | + |
| 18 | +You can install **dbtests** using pip: |
| 19 | + |
| 20 | +```bash |
| 21 | +pip install dbtests |
| 22 | +``` |
| 23 | + |
| 24 | +## Usage |
| 25 | +Here's how you can use dbtests in your projects: |
| 26 | + |
| 27 | +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`. |
| 28 | +```shell |
| 29 | +my-tool/ |
| 30 | +├── my-tool/ |
| 31 | +│ ├── __init__.py |
| 32 | +│ ├── script_1.py |
| 33 | +└── tests/ |
| 34 | + ├── __init__.py |
| 35 | + └── test_script_1.py |
| 36 | +``` |
| 37 | + |
| 38 | +2. Once the script is created, we need to import the dependencies: |
| 39 | +```python |
| 40 | +# test_script_1.py |
| 41 | +# Dependencies |
| 42 | +import unittest |
| 43 | +from dbtest.unittest import DBintegrityTest |
| 44 | +``` |
| 45 | + |
| 46 | +3. 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: |
| 47 | +```python |
| 48 | +class TestDBTestModule(DBintegrityTest): |
| 49 | + db_config_file = '.env' |
| 50 | +``` |
| 51 | + |
| 52 | +4. Create as many `test` functions as pleased: |
| 53 | +```python |
| 54 | + def test_not_null_items_in_column(self): |
| 55 | + # the query that SHOULDN'T create an assertion |
| 56 | + sql_query = """ |
| 57 | + SELECT |
| 58 | + id |
| 59 | + FROM test_table; |
| 60 | + """ |
| 61 | + df = self.db.get_df_from_sql_query(sql_query) |
| 62 | + self.assertNotNullItemsInColumn(df, 'id') |
| 63 | + |
| 64 | +if __name__ == '__main__': |
| 65 | + unittest.main() |
| 66 | +``` |
| 67 | + |
| 68 | +5. Run it as you please: |
| 69 | +```shell |
| 70 | +python -m unittest tests/test_script_1.py |
| 71 | +``` |
| 72 | + |
| 73 | +The tests can be run locally or integrated with Github Actions. |
| 74 | +For more examples, please check: |
| 75 | +- [Github Action example](./.github/workflows/module_tests.yml) |
| 76 | +- [Test example](./tests/module.py) |
| 77 | + |
| 78 | +## Custom Assert |
| 79 | +The **dbtest** module contemplates new assert functions over `panda.Dataframe` objects. |
| 80 | +This way the result of a simple query can be easily checked with the standard `unittest` nomenclature. |
| 81 | + |
| 82 | +The list of current Asserts is the following: |
| 83 | +- assertNotNullItemsInColumn(self, df, column) |
| 84 | +- assertCustomNullItemsInColumn(self, df, column, target) |
| 85 | +- assertNoRows(self, df) |
| 86 | +- assertNRows(self, df, target_rows) |
| 87 | + |
| 88 | +There will be more to come (under demand most probably). Feel free to suggest new ones though! |
| 89 | + |
| 90 | + |
| 91 | +## Contributing |
| 92 | +If you want to contribute to this project, please follow these guidelines: |
| 93 | + |
| 94 | +- Fork the repository |
| 95 | +- Create a new branch |
| 96 | +- Make your changes |
| 97 | +- Submit a pull request |
| 98 | +- Bugs and Issues |
| 99 | + |
| 100 | +If you encounter any bugs or issues, please report them here. |
| 101 | + |
| 102 | +## Contact |
| 103 | +Author: Mikel Cortes ([@cortze](https://github.com/cortze)) |
| 104 | + |
| 105 | +Feel free to reach out if you have any questions or suggestions! |
| 106 | + |
| 107 | +## License |
| 108 | +This project is licensed under the MIT License - see the [LICENSE](./LICENSE) file for details. |
0 commit comments