Skip to content

Commit

Permalink
Updating examples and fixing codeblocks
Browse files Browse the repository at this point in the history
  • Loading branch information
tabiva committed Sep 15, 2024
1 parent 3f7fa71 commit cda70ad
Showing 1 changed file with 56 additions and 42 deletions.
98 changes: 56 additions & 42 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Dharma, you can execute checks such as:

- How many records have been inserted?
- Does the ``foo`` column contain values other than ``bar``?
- Does each ``biz`` entry correspond to a ``baz`` entry?

You can save the results of these checks and then analyze them or take
necessary precautions based on the findings.
Expand All @@ -36,14 +37,21 @@ Installation

To install Django Dharma, you can use ``pip``:

#. **Install the package:**
1. **Install the package:**

``bash pip install django-dharma``
.. code-block:: bash
#. **Add ``django_dharma`` to your Django project's ``INSTALLED_APPS``
pip install django-dharma
2. **Add ``django_dharma`` to your Django project's ``INSTALLED_APPS``
in ``settings.py``:**

``python INSTALLED_APPS = [ # ... other installed apps 'django_dharma', ]``
.. code-block:: python
INSTALLED_APPS = [
# ... other installed apps
'django_dharma',
]
Usage
-----
Expand All @@ -53,74 +61,76 @@ command to execute the checks on your models. This command will collect
all implementations of the specified protocol and run the checks, saving
any anomalies to the ``Anomaly`` model.

#. **Run migrations:**
1. **Run migrations:**

\```bash python manage.py migrate
.. code-block:: bash
\``\`
python manage.py migrate
#. **Create a check:**
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:

``
from datetime import datetime

from django\ *dharma.base
import count*\ check from myapp import models
.. code-block:: python
class MyModelCheck: model = models.MyModel
from datetime import datetime
from django_dharma.base import count_check
from myapp import models
def run_checks(self) -> None:
"""
Verifies that the 'foo' column contains only 'biz' and 'foo' values.
"""
allowed_values = {'biz', 'foo'}
class MyModelCheck:
model = models.MyModel
# Get distinct values in the 'foo' column
distinct_values = set(self.model.objects.values_list('foo', flat=True).distinct())
def run_checks(self) -> None:
"""
Verifies that the 'foo' column contains only 'biz' and 'foo' values.
"""
allowed_values = {'biz', 'foo'}
# 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}"
)
# 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}"
)
"""
Some example checks are included in this package.
Please contribute if you have useful checks to share!
This check verifies that there are at least 30 records in the MyModel model for today.
"""
count_check(model=self.model, filters={"date": datetime.today().date()}, count=30)
# This check verifies that there are at least 30 records
# in the MyModel model for today.
count_check(model=self.model, filters={"date": datetime.today().date()}, count=30)
print("All checks passed!")
print("All checks passed!")
``
3. **Run the checks:**

#. **Run the checks:**
.. code-block:: bash
``bash python manage.py perform_checks``
python manage.py perform_checks
Contributing
------------

If you would like to contribute to the project, please follow these
steps:

#. **Fork the repository.**
1. **Fork the repository.**

#. **Create a branch for your change:**
2. **Create a branch for your change:**

``bash git checkout -b my-feature``
.. code-block:: bash
#. **Add and commit your changes:**
git checkout -b my-feature
``bash git add . git commit -m "Add a new feature"``
3. **Add and commit your changes:**

#. **Push your branch and open a pull request.**
.. code-block:: bash
git add .
git commit -m "Add a new feature"
4. **Push your branch and open a pull request.**

Testing
-------
Expand All @@ -129,4 +139,8 @@ The project uses ``flake8`` for linting, ``black`` for code formatting,
and ``isort`` for import sorting. You can run linting and formatting
checks with the following commands:

``bash poetry run flake8 django_dharma/ poetry run black --check django_dharma/ poetry run isort --check-only django_dharma/``
.. code-block:: bash
poetry run flake8 django_dharma/
poetry run black --check django_dharma/
poetry run isort --check-only django_dharma/

0 comments on commit cda70ad

Please sign in to comment.