Merge lp:~ricardokirkner/django-pgtools/allow-backend-subclasses into lp:django-pgtools

Proposed by Ricardo Kirkner
Status: Merged
Approved by: Łukasz Czyżykowski
Approved revision: 6
Merged at revision: 6
Proposed branch: lp:~ricardokirkner/django-pgtools/allow-backend-subclasses
Merge into: lp:django-pgtools
Diff against target: 181 lines (+76/-18)
3 files modified
pgtools/tests.py (+37/-10)
pgtools/utils.py (+9/-2)
setup.py (+30/-6)
To merge this branch: bzr merge lp:~ricardokirkner/django-pgtools/allow-backend-subclasses
Reviewer Review Type Date Requested Status
Łukasz Czyżykowski (community) Approve
Review via email: mp+149855@code.launchpad.net

Commit message

Support any backend that inherits from postgresql_psycopg2.

Description of the change

Support any backend that inherits from postgresql_psycopg2.

Also got tests to run again.

To post a comment you must log in.
Revision history for this message
Ubuntu One Auto Pilot (otto-pilot) wrote :

Voting does not meet specified criteria. Required: Approve >= 1, Disapprove == 0, Needs Fixing == 0, Needs Information == 0, Resubmit == 0, Pending == 0. Got: 1 Pending.

Revision history for this message
Łukasz Czyżykowski (lukasz-czyzykowski) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'pgtools/tests.py'
--- pgtools/tests.py 2012-04-13 18:11:02 +0000
+++ pgtools/tests.py 2013-02-21 15:03:28 +0000
@@ -13,11 +13,14 @@
1313
14from mock import Mock, patch14from mock import Mock, patch
1515
16import django
16from django.conf import settings17from django.conf import settings
17from django.core.management import call_command18from django.core.management import call_command
18from django.core.management.base import CommandError19from django.core.management.base import CommandError
19from django.db import connection20from django.db import connection
21from django.db.backends import postgresql_psycopg2
20from django.db.models.loading import get_models22from django.db.models.loading import get_models
23from django.db.utils import load_backend
21from django.test import TestCase24from django.test import TestCase
2225
2326
@@ -102,7 +105,7 @@
102 connection.settings_dict[setting_name] = attvalue105 connection.settings_dict[setting_name] = attvalue
103 else:106 else:
104 # Django 1.0 doesn't have setting cache, so there's nothing to107 # Django 1.0 doesn't have setting cache, so there's nothing to
105 # refresh 108 # refresh
106 pass109 pass
107 connection.close()110 connection.close()
108111
@@ -441,20 +444,25 @@
441 self.assertEqual(str(err),444 self.assertEqual(str(err),
442 "User cannot be deleted: %s" % TEST_USER_NAME1)445 "User cannot be deleted: %s" % TEST_USER_NAME1)
443446
447 def get_backends(self):
448 django_version = django.VERSION[:2]
449 backends = ['django.db.backends.postgresql_psycopg2']
450 if django_version < (1,4):
451 backends.insert(0, 'postgresql_psycopg2')
452 return backends
453
444 def test_utils_check_database_engine_11_style(self):454 def test_utils_check_database_engine_11_style(self):
445 # Test default: 'postgresql_psycopg2' (hopefully)455 # Test default: 'postgresql_psycopg2' (hopefully)
446 settings_to_patch = {}456 settings_to_patch = {}
447 if hasattr(settings, 'DATABASES'):457 if hasattr(settings, 'DATABASES'):
448 settings_to_patch['DATABASES'] = None458 settings_to_patch['DATABASES'] = None
449 for engine in [459 for engine in self.get_backends():
450 'postgresql_psycopg2',
451 'django.db.backends.postgresql_psycopg2']:
452 settings_to_patch['DATABASE_ENGINE'] = engine460 settings_to_patch['DATABASE_ENGINE'] = engine
453 with patch_settings(**settings_to_patch):461 with patch_settings(**settings_to_patch):
454 check_database_engine()462 check_database_engine()
455463
456 # Test an unsupported engine464 # Test an unsupported engine
457 settings_to_patch['DATABASE_ENGINE'] = 'sqlite3'465 settings_to_patch['DATABASE_ENGINE'] = 'django.db.backends.sqlite3'
458 with patch_settings(**settings_to_patch):466 with patch_settings(**settings_to_patch):
459 self.assertRaises(CommandError, check_database_engine)467 self.assertRaises(CommandError, check_database_engine)
460468
@@ -462,16 +470,35 @@
462 def test_utils_check_database_engine_13_style(self, mock_settings):470 def test_utils_check_database_engine_13_style(self, mock_settings):
463 dbconfig = {'default': {'NAME': 'mydatabase'}}471 dbconfig = {'default': {'NAME': 'mydatabase'}}
464 mock_settings.DATABASES = dbconfig472 mock_settings.DATABASES = dbconfig
465 for engine in ['postgresql_psycopg2',473 for engine in self.get_backends():
466 'django.db.backends.postgresql_psycopg2']:
467 dbconfig['default']['ENGINE'] = engine474 dbconfig['default']['ENGINE'] = engine
468 mock_settings.DATABASE_ENGINE = 'sqlite3'475 mock_settings.DATABASE_ENGINE = 'django.db.backends.sqlite3'
469 check_database_engine()476 check_database_engine()
470 # Test an unsupported engine477 # Test an unsupported engine
471 dbconfig['default']['ENGINE'] = 'sqlite3'478 dbconfig['default']['ENGINE'] = 'django.db.backends.sqlite3'
472 mock_settings.DATABASE_ENGINE = 'postgresql_psycopg2'479 mock_settings.DATABASE_ENGINE = 'django.db.backends.postgresql_psycopg2'
473 self.assertRaises(CommandError, check_database_engine)480 self.assertRaises(CommandError, check_database_engine)
474481
482 @patch('pgtools.utils.settings')
483 def test_utils_check_database_engine_subclass_13_style(self,
484 mock_settings):
485 class CustomDatabaseWrapper(postgresql_psycopg2.base.DatabaseWrapper):
486 pass
487
488 dbconfig = {'default': {'NAME': 'mydatabase',
489 'ENGINE': 'custom_backend'}}
490 mock_settings.DATABASE_ENGINE = 'custom_backend'
491 mock_settings.DATABASES = dbconfig
492 def mock_load_backend(name):
493 if name == 'custom_backend':
494 backend = Mock()
495 backend.DatabaseWrapper = CustomDatabaseWrapper
496 return backend
497 return load_backend(name)
498
499 with patch('pgtools.utils.load_backend', mock_load_backend):
500 check_database_engine()
501
475 def test_utils_get_rolename_from_settings(self):502 def test_utils_get_rolename_from_settings(self):
476 # Test with default (nothing set)503 # Test with default (nothing set)
477 self.assertRaises(CommandError, get_rolename_from_settings)504 self.assertRaises(CommandError, get_rolename_from_settings)
478505
=== modified file 'pgtools/utils.py'
--- pgtools/utils.py 2012-04-13 18:11:02 +0000
+++ pgtools/utils.py 2013-02-21 15:03:28 +0000
@@ -4,18 +4,25 @@
44
5from django.conf import settings5from django.conf import settings
6from django.core.management.base import CommandError6from django.core.management.base import CommandError
7from django.db.utils import load_backend
78
89
9def check_database_engine():10def check_database_engine():
10 all_psql = True11 all_psql = True
12 postgresql_psycopg2_engine = load_backend('django.db.backends.postgresql_psycopg2')
11 if getattr(settings, 'DATABASES', None):13 if getattr(settings, 'DATABASES', None):
12 # Django 1.2+ style14 # Django 1.2+ style
13 for db in settings.DATABASES.values():15 for db in settings.DATABASES.values():
14 if not db['ENGINE'].endswith('postgresql_psycopg2'):16 engine = load_backend(db['ENGINE'])
17 if not issubclass(engine.DatabaseWrapper,
18 postgresql_psycopg2_engine.DatabaseWrapper):
15 all_psql = False19 all_psql = False
16 else:20 else:
17 # Django -1.1 style21 # Django -1.1 style
18 all_psql = settings.DATABASE_ENGINE.endswith('postgresql_psycopg2')22 engine = load_backend(settings.DATABASE_ENGINE)
23 if not issubclass(engine.DatabaseWrapper,
24 postgresql_psycopg2_engine.DatabaseWrapper):
25 all_psql = False
19 if not all_psql:26 if not all_psql:
20 raise CommandError(27 raise CommandError(
21 'Only the postgresql_psycopg2 database engine is supported.')28 'Only the postgresql_psycopg2 database engine is supported.')
2229
=== modified file 'setup.py'
--- setup.py 2011-06-02 14:54:18 +0000
+++ setup.py 2013-02-21 15:03:28 +0000
@@ -18,17 +18,41 @@
18 def run(self):18 def run(self):
19 """Run the project tests."""19 """Run the project tests."""
2020
21 import django
21 from django.conf import settings22 from django.conf import settings
22 from django.core.management import call_command23 from django.core.management import call_command
2324
24 settings.configure(**{25 config = {
25 'DATABASE_ENGINE': 'postgresql_psycopg2',
26 'DATABASE_NAME': 'pgtools',
27 'DATABASE_USER': 'postgres',
28 'SITE_ID': 1,26 'SITE_ID': 1,
29 'ROOT_URLCONF': '',27 'ROOT_URLCONF': '',
30 'INSTALLED_APPS': ['pgtools'],28 'INSTALLED_APPS': [
31 })29 'django.contrib.admin',
30 'django.contrib.auth',
31 'django.contrib.contenttypes',
32 'django.contrib.sessions',
33 'pgtools',
34 ],
35 }
36
37 django_version = django.VERSION[:2]
38 if django_version > (1,1):
39 config.update({
40 'DATABASES': {
41 'default': {
42 'NAME': 'pgtools',
43 'ENGINE': 'django.db.backends.postgresql_psycopg2',
44 'USER': 'postgres',
45 }
46 }
47 })
48 if django_version < (1,4):
49 config.update({
50 'DATABASE_ENGINE': 'django.db.backends.postgresql_psycopg2',
51 'DATABASE_NAME': 'pgtools',
52 'DATABASE_USER': 'postgres',
53 })
54
55 settings.configure(**config)
3256
33 call_command('test', interactive=False)57 call_command('test', interactive=False)
3458

Subscribers

People subscribed via source and target branches