Skip to content

Commit bfde949

Browse files
authored
Merge pull request #2 from cortze/extend-readme
Extend readme
2 parents 5deea70 + 3b9f845 commit bfde949

File tree

3 files changed

+126
-3
lines changed

3 files changed

+126
-3
lines changed

.github/pull_request_template.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Motivation
2+
<!-- Why are we developing this new code. Is the refactor needed, is the feature getting more data... -->
3+
4+
5+
_Related links:_
6+
- first link
7+
8+
# Description
9+
<!-- How are we approaching to solve the problem or deploy the new code -->
10+
<!-- What new structures will be added or what major changes will be applied -->
11+
12+
# Tasks
13+
<!-- Checklist of tasks to do -->
14+
- [ ] first task
15+
16+
# Proof of Success
17+
<!-- Here we may add any logs proving that we achieved what we expected or even diagrams that might be useful to understand the code -->

.github/workflows/module_tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# taking as reference: https://github.com/actions/starter-workflows/blob/main/ci/python-package.yml
22
name: Module Tests
33

4-
on: [push, pull_request]
4+
on: [push, merge_group]
55

66

77
env:

README.md

Lines changed: 108 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,108 @@
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 &middot; ![](https://github.com/cortze/dbtest/actions/workflows/module_tests.yml/badge.svg) [![License](https://img.shields.io/badge/license-MIT-blue.svg)](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

Comments
 (0)