Package: django-dbdiff
django-dbdiffI’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 assertionA 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 extractshuf -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 rundjango-admin dumpdata –indent=4 cities_light > cities_light/tests/cities_test_expected.txtthen, 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 withFixture('appname/path/to/fixture',models=[YourModelToTest]).assertNoDiff()in adjango.test.TransactionTestCasewhich hasreset_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.UsageExample:fromdjangoimportTransactionTestCasefromdbdiff.fixtureimportFixtureclassYourImportTest(test.TransactionTestCase):reset_sequences=Truedeftest_your_import(self):your_import()Fixture('yourapp/tests/yourtest.json',models=[YourModel]).assertNoDiff()The first time, it will raise aFixtureCreatedexception, 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, thendiff()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']}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.RequirementsMySQL, SQLite and PostgreSQL, Python 2.7 and 3.4 are supported along with
Django 1.7 to 1.10 - it’s always better to support django’s master so that we
canupgrade easily when it is released, which is one of the selling points
for having 100% coverage.InstallInstalldjango-dbdiffwith pip and adddbdifftoINSTALLED_APPS.Django model observerIt is interresting to note that a related, perhaps sort-of similar app exists:https://github.com/Griffosx/djmo