Skip to main content
https://github.com/yourlabs/django-dbdiff/actions/workflows/ci.yml/badge.svg https://codecov.io/gh/yourlabs/django-dbdiff/branch/master/graph/badge.svg https://badge.fury.io/py/django-dbdiff.png

django-dbdiff

I’m pretty lazy when it comes to writing tests for existing code, however, I’m even lazier when it comes to repetitive manual testing action.

This package aims at de-duplicating the data import tests from django-representatives and django-representatives-votes which is re-used in django-cities-light.

Database state assertion

A nice way to test a data import script is to create a source data fixture with a subset of data, ie. with only 10 cities instead of 28K or only 3 european parliament representatives instead of 3600, feed the import function with that and then compare the database state with a django fixture. This looks like what I was used to do:

  • use such a command to create a small data extract shuf -n3 cities15000.txt > cities_light/tests/cities_test_fixture.txt,

  • use it against the import script on a clean database,

  • verify the database manually, and run django-admin dumpdata –indent=4 cities_light > cities_light/tests/cities_test_expected.txt

  • then, make a test case that calls the import script against the fixture,

  • write and maintain some funny (fuzzy ?) repetitive test code to ensure that the database is in the expected state.

When a bug is fixed, just add the case to the fixture and repeat the process to create new expected data dumps, use coverage to ensure no case is missed.

With django-dbdiff, I just need to maintain to initial data extract, and test it with Fixture('appname/path/to/fixture', models=[YourModelToTest]).assertNoDiff() in a django.test.TransactionTestCase which has reset_sequences=True:

  • if the fixture in question doesn’t exist, it’ll be automatically created on with dumpdata for the concerned models on the first run, raising “FixtureCreated” exception to fail the test and inform of the path of the created fixture, so that it doesn’t mislead the user in thinking the test passed with an existing fixture,

  • if the fixture exists, it’ll run dumpdata on the models concerned and GNU diff it against the fixture, if there’s any output it’ll be raised in the “DiffFound” exception, failing the test and printing the diff.

Usage

Example:

from django.test import TransactionTestCase
from dbdiff.fixture import Fixture


class YourImportTest(TransactionTestCase):
    reset_sequences = True

    def test_your_import(self):
        your_import()

        Fixture('yourapp/tests/yourtest.json',
                models=[YourModel]).assertNoDiff()

The first time, it will raise a FixtureCreated exception, and the test will fail. This is to inform the user that the test didn’t really run. On the next run though, it will pass.

If any difference is found between the database and the test fixture, then diff() will return the diff as outputed by GNU diff.

If you need to ignore fields globally, set the class-level variable exclude as such:

Fixture.exclude = {'mrsrequest.mrsrequest': ['token']}

If your import produces records with non-deterministic primary keys (e.g. UUIDs or sequence gaps), pass ignore_pk=True to match records by their field content instead of by pk:

Fixture('yourapp/tests/yourtest.json',
        models=[YourModel],
        ignore_pk=True).assertNoDiff()

Instead of deleting the fixtures manually before running the tests to regenerate them, just run your tests with FIXTURE_REWRITE=1 environment variable. This will overwrite the fixtures and make the tests look like it passed.

See tests and docstrings for crunchy details.

Requirements

MySQL, SQLite and PostgreSQL, Python 3.10 to 3.14 are supported along with Django 4.2, 5.2, and 6.0 - it’s always better to support django’s master so that we can upgrade easily when it is released, which is one of the selling points for having 100% coverage.

Install

Install django-dbdiff with pip and add dbdiff to INSTALLED_APPS.

Django model observer

It is interresting to note that a related, perhaps sort-of similar app exists: https://github.com/Griffosx/djmo

Metadata

Release files for django-dbdiff 0.9.7

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

Source distribution (sdist)

Source distribution for django-dbdiff 0.9.7
File Size Uploaded
django_dbdiff-0.9.7.tar.gz 18.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-dbdiff 0.9.7
File Interpreter ABI Platform
django_dbdiff-0.9.7-py3-none-any.whl Python 3 none any Details

Total release size: 42.7 kB

Release files / django_dbdiff-0.9.7.tar.gz

Download URL django_dbdiff-0.9.7.tar.gz
Size 18.9 kB
Tags Source
SHA-256 checksum
How to use checksums
f972361d816c0014c164e25a20f51632ae53a827182af634c3590e22c05aeb70
BLAKE2b-256 checksum
How to use checksums
800e9b35f57ad81cf2538dd1d529d23773e50fce884c1fee405e7bd890b26ec1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.5

Release files / django_dbdiff-0.9.7-py3-none-any.whl

Download URL django_dbdiff-0.9.7-py3-none-any.whl
Size 23.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
be4c36563e99db6a97e09cbf640c781a911643f036ad78e59738a2f08befb73d
BLAKE2b-256 checksum
How to use checksums
515c1d48a8efc18d5d5eba954a12d406c69cb7d604b48b64b2efc24e7d96679e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.5

Release history Release notifications | RSS feed

This release

0.9.7 This release

2 release files

0.9.6

2 release files

0.9.5

1 release file

0.9.4

1 release file

0.9.3

1 release file

0.9.2

1 release file

0.9.1

1 release file

0.9.0

1 release file

0.8.1

1 release file

0.8.0

1 release file

0.7.3

1 release file

0.7.2

1 release file

0.7.1

1 release file

0.7.0

1 release file

0.6.0

1 release file

0.5.0

1 release file

0.4.0

1 release file

0.3.0

1 release file

0.2.0

1 release file

0.1.1

1 release file

0.1.0

1 release file

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