diff --git a/django/db/backends/postgresql_psycopg2/base.py b/django/db/backends/postgresql_psycopg2/base.py index 1530e20eb7..66200c0064 100644 --- a/django/db/backends/postgresql_psycopg2/base.py +++ b/django/db/backends/postgresql_psycopg2/base.py @@ -216,7 +216,7 @@ class DatabaseWrapper(BaseDatabaseWrapper): @cached_property def psycopg2_version(self): version = psycopg2.__version__.split(' ', 1)[0] - return tuple(int(v) for v in version.split('.')) + return tuple(int(v) for v in version.split('.') if v.isdigit()) @cached_property def pg_version(self): diff --git a/tests/backends/tests.py b/tests/backends/tests.py index c5c032fe05..d47ca43d1c 100644 --- a/tests/backends/tests.py +++ b/tests/backends/tests.py @@ -22,7 +22,7 @@ from django.db.backends.utils import format_number, CursorWrapper from django.db.models import Sum, Avg, Variance, StdDev from django.db.models.sql.constants import CURSOR from django.db.utils import ConnectionHandler -from django.test import (TestCase, TransactionTestCase, override_settings, +from django.test import (TestCase, TransactionTestCase, mock, override_settings, skipUnlessDBFeature, skipIfDBFeature) from django.test.utils import str_prefix, IgnoreAllDeprecationWarningsMixin from django.utils import six @@ -246,6 +246,16 @@ class PostgreSQLTests(TestCase): 'istartswith', 'endswith', 'iendswith', 'regex', 'iregex'): self.assertIn('::text', do.lookup_cast(lookup)) + def test_correct_extraction_psycopg2_version(self): + from django.db.backends.postgresql_psycopg2.base import DatabaseWrapper + version_path = 'django.db.backends.postgresql_psycopg2.base.Database.__version__' + + with mock.patch(version_path, '2.6.9'): + self.assertEqual(DatabaseWrapper.psycopg2_version.__get__(self), (2, 6, 9)) + + with mock.patch(version_path, '2.5.dev0'): + self.assertEqual(DatabaseWrapper.psycopg2_version.__get__(self), (2, 5)) + class DateQuotingTest(TestCase):