2017-02-11 20:37:49 +00:00
|
|
|
import unittest
|
|
|
|
from unittest import mock
|
|
|
|
|
2018-01-18 18:15:16 +00:00
|
|
|
from django.core.exceptions import ImproperlyConfigured
|
2018-04-03 20:21:47 +00:00
|
|
|
from django.db import DatabaseError, connection, connections
|
2017-02-11 20:37:49 +00:00
|
|
|
from django.test import TestCase
|
|
|
|
|
|
|
|
|
|
|
|
@unittest.skipUnless(connection.vendor == 'postgresql', 'PostgreSQL tests')
|
|
|
|
class Tests(TestCase):
|
|
|
|
|
|
|
|
def test_nodb_connection(self):
|
|
|
|
"""
|
|
|
|
The _nodb_connection property fallbacks to the default connection
|
|
|
|
database when access to the 'postgres' database is not granted.
|
|
|
|
"""
|
|
|
|
def mocked_connect(self):
|
|
|
|
if self.settings_dict['NAME'] is None:
|
|
|
|
raise DatabaseError()
|
|
|
|
return ''
|
|
|
|
|
|
|
|
nodb_conn = connection._nodb_connection
|
|
|
|
self.assertIsNone(nodb_conn.settings_dict['NAME'])
|
|
|
|
|
|
|
|
# Now assume the 'postgres' db isn't available
|
2018-04-27 21:18:15 +00:00
|
|
|
msg = (
|
|
|
|
"Normally Django will use a connection to the 'postgres' database "
|
|
|
|
"to avoid running initialization queries against the production "
|
|
|
|
"database when it's not needed (for example, when running tests). "
|
|
|
|
"Django was unable to create a connection to the 'postgres' "
|
|
|
|
"database and will use the first PostgreSQL database instead."
|
|
|
|
)
|
|
|
|
with self.assertWarnsMessage(RuntimeWarning, msg):
|
2017-02-11 20:37:49 +00:00
|
|
|
with mock.patch('django.db.backends.base.base.BaseDatabaseWrapper.connect',
|
|
|
|
side_effect=mocked_connect, autospec=True):
|
2018-04-03 20:21:47 +00:00
|
|
|
with mock.patch.object(
|
|
|
|
connection,
|
|
|
|
'settings_dict',
|
|
|
|
{**connection.settings_dict, 'NAME': 'postgres'},
|
|
|
|
):
|
|
|
|
nodb_conn = connection._nodb_connection
|
2017-02-11 20:37:49 +00:00
|
|
|
self.assertIsNotNone(nodb_conn.settings_dict['NAME'])
|
2018-04-03 20:21:47 +00:00
|
|
|
self.assertEqual(nodb_conn.settings_dict['NAME'], connections['other'].settings_dict['NAME'])
|
2017-02-11 20:37:49 +00:00
|
|
|
|
2018-01-18 18:15:16 +00:00
|
|
|
def test_database_name_too_long(self):
|
|
|
|
from django.db.backends.postgresql.base import DatabaseWrapper
|
|
|
|
settings = connection.settings_dict.copy()
|
|
|
|
max_name_length = connection.ops.max_name_length()
|
|
|
|
settings['NAME'] = 'a' + (max_name_length * 'a')
|
|
|
|
msg = (
|
2018-08-17 16:43:56 +00:00
|
|
|
"The database name '%s' (%d characters) is longer than "
|
|
|
|
"PostgreSQL's limit of %s characters. Supply a shorter NAME in "
|
|
|
|
"settings.DATABASES."
|
|
|
|
) % (settings['NAME'], max_name_length + 1, max_name_length)
|
2018-01-18 18:15:16 +00:00
|
|
|
with self.assertRaisesMessage(ImproperlyConfigured, msg):
|
|
|
|
DatabaseWrapper(settings).get_connection_params()
|
|
|
|
|
2017-02-11 20:37:49 +00:00
|
|
|
def test_connect_and_rollback(self):
|
|
|
|
"""
|
|
|
|
PostgreSQL shouldn't roll back SET TIME ZONE, even if the first
|
|
|
|
transaction is rolled back (#17062).
|
|
|
|
"""
|
|
|
|
new_connection = connection.copy()
|
|
|
|
try:
|
|
|
|
# Ensure the database default time zone is different than
|
|
|
|
# the time zone in new_connection.settings_dict. We can
|
|
|
|
# get the default time zone by reset & show.
|
2017-11-28 13:12:28 +00:00
|
|
|
with new_connection.cursor() as cursor:
|
|
|
|
cursor.execute("RESET TIMEZONE")
|
|
|
|
cursor.execute("SHOW TIMEZONE")
|
|
|
|
db_default_tz = cursor.fetchone()[0]
|
2017-02-11 20:37:49 +00:00
|
|
|
new_tz = 'Europe/Paris' if db_default_tz == 'UTC' else 'UTC'
|
|
|
|
new_connection.close()
|
|
|
|
|
|
|
|
# Invalidate timezone name cache, because the setting_changed
|
|
|
|
# handler cannot know about new_connection.
|
|
|
|
del new_connection.timezone_name
|
|
|
|
|
|
|
|
# Fetch a new connection with the new_tz as default
|
|
|
|
# time zone, run a query and rollback.
|
|
|
|
with self.settings(TIME_ZONE=new_tz):
|
|
|
|
new_connection.set_autocommit(False)
|
|
|
|
new_connection.rollback()
|
|
|
|
|
|
|
|
# Now let's see if the rollback rolled back the SET TIME ZONE.
|
2017-11-28 13:12:28 +00:00
|
|
|
with new_connection.cursor() as cursor:
|
|
|
|
cursor.execute("SHOW TIMEZONE")
|
|
|
|
tz = cursor.fetchone()[0]
|
2017-02-11 20:37:49 +00:00
|
|
|
self.assertEqual(new_tz, tz)
|
|
|
|
|
|
|
|
finally:
|
|
|
|
new_connection.close()
|
|
|
|
|
|
|
|
def test_connect_non_autocommit(self):
|
|
|
|
"""
|
|
|
|
The connection wrapper shouldn't believe that autocommit is enabled
|
|
|
|
after setting the time zone when AUTOCOMMIT is False (#21452).
|
|
|
|
"""
|
|
|
|
new_connection = connection.copy()
|
|
|
|
new_connection.settings_dict['AUTOCOMMIT'] = False
|
|
|
|
|
|
|
|
try:
|
|
|
|
# Open a database connection.
|
|
|
|
new_connection.cursor()
|
|
|
|
self.assertFalse(new_connection.get_autocommit())
|
|
|
|
finally:
|
|
|
|
new_connection.close()
|
|
|
|
|
|
|
|
def test_connect_isolation_level(self):
|
|
|
|
"""
|
|
|
|
The transaction level can be configured with
|
|
|
|
DATABASES ['OPTIONS']['isolation_level'].
|
|
|
|
"""
|
|
|
|
import psycopg2
|
|
|
|
from psycopg2.extensions import (
|
|
|
|
ISOLATION_LEVEL_READ_COMMITTED as read_committed,
|
|
|
|
ISOLATION_LEVEL_SERIALIZABLE as serializable,
|
|
|
|
)
|
|
|
|
# Since this is a django.test.TestCase, a transaction is in progress
|
|
|
|
# and the isolation level isn't reported as 0. This test assumes that
|
|
|
|
# PostgreSQL is configured with the default isolation level.
|
|
|
|
|
|
|
|
# Check the level on the psycopg2 connection, not the Django wrapper.
|
|
|
|
default_level = read_committed if psycopg2.__version__ < '2.7' else None
|
|
|
|
self.assertEqual(connection.connection.isolation_level, default_level)
|
|
|
|
|
|
|
|
new_connection = connection.copy()
|
|
|
|
new_connection.settings_dict['OPTIONS']['isolation_level'] = serializable
|
|
|
|
try:
|
|
|
|
# Start a transaction so the isolation level isn't reported as 0.
|
|
|
|
new_connection.set_autocommit(False)
|
|
|
|
# Check the level on the psycopg2 connection, not the Django wrapper.
|
|
|
|
self.assertEqual(new_connection.connection.isolation_level, serializable)
|
|
|
|
finally:
|
|
|
|
new_connection.close()
|
|
|
|
|
2019-02-20 09:59:40 +00:00
|
|
|
def test_connect_no_is_usable_checks(self):
|
|
|
|
new_connection = connection.copy()
|
|
|
|
with mock.patch.object(new_connection, 'is_usable') as is_usable:
|
|
|
|
new_connection.connect()
|
|
|
|
is_usable.assert_not_called()
|
|
|
|
|
2017-02-11 20:37:49 +00:00
|
|
|
def _select(self, val):
|
|
|
|
with connection.cursor() as cursor:
|
|
|
|
cursor.execute('SELECT %s', (val,))
|
|
|
|
return cursor.fetchone()[0]
|
|
|
|
|
|
|
|
def test_select_ascii_array(self):
|
|
|
|
a = ['awef']
|
|
|
|
b = self._select(a)
|
|
|
|
self.assertEqual(a[0], b[0])
|
|
|
|
|
|
|
|
def test_select_unicode_array(self):
|
|
|
|
a = ['ᄲawef']
|
|
|
|
b = self._select(a)
|
|
|
|
self.assertEqual(a[0], b[0])
|
|
|
|
|
|
|
|
def test_lookup_cast(self):
|
|
|
|
from django.db.backends.postgresql.operations import DatabaseOperations
|
|
|
|
do = DatabaseOperations(connection=None)
|
|
|
|
lookups = (
|
|
|
|
'iexact', 'contains', 'icontains', 'startswith', 'istartswith',
|
|
|
|
'endswith', 'iendswith', 'regex', 'iregex',
|
|
|
|
)
|
|
|
|
for lookup in lookups:
|
|
|
|
with self.subTest(lookup=lookup):
|
|
|
|
self.assertIn('::text', do.lookup_cast(lookup))
|
2017-11-14 21:51:51 +00:00
|
|
|
for lookup in lookups:
|
|
|
|
for field_type in ('CICharField', 'CIEmailField', 'CITextField'):
|
|
|
|
with self.subTest(lookup=lookup, field_type=field_type):
|
|
|
|
self.assertIn('::citext', do.lookup_cast(lookup, internal_type=field_type))
|
2017-02-11 20:37:49 +00:00
|
|
|
|
|
|
|
def test_correct_extraction_psycopg2_version(self):
|
|
|
|
from django.db.backends.postgresql.base import psycopg2_version
|
|
|
|
with mock.patch('psycopg2.__version__', '4.2.1 (dt dec pq3 ext lo64)'):
|
|
|
|
self.assertEqual(psycopg2_version(), (4, 2, 1))
|
|
|
|
with mock.patch('psycopg2.__version__', '4.2b0.dev1 (dt dec pq3 ext lo64)'):
|
|
|
|
self.assertEqual(psycopg2_version(), (4, 2))
|