Skip to content

Commit

Permalink
Updating README
Browse files Browse the repository at this point in the history
  • Loading branch information
tabiva committed Sep 6, 2024
1 parent 80c132b commit 6e85dc1
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 28 deletions.
73 changes: 46 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,18 @@ Django Dharma is a Django library designed to facilitate running checks on model

The project consists of two main components:

- `django_dharma/`: The core library containing logic for running model checks.
- `test_project/`: A test Django project used to perform migrations and test the library with different Django versions.

## Installation

To install and set up the project, follow these steps:

1. **Clone the repository:**

```bash
git clone https://github.com/your-username/django-dharma.git
cd django-dharma
```

2. **Install dependencies:**
# Django Dharma

Make sure you have [Poetry](https://python-poetry.org/) installed. Then run:
Django Dharma is a Django library designed to facilitate running checks on models. It provides a structured way to perform and manage checks on your Django models.

```bash
poetry install
```
## Why Use Django Dharma?

3. **Add Django to the test project:**
Django Dharma is useful in scenarios where you need to validate data after it has been entered into your system. For example, if you are importing data from an external source without validating it during the import process (maybe you want to get them in your system as they are), you might want to perform validation checks afterward. With Django Dharma, you can execute checks such as:

```bash
poetry add django
```
- How many records have been inserted?
- Does the `foo` column contain values other than `bar`?

# Django Dharma

Django Dharma is a Django library designed to facilitate running checks on models. It provides a structured way to perform and manage checks on your Django models.
You can save the results of these checks and then analyze them or take necessary precautions based on the findings.

## Project Structure

Expand Down Expand Up @@ -72,9 +53,47 @@ To use Django Dharma, you need to run the `perform_checks` management command to
```bash
python manage.py migrate
```
2. **Create a check:**
To create a check, define a class that implements the `CheckProtocol`. The class should include a `run_checks` method and an attribute `model` of type `models.MyModel`. Here is an example:
```python
from datetime import datetime
from django_dharma.base import count_check
from myapp import models
class MyModelCheck:
model = models.MyModel
def run_checks(self) -> None:
"""
Verifies that the 'foo' column contains only 'biz' and 'foo' values.
"""
allowed_values = {'biz', 'foo'}
# Get distinct values in the 'foo' column
distinct_values = set(self.model.objects.values_list('foo', flat=True).distinct())
# Check if all distinct values are in the allowed_values set
assert distinct_values.issubset(allowed_values), (
f"Column 'foo' contains unexpected values: {distinct_values - allowed_values}"
)
print("All values in the 'foo' column are valid!")
"""
Checks that there are at least 30 records for today in the MyModel model.
"""
count_check(model=self.model, filters={"date": datetime.today().date()}, count=30)
print("All checks passed!")
```
2. **Run the checks:**
3. **Run the checks:**
```bash
python manage.py perform_checks
Expand Down
2 changes: 1 addition & 1 deletion django_dharma/protocols.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def run_checks(self) -> None:
check_instance.run_checks()
""" # noqa: E501

model: models.Model
model: models.Model = None

def run_checks(self) -> None:
"""
Expand Down

0 comments on commit 6e85dc1

Please # to comment.