Skip to content

Commit 5d93b7a

Browse files
committed
Add DbdiffTestMixin, drop dj17 support
1 parent 02021df commit 5d93b7a

File tree

6 files changed

+160
-8
lines changed

6 files changed

+160
-8
lines changed

.travis.yml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@ python:
44
- '3.5'
55
env:
66
matrix:
7-
- TOXENV=py27-django17-sqlite
8-
- TOXENV=py27-django17-mysql
9-
- TOXENV=py27-django17-postgresql
107
- TOXENV=py27-django18-sqlite
118
- TOXENV=py27-django18-mysql
129
- TOXENV=py27-django18-postgresql
@@ -16,9 +13,6 @@ env:
1613
- TOXENV=py27-django110-sqlite
1714
- TOXENV=py27-django110-mysql
1815
- TOXENV=py27-django110-postgresql
19-
- TOXENV=py34-django17-sqlite
20-
- TOXENV=py34-django17-mysql
21-
- TOXENV=py34-django17-postgresql
2216
- TOXENV=py34-django18-sqlite
2317
- TOXENV=py34-django18-mysql
2418
- TOXENV=py34-django18-postgresql

CHANGELOG

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1+
0.7.0 Added DbdiffTestMixin
2+
13
0.6.0 Added exclude

dbdiff/test.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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+
)

dbdiff/tests/test_mixin.json

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
[
2+
{
3+
"fields": {
4+
"app_label": "admin",
5+
"model": "logentry"
6+
},
7+
"model": "contenttypes.contenttype",
8+
"pk": 1
9+
},
10+
{
11+
"fields": {
12+
"app_label": "auth",
13+
"model": "permission"
14+
},
15+
"model": "contenttypes.contenttype",
16+
"pk": 2
17+
},
18+
{
19+
"fields": {
20+
"app_label": "auth",
21+
"model": "group"
22+
},
23+
"model": "contenttypes.contenttype",
24+
"pk": 3
25+
},
26+
{
27+
"fields": {
28+
"app_label": "auth",
29+
"model": "user"
30+
},
31+
"model": "contenttypes.contenttype",
32+
"pk": 4
33+
},
34+
{
35+
"fields": {
36+
"app_label": "contenttypes",
37+
"model": "contenttype"
38+
},
39+
"model": "contenttypes.contenttype",
40+
"pk": 5
41+
},
42+
{
43+
"fields": {
44+
"app_label": "sessions",
45+
"model": "session"
46+
},
47+
"model": "contenttypes.contenttype",
48+
"pk": 6
49+
},
50+
{
51+
"fields": {
52+
"app_label": "decimal_test",
53+
"model": "testmodel"
54+
},
55+
"model": "contenttypes.contenttype",
56+
"pk": 7
57+
},
58+
{
59+
"fields": {
60+
"app_label": "",
61+
"model": ""
62+
},
63+
"model": "contenttypes.contenttype",
64+
"pk": 8
65+
}
66+
]

dbdiff/tests/test_mixin.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from dbdiff.test import DbdiffTestMixin
2+
3+
from django import test
4+
from django.contrib.contenttypes.models import ContentType
5+
from django.db import connection
6+
7+
8+
class ContentTypeTestCase(DbdiffTestMixin, test.TestCase):
9+
dbdiff_models = [ContentType]
10+
dbdiff_exclude = {'*': ['created']}
11+
dbdiff_reset_sequences = True
12+
dbdiff_expected = 'dbdiff/tests/test_mixin.json'
13+
14+
def test_db_import(self):
15+
if connection.vendor != 'postgresql':
16+
return # not supported for now
17+
super(ContentTypeTestCase, self).test_db_import()
18+
self.assertTrue(self.dbdiff_test_executed)
19+
20+
def dbdiff_test(self):
21+
ContentType.objects.create()
22+
self.dbdiff_test_executed = True

tox.ini

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[tox]
2-
envlist = py{27,34}-django{17,18,19,110}{-sqlite,-mysql,-postgresql}
2+
envlist = py{27,34}-django{18,19,110}{-sqlite,-mysql,-postgresql}
33

44
[testenv]
55
usedevelop = true
@@ -16,7 +16,6 @@ deps =
1616
pytest-cov
1717
mock
1818
coverage
19-
django17: Django>=1.7,<1.8
2019
django18: Django>=1.8,<1.9
2120
django19: Django>=1.9,<1.10
2221
django110: https://github.com/django/django/archive/master.tar.gz

0 commit comments

Comments
 (0)