|
| 1 | +"""Convenience test mixin.""" |
| 2 | +from django.core.management import call_command |
| 3 | +from django.db import connection |
| 4 | + |
| 5 | +from .fixture import Fixture |
| 6 | + |
| 7 | + |
| 8 | +class DbdiffTestMixin(object): |
| 9 | + """ |
| 10 | + Convenience mixin with better sequence resetting than TransactionTestCase. |
| 11 | +
|
| 12 | + The difference with using TransactionTestCase with reset_sequences=True is |
| 13 | + that this will reset sequences for the given models to their higher value, |
| 14 | + supporting pre-existing models which could have been created by a |
| 15 | + migration. |
| 16 | +
|
| 17 | + The test case subclass requires some attributes and an implementation of a |
| 18 | + ``dbdiff_test()`` method that does the actual import call that this |
| 19 | + should test. Example usage:: |
| 20 | +
|
| 21 | + class FrancedataImportTest(DbdiffTestMixin, test.TestCase): |
| 22 | + dbdiff_models = [YourModel] |
| 23 | + dbdiff_exclude = {'*': ['created']} |
| 24 | + dbdiff_reset_sequences = True |
| 25 | + dbdiff_expected = 'yourapp/tests/yourexpectedfixture.json' |
| 26 | + dbdiff_fixtures = ['your-fixtures.json'] |
| 27 | +
|
| 28 | + def dbdiff_test(self): |
| 29 | + fixture = os.path.join( |
| 30 | + os.path.dirname(__file__), |
| 31 | + 'representatives_fixture.json' |
| 32 | + ) |
| 33 | +
|
| 34 | + with open(fixture, 'r') as f: |
| 35 | + do_your_import.main(f) |
| 36 | +
|
| 37 | + Supports postgresql. |
| 38 | + """ |
| 39 | + |
| 40 | + def test_db_import(self): |
| 41 | + """Actual test method, ran by the test suite.""" |
| 42 | + call_command('flush', interactive=False) |
| 43 | + |
| 44 | + for fixture in getattr(self, 'dbdiff_fixtures', []): |
| 45 | + call_command('loaddata', fixture) |
| 46 | + |
| 47 | + for model in self.dbdiff_models: |
| 48 | + if connection.vendor == 'postgresql': |
| 49 | + reset = """ |
| 50 | + SELECT |
| 51 | + setval( |
| 52 | + pg_get_serial_sequence('%s', 'id'), |
| 53 | + coalesce(max(id),0) + 1, |
| 54 | + false |
| 55 | + ) |
| 56 | + FROM %s |
| 57 | + """ % (model._meta.db_table, model._meta.db_table) |
| 58 | + else: |
| 59 | + raise NotImplemented() |
| 60 | + connection.cursor().execute(reset) |
| 61 | + |
| 62 | + self.dbdiff_test() |
| 63 | + |
| 64 | + Fixture( |
| 65 | + self.dbdiff_expected, |
| 66 | + models=self.dbdiff_models, |
| 67 | + ).assertNoDiff( |
| 68 | + exclude=self.dbdiff_exclude, |
| 69 | + ) |
0 commit comments