2008-01-19 06:32:42 +03:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
2011-01-07 04:44:19 +03:00
|
|
|
# test_connection.py - unit test for connection attributes
|
|
|
|
#
|
2019-02-17 04:34:52 +03:00
|
|
|
# Copyright (C) 2008-2019 James Henstridge <james@jamesh.id.au>
|
2020-01-18 00:10:44 +03:00
|
|
|
# Copyright (C) 2020 The Psycopg Team
|
2011-01-07 04:44:19 +03:00
|
|
|
#
|
|
|
|
# psycopg2 is free software: you can redistribute it and/or modify it
|
|
|
|
# under the terms of the GNU Lesser General Public License as published
|
|
|
|
# by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# In addition, as a special exception, the copyright holders give
|
|
|
|
# permission to link this program with the OpenSSL library (or with
|
|
|
|
# modified versions of OpenSSL that use the same license as OpenSSL),
|
|
|
|
# and distribute linked combinations including the two.
|
|
|
|
#
|
|
|
|
# You must obey the GNU Lesser General Public License in all respects for
|
|
|
|
# all of the code used other than OpenSSL.
|
|
|
|
#
|
|
|
|
# psycopg2 is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
|
|
|
|
# License for more details.
|
|
|
|
|
2019-03-16 18:30:15 +03:00
|
|
|
import gc
|
2011-04-08 14:27:45 +04:00
|
|
|
import os
|
2019-03-16 18:30:15 +03:00
|
|
|
import re
|
2015-10-01 15:20:11 +03:00
|
|
|
import sys
|
2019-03-16 18:30:15 +03:00
|
|
|
import time
|
2019-03-18 02:55:04 +03:00
|
|
|
import ctypes
|
|
|
|
import shutil
|
|
|
|
import tempfile
|
|
|
|
import threading
|
|
|
|
import subprocess as sp
|
2019-03-16 18:30:15 +03:00
|
|
|
from collections import deque
|
2010-10-11 16:03:37 +04:00
|
|
|
from operator import attrgetter
|
2019-03-16 18:30:15 +03:00
|
|
|
from weakref import ref
|
2010-10-11 16:03:37 +04:00
|
|
|
|
2008-01-19 06:32:42 +03:00
|
|
|
import psycopg2
|
2019-03-16 21:56:56 +03:00
|
|
|
import psycopg2.extras
|
2016-10-11 02:10:53 +03:00
|
|
|
from psycopg2 import extensions as ext
|
|
|
|
|
2017-12-04 05:47:19 +03:00
|
|
|
from .testutils import (
|
2019-03-16 20:15:16 +03:00
|
|
|
PY2, unittest, skip_if_no_superuser, skip_before_postgres,
|
2018-08-17 02:55:40 +03:00
|
|
|
skip_after_postgres, skip_before_libpq, skip_after_libpq,
|
|
|
|
ConnectingTestCase, skip_if_tpc_disabled, skip_if_windows, slow)
|
2008-01-19 06:32:42 +03:00
|
|
|
|
2018-10-05 10:23:13 +03:00
|
|
|
from .testconfig import dbhost, dsn, dbname
|
2010-11-28 19:00:32 +03:00
|
|
|
|
2008-01-19 06:32:42 +03:00
|
|
|
|
2013-04-07 03:23:30 +04:00
|
|
|
class ConnectionTests(ConnectingTestCase):
|
2008-01-19 06:32:42 +03:00
|
|
|
def test_closed_attribute(self):
|
2010-11-28 19:00:32 +03:00
|
|
|
conn = self.conn
|
2008-01-19 06:32:42 +03:00
|
|
|
self.assertEqual(conn.closed, False)
|
|
|
|
conn.close()
|
|
|
|
self.assertEqual(conn.closed, True)
|
|
|
|
|
2012-03-04 09:03:15 +04:00
|
|
|
def test_close_idempotent(self):
|
|
|
|
conn = self.conn
|
|
|
|
conn.close()
|
|
|
|
conn.close()
|
|
|
|
self.assert_(conn.closed)
|
|
|
|
|
2008-01-19 06:32:42 +03:00
|
|
|
def test_cursor_closed_attribute(self):
|
2010-11-28 19:00:32 +03:00
|
|
|
conn = self.conn
|
2008-01-19 06:32:42 +03:00
|
|
|
curs = conn.cursor()
|
|
|
|
self.assertEqual(curs.closed, False)
|
|
|
|
curs.close()
|
|
|
|
self.assertEqual(curs.closed, True)
|
|
|
|
|
|
|
|
# Closing the connection closes the cursor:
|
|
|
|
curs = conn.cursor()
|
|
|
|
conn.close()
|
|
|
|
self.assertEqual(curs.closed, True)
|
|
|
|
|
2013-03-16 15:56:38 +04:00
|
|
|
@skip_before_postgres(8, 4)
|
|
|
|
@skip_if_no_superuser
|
2014-05-13 02:12:50 +04:00
|
|
|
@skip_if_windows
|
2013-03-16 15:56:38 +04:00
|
|
|
def test_cleanup_on_badconn_close(self):
|
|
|
|
# ticket #148
|
|
|
|
conn = self.conn
|
|
|
|
cur = conn.cursor()
|
2016-07-04 23:49:45 +03:00
|
|
|
self.assertRaises(psycopg2.OperationalError,
|
|
|
|
cur.execute, "select pg_terminate_backend(pg_backend_pid())")
|
2013-03-16 15:56:38 +04:00
|
|
|
|
|
|
|
self.assertEqual(conn.closed, 2)
|
|
|
|
conn.close()
|
|
|
|
self.assertEqual(conn.closed, 1)
|
|
|
|
|
2009-08-09 18:19:08 +04:00
|
|
|
def test_reset(self):
|
2010-11-28 19:00:32 +03:00
|
|
|
conn = self.conn
|
2017-02-16 14:04:02 +03:00
|
|
|
# switch session characteristics
|
|
|
|
conn.autocommit = True
|
|
|
|
conn.isolation_level = 'serializable'
|
|
|
|
conn.readonly = True
|
2018-10-13 05:28:42 +03:00
|
|
|
if self.conn.info.server_version >= 90100:
|
2017-02-16 14:04:02 +03:00
|
|
|
conn.deferrable = False
|
|
|
|
|
|
|
|
self.assert_(conn.autocommit)
|
|
|
|
self.assertEqual(conn.isolation_level, ext.ISOLATION_LEVEL_SERIALIZABLE)
|
|
|
|
self.assert_(conn.readonly is True)
|
2018-10-13 05:28:42 +03:00
|
|
|
if self.conn.info.server_version >= 90100:
|
2017-02-16 14:04:02 +03:00
|
|
|
self.assert_(conn.deferrable is False)
|
|
|
|
|
2009-08-09 18:19:08 +04:00
|
|
|
conn.reset()
|
2017-02-16 14:04:02 +03:00
|
|
|
# now the session characteristics should be reverted
|
|
|
|
self.assert_(not conn.autocommit)
|
|
|
|
self.assertEqual(conn.isolation_level, ext.ISOLATION_LEVEL_DEFAULT)
|
|
|
|
self.assert_(conn.readonly is None)
|
2018-10-13 05:28:42 +03:00
|
|
|
if self.conn.info.server_version >= 90100:
|
2017-02-16 14:04:02 +03:00
|
|
|
self.assert_(conn.deferrable is None)
|
2009-08-09 18:19:08 +04:00
|
|
|
|
2010-04-20 20:52:05 +04:00
|
|
|
def test_notices(self):
|
2010-11-28 19:00:32 +03:00
|
|
|
conn = self.conn
|
2010-04-20 20:52:05 +04:00
|
|
|
cur = conn.cursor()
|
2018-10-13 05:28:42 +03:00
|
|
|
if self.conn.info.server_version >= 90300:
|
2013-03-15 21:10:13 +04:00
|
|
|
cur.execute("set client_min_messages=debug1")
|
2010-04-20 20:52:05 +04:00
|
|
|
cur.execute("create temp table chatty (id serial primary key);")
|
|
|
|
self.assertEqual("CREATE TABLE", cur.statusmessage)
|
|
|
|
self.assert_(conn.notices)
|
2008-01-19 06:32:42 +03:00
|
|
|
|
2010-11-11 14:54:43 +03:00
|
|
|
def test_notices_consistent_order(self):
|
2010-11-28 19:00:32 +03:00
|
|
|
conn = self.conn
|
2010-11-11 14:54:43 +03:00
|
|
|
cur = conn.cursor()
|
2018-10-13 05:28:42 +03:00
|
|
|
if self.conn.info.server_version >= 90300:
|
2013-03-15 21:10:13 +04:00
|
|
|
cur.execute("set client_min_messages=debug1")
|
2016-10-11 02:10:53 +03:00
|
|
|
cur.execute("""
|
|
|
|
create temp table table1 (id serial);
|
|
|
|
create temp table table2 (id serial);
|
|
|
|
""")
|
|
|
|
cur.execute("""
|
|
|
|
create temp table table3 (id serial);
|
|
|
|
create temp table table4 (id serial);
|
|
|
|
""")
|
2010-11-11 14:54:43 +03:00
|
|
|
self.assertEqual(4, len(conn.notices))
|
|
|
|
self.assert_('table1' in conn.notices[0])
|
|
|
|
self.assert_('table2' in conn.notices[1])
|
|
|
|
self.assert_('table3' in conn.notices[2])
|
|
|
|
self.assert_('table4' in conn.notices[3])
|
|
|
|
|
2017-02-02 05:58:22 +03:00
|
|
|
@slow
|
2010-11-11 14:54:43 +03:00
|
|
|
def test_notices_limited(self):
|
2010-11-28 19:00:32 +03:00
|
|
|
conn = self.conn
|
2010-11-11 14:54:43 +03:00
|
|
|
cur = conn.cursor()
|
2018-10-13 05:28:42 +03:00
|
|
|
if self.conn.info.server_version >= 90300:
|
2013-03-15 21:10:13 +04:00
|
|
|
cur.execute("set client_min_messages=debug1")
|
2010-11-11 14:54:43 +03:00
|
|
|
for i in range(0, 100, 10):
|
2016-10-11 02:10:53 +03:00
|
|
|
sql = " ".join(["create temp table table%d (id serial);" % j
|
|
|
|
for j in range(i, i + 10)])
|
2010-11-11 14:54:43 +03:00
|
|
|
cur.execute(sql)
|
|
|
|
|
|
|
|
self.assertEqual(50, len(conn.notices))
|
|
|
|
self.assert_('table99' in conn.notices[-1], conn.notices[-1])
|
|
|
|
|
2017-02-02 05:58:22 +03:00
|
|
|
@slow
|
2015-06-02 19:02:04 +03:00
|
|
|
def test_notices_deque(self):
|
|
|
|
conn = self.conn
|
|
|
|
self.conn.notices = deque()
|
|
|
|
cur = conn.cursor()
|
2018-10-13 05:28:42 +03:00
|
|
|
if self.conn.info.server_version >= 90300:
|
2015-06-02 19:02:04 +03:00
|
|
|
cur.execute("set client_min_messages=debug1")
|
|
|
|
|
2016-10-11 02:10:53 +03:00
|
|
|
cur.execute("""
|
|
|
|
create temp table table1 (id serial);
|
|
|
|
create temp table table2 (id serial);
|
|
|
|
""")
|
|
|
|
cur.execute("""
|
|
|
|
create temp table table3 (id serial);
|
|
|
|
create temp table table4 (id serial);""")
|
2015-06-02 19:02:04 +03:00
|
|
|
self.assertEqual(len(conn.notices), 4)
|
|
|
|
self.assert_('table1' in conn.notices.popleft())
|
|
|
|
self.assert_('table2' in conn.notices.popleft())
|
|
|
|
self.assert_('table3' in conn.notices.popleft())
|
|
|
|
self.assert_('table4' in conn.notices.popleft())
|
|
|
|
self.assertEqual(len(conn.notices), 0)
|
|
|
|
|
|
|
|
# not limited, but no error
|
|
|
|
for i in range(0, 100, 10):
|
2016-10-11 02:10:53 +03:00
|
|
|
sql = " ".join(["create temp table table2_%d (id serial);" % j
|
|
|
|
for j in range(i, i + 10)])
|
2015-06-02 19:02:04 +03:00
|
|
|
cur.execute(sql)
|
|
|
|
|
2015-10-01 15:31:13 +03:00
|
|
|
self.assertEqual(len([n for n in conn.notices if 'CREATE TABLE' in n]),
|
|
|
|
100)
|
2015-06-02 19:02:04 +03:00
|
|
|
|
|
|
|
def test_notices_noappend(self):
|
|
|
|
conn = self.conn
|
|
|
|
self.conn.notices = None # will make an error swallowes ok
|
|
|
|
cur = conn.cursor()
|
2018-10-13 05:28:42 +03:00
|
|
|
if self.conn.info.server_version >= 90300:
|
2015-06-02 19:02:04 +03:00
|
|
|
cur.execute("set client_min_messages=debug1")
|
|
|
|
|
|
|
|
cur.execute("create temp table table1 (id serial);")
|
|
|
|
|
|
|
|
self.assertEqual(self.conn.notices, None)
|
|
|
|
|
2010-04-21 16:22:09 +04:00
|
|
|
def test_server_version(self):
|
2010-11-28 19:00:32 +03:00
|
|
|
self.assert_(self.conn.server_version)
|
2010-04-21 16:22:09 +04:00
|
|
|
|
2010-04-21 21:40:05 +04:00
|
|
|
def test_protocol_version(self):
|
2016-03-03 18:31:37 +03:00
|
|
|
self.assert_(self.conn.protocol_version in (2, 3),
|
2010-11-28 19:00:32 +03:00
|
|
|
self.conn.protocol_version)
|
2010-04-21 21:40:05 +04:00
|
|
|
|
2010-11-19 06:55:37 +03:00
|
|
|
def test_tpc_unsupported(self):
|
2010-11-28 19:00:32 +03:00
|
|
|
cnn = self.conn
|
2018-10-13 05:28:42 +03:00
|
|
|
if cnn.info.server_version >= 80100:
|
2010-11-19 06:55:37 +03:00
|
|
|
return self.skipTest("tpc is supported")
|
|
|
|
|
|
|
|
self.assertRaises(psycopg2.NotSupportedError,
|
|
|
|
cnn.xid, 42, "foo", "bar")
|
|
|
|
|
2017-02-02 04:53:50 +03:00
|
|
|
@slow
|
2011-02-15 20:11:07 +03:00
|
|
|
@skip_before_postgres(8, 2)
|
2010-11-28 16:42:06 +03:00
|
|
|
def test_concurrent_execution(self):
|
2010-11-28 19:00:32 +03:00
|
|
|
def slave():
|
2013-04-07 03:23:30 +04:00
|
|
|
cnn = self.connect()
|
2010-11-28 16:42:06 +03:00
|
|
|
cur = cnn.cursor()
|
2011-02-18 21:52:17 +03:00
|
|
|
cur.execute("select pg_sleep(4)")
|
2010-11-28 16:42:06 +03:00
|
|
|
cur.close()
|
2010-11-28 19:00:32 +03:00
|
|
|
cnn.close()
|
2010-11-28 16:42:06 +03:00
|
|
|
|
2010-11-28 19:00:32 +03:00
|
|
|
t1 = threading.Thread(target=slave)
|
|
|
|
t2 = threading.Thread(target=slave)
|
2010-11-28 16:42:06 +03:00
|
|
|
t0 = time.time()
|
|
|
|
t1.start()
|
|
|
|
t2.start()
|
|
|
|
t1.join()
|
|
|
|
t2.join()
|
2011-02-18 21:52:17 +03:00
|
|
|
self.assert_(time.time() - t0 < 7,
|
2010-11-28 16:42:06 +03:00
|
|
|
"something broken in concurrency")
|
2010-11-18 03:20:39 +03:00
|
|
|
|
2011-02-11 01:59:31 +03:00
|
|
|
def test_encoding_name(self):
|
|
|
|
self.conn.set_client_encoding("EUC_JP")
|
|
|
|
# conn.encoding is 'EUCJP' now.
|
|
|
|
cur = self.conn.cursor()
|
2017-02-16 05:07:05 +03:00
|
|
|
ext.register_type(ext.UNICODE, cur)
|
2011-02-11 01:59:31 +03:00
|
|
|
cur.execute("select 'foo'::text;")
|
|
|
|
self.assertEqual(cur.fetchone()[0], u'foo')
|
|
|
|
|
2011-04-08 14:27:45 +04:00
|
|
|
def test_connect_nonnormal_envvar(self):
|
|
|
|
# We must perform encoding normalization at connection time
|
|
|
|
self.conn.close()
|
|
|
|
oldenc = os.environ.get('PGCLIENTENCODING')
|
|
|
|
os.environ['PGCLIENTENCODING'] = 'utf-8' # malformed spelling
|
|
|
|
try:
|
2013-04-07 03:23:30 +04:00
|
|
|
self.conn = self.connect()
|
2011-04-08 14:27:45 +04:00
|
|
|
finally:
|
|
|
|
if oldenc is not None:
|
|
|
|
os.environ['PGCLIENTENCODING'] = oldenc
|
|
|
|
else:
|
|
|
|
del os.environ['PGCLIENTENCODING']
|
|
|
|
|
2018-02-20 20:33:12 +03:00
|
|
|
def test_connect_no_string(self):
|
|
|
|
class MyString(str):
|
|
|
|
pass
|
|
|
|
|
|
|
|
conn = psycopg2.connect(MyString(dsn))
|
|
|
|
conn.close()
|
|
|
|
|
2011-01-03 13:52:38 +03:00
|
|
|
def test_weakref(self):
|
2011-02-11 01:59:31 +03:00
|
|
|
conn = psycopg2.connect(dsn)
|
2011-01-03 13:52:38 +03:00
|
|
|
w = ref(conn)
|
|
|
|
conn.close()
|
|
|
|
del conn
|
2011-12-27 01:35:33 +04:00
|
|
|
gc.collect()
|
2011-01-03 13:52:38 +03:00
|
|
|
self.assert_(w() is None)
|
|
|
|
|
2017-02-02 04:53:50 +03:00
|
|
|
@slow
|
2012-02-24 07:04:24 +04:00
|
|
|
def test_commit_concurrency(self):
|
|
|
|
# The problem is the one reported in ticket #103. Because of bad
|
|
|
|
# status check, we commit even when a commit is already on its way.
|
|
|
|
# We can detect this condition by the warnings.
|
|
|
|
conn = self.conn
|
|
|
|
notices = []
|
|
|
|
stop = []
|
|
|
|
|
|
|
|
def committer():
|
|
|
|
while not stop:
|
|
|
|
conn.commit()
|
|
|
|
while conn.notices:
|
|
|
|
notices.append((2, conn.notices.pop()))
|
|
|
|
|
|
|
|
cur = conn.cursor()
|
|
|
|
t1 = threading.Thread(target=committer)
|
|
|
|
t1.start()
|
|
|
|
for i in range(1000):
|
2016-03-03 18:31:37 +03:00
|
|
|
cur.execute("select %s;", (i,))
|
2012-02-24 07:04:24 +04:00
|
|
|
conn.commit()
|
|
|
|
while conn.notices:
|
|
|
|
notices.append((1, conn.notices.pop()))
|
|
|
|
|
|
|
|
# Stop the committer thread
|
|
|
|
stop.append(True)
|
|
|
|
|
|
|
|
self.assert_(not notices, "%d notices raised" % len(notices))
|
|
|
|
|
2013-04-07 05:30:12 +04:00
|
|
|
def test_connect_cursor_factory(self):
|
|
|
|
conn = self.connect(cursor_factory=psycopg2.extras.DictCursor)
|
|
|
|
cur = conn.cursor()
|
|
|
|
cur.execute("select 1 as a")
|
|
|
|
self.assertEqual(cur.fetchone()['a'], 1)
|
|
|
|
|
|
|
|
def test_cursor_factory(self):
|
|
|
|
self.assertEqual(self.conn.cursor_factory, None)
|
|
|
|
cur = self.conn.cursor()
|
|
|
|
cur.execute("select 1 as a")
|
|
|
|
self.assertRaises(TypeError, (lambda r: r['a']), cur.fetchone())
|
|
|
|
|
|
|
|
self.conn.cursor_factory = psycopg2.extras.DictCursor
|
|
|
|
self.assertEqual(self.conn.cursor_factory, psycopg2.extras.DictCursor)
|
|
|
|
cur = self.conn.cursor()
|
|
|
|
cur.execute("select 1 as a")
|
|
|
|
self.assertEqual(cur.fetchone()['a'], 1)
|
|
|
|
|
|
|
|
self.conn.cursor_factory = None
|
|
|
|
self.assertEqual(self.conn.cursor_factory, None)
|
|
|
|
cur = self.conn.cursor()
|
|
|
|
cur.execute("select 1 as a")
|
|
|
|
self.assertRaises(TypeError, (lambda r: r['a']), cur.fetchone())
|
|
|
|
|
2014-04-30 20:56:09 +04:00
|
|
|
def test_cursor_factory_none(self):
|
|
|
|
# issue #210
|
|
|
|
conn = self.connect()
|
|
|
|
cur = conn.cursor(cursor_factory=None)
|
2017-02-16 05:07:05 +03:00
|
|
|
self.assertEqual(type(cur), ext.cursor)
|
2014-04-30 20:56:09 +04:00
|
|
|
|
|
|
|
conn = self.connect(cursor_factory=psycopg2.extras.DictCursor)
|
|
|
|
cur = conn.cursor(cursor_factory=None)
|
|
|
|
self.assertEqual(type(cur), psycopg2.extras.DictCursor)
|
|
|
|
|
2014-04-03 04:41:19 +04:00
|
|
|
def test_failed_init_status(self):
|
2017-02-16 05:07:05 +03:00
|
|
|
class SubConnection(ext.connection):
|
2014-04-03 04:41:19 +04:00
|
|
|
def __init__(self, dsn):
|
|
|
|
try:
|
|
|
|
super(SubConnection, self).__init__(dsn)
|
|
|
|
except Exception:
|
|
|
|
pass
|
|
|
|
|
|
|
|
c = SubConnection("dbname=thereisnosuchdatabasemate password=foobar")
|
|
|
|
self.assert_(c.closed, "connection failed so it must be closed")
|
|
|
|
self.assert_('foobar' not in c.dsn, "password was not obscured")
|
2014-04-03 05:28:02 +04:00
|
|
|
|
2018-10-07 14:54:24 +03:00
|
|
|
def test_get_native_connection(self):
|
|
|
|
conn = self.connect()
|
|
|
|
capsule = conn.get_native_connection()
|
|
|
|
# we can't do anything else in Python
|
|
|
|
self.assertIsNotNone(capsule)
|
2015-10-01 13:52:42 +03:00
|
|
|
|
2019-02-16 19:30:34 +03:00
|
|
|
def test_pgconn_ptr(self):
|
|
|
|
conn = self.connect()
|
|
|
|
self.assert_(conn.pgconn_ptr is not None)
|
|
|
|
|
2019-03-18 22:49:16 +03:00
|
|
|
try:
|
|
|
|
f = self.libpq.PQserverVersion
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
f.argtypes = [ctypes.c_void_p]
|
|
|
|
f.restype = ctypes.c_int
|
|
|
|
ver = f(conn.pgconn_ptr)
|
|
|
|
if ver == 0 and sys.platform == 'darwin':
|
|
|
|
return self.skipTest(
|
|
|
|
"I don't know why this func returns 0 on OSX")
|
|
|
|
|
|
|
|
self.assertEqual(ver, conn.server_version)
|
2019-02-16 19:30:34 +03:00
|
|
|
|
|
|
|
conn.close()
|
|
|
|
self.assert_(conn.pgconn_ptr is None)
|
|
|
|
|
2019-03-18 02:06:55 +03:00
|
|
|
@slow
|
|
|
|
def test_multiprocess_close(self):
|
2019-03-18 02:55:04 +03:00
|
|
|
dir = tempfile.mkdtemp()
|
|
|
|
try:
|
|
|
|
with open(os.path.join(dir, "mptest.py"), 'w') as f:
|
|
|
|
f.write("""\
|
2019-03-18 02:06:55 +03:00
|
|
|
import time
|
|
|
|
import psycopg2
|
|
|
|
|
|
|
|
def thread():
|
|
|
|
conn = psycopg2.connect(%(dsn)r)
|
|
|
|
curs = conn.cursor()
|
|
|
|
for i in range(10):
|
|
|
|
curs.execute("select 1")
|
|
|
|
time.sleep(0.1)
|
|
|
|
|
|
|
|
def process():
|
|
|
|
time.sleep(0.2)
|
2019-03-18 02:55:04 +03:00
|
|
|
""" % {'dsn': dsn})
|
|
|
|
|
|
|
|
script = ("""\
|
|
|
|
import sys
|
|
|
|
sys.path.insert(0, %(dir)r)
|
|
|
|
import time
|
|
|
|
import threading
|
|
|
|
import multiprocessing
|
|
|
|
import mptest
|
2019-03-18 02:06:55 +03:00
|
|
|
|
2019-03-18 02:55:04 +03:00
|
|
|
t = threading.Thread(target=mptest.thread, name='mythread')
|
2019-03-18 02:06:55 +03:00
|
|
|
t.start()
|
|
|
|
time.sleep(0.2)
|
2019-03-18 02:55:04 +03:00
|
|
|
multiprocessing.Process(target=mptest.process, name='myprocess').start()
|
2019-03-18 02:06:55 +03:00
|
|
|
t.join()
|
2019-03-18 02:55:04 +03:00
|
|
|
""" % {'dir': dir})
|
2019-03-18 02:06:55 +03:00
|
|
|
|
2019-03-18 02:55:04 +03:00
|
|
|
out = sp.check_output(
|
|
|
|
[sys.executable, '-c', script], stderr=sp.STDOUT)
|
|
|
|
self.assertEqual(out, b'', out)
|
|
|
|
finally:
|
|
|
|
shutil.rmtree(dir, ignore_errors=True)
|
2019-03-18 02:06:55 +03:00
|
|
|
|
2019-02-16 19:30:34 +03:00
|
|
|
|
2015-10-01 13:52:42 +03:00
|
|
|
class ParseDsnTestCase(ConnectingTestCase):
|
2015-06-01 16:11:12 +03:00
|
|
|
def test_parse_dsn(self):
|
2016-10-11 02:10:53 +03:00
|
|
|
self.assertEqual(
|
|
|
|
ext.parse_dsn('dbname=test user=tester password=secret'),
|
|
|
|
dict(user='tester', password='secret', dbname='test'),
|
|
|
|
"simple DSN parsed")
|
2015-06-01 16:11:12 +03:00
|
|
|
|
2019-03-16 21:56:56 +03:00
|
|
|
self.assertRaises(psycopg2.ProgrammingError, ext.parse_dsn,
|
2015-06-01 16:18:03 +03:00
|
|
|
"dbname=test 2 user=tester password=secret")
|
|
|
|
|
2016-10-11 02:10:53 +03:00
|
|
|
self.assertEqual(
|
|
|
|
ext.parse_dsn("dbname='test 2' user=tester password=secret"),
|
|
|
|
dict(user='tester', password='secret', dbname='test 2'),
|
|
|
|
"DSN with quoting parsed")
|
2015-06-01 16:11:12 +03:00
|
|
|
|
|
|
|
# Can't really use assertRaisesRegexp() here since we need to
|
2017-11-28 11:11:42 +03:00
|
|
|
# make sure that secret is *not* exposed in the error message.
|
2015-06-01 16:11:12 +03:00
|
|
|
raised = False
|
|
|
|
try:
|
|
|
|
# unterminated quote after dbname:
|
2016-03-03 18:31:37 +03:00
|
|
|
ext.parse_dsn("dbname='test 2 user=tester password=secret")
|
2019-03-16 21:56:56 +03:00
|
|
|
except psycopg2.ProgrammingError as e:
|
2015-06-01 16:11:12 +03:00
|
|
|
raised = True
|
2015-10-01 14:00:33 +03:00
|
|
|
self.assertTrue(str(e).find('secret') < 0,
|
2015-06-01 16:11:12 +03:00
|
|
|
"DSN was not exposed in error message")
|
|
|
|
self.assertTrue(raised, "ProgrammingError raised due to invalid DSN")
|
|
|
|
|
2015-06-02 15:02:29 +03:00
|
|
|
@skip_before_libpq(9, 2)
|
|
|
|
def test_parse_dsn_uri(self):
|
2016-03-03 18:31:37 +03:00
|
|
|
self.assertEqual(ext.parse_dsn('postgresql://tester:secret@/test'),
|
2015-06-02 15:02:29 +03:00
|
|
|
dict(user='tester', password='secret', dbname='test'),
|
|
|
|
"valid URI dsn parsed")
|
|
|
|
|
|
|
|
raised = False
|
|
|
|
try:
|
|
|
|
# extra '=' after port value
|
2016-03-03 18:31:37 +03:00
|
|
|
ext.parse_dsn(dsn='postgresql://tester:secret@/test?port=1111=x')
|
2017-11-21 07:00:35 +03:00
|
|
|
except psycopg2.ProgrammingError as e:
|
2015-06-02 15:02:29 +03:00
|
|
|
raised = True
|
2015-10-01 14:00:33 +03:00
|
|
|
self.assertTrue(str(e).find('secret') < 0,
|
2015-06-02 15:02:29 +03:00
|
|
|
"URI was not exposed in error message")
|
|
|
|
self.assertTrue(raised, "ProgrammingError raised due to invalid URI")
|
|
|
|
|
2015-10-01 15:20:11 +03:00
|
|
|
def test_unicode_value(self):
|
|
|
|
snowman = u"\u2603"
|
2016-03-03 18:31:37 +03:00
|
|
|
d = ext.parse_dsn('dbname=' + snowman)
|
2019-03-16 20:15:16 +03:00
|
|
|
if PY2:
|
2015-10-01 15:20:11 +03:00
|
|
|
self.assertEqual(d['dbname'], snowman.encode('utf8'))
|
|
|
|
else:
|
|
|
|
self.assertEqual(d['dbname'], snowman)
|
|
|
|
|
|
|
|
def test_unicode_key(self):
|
|
|
|
snowman = u"\u2603"
|
2016-03-03 18:31:37 +03:00
|
|
|
self.assertRaises(psycopg2.ProgrammingError, ext.parse_dsn,
|
2015-10-01 15:20:11 +03:00
|
|
|
snowman + '=' + snowman)
|
|
|
|
|
|
|
|
def test_bad_param(self):
|
2016-03-03 18:31:37 +03:00
|
|
|
self.assertRaises(TypeError, ext.parse_dsn, None)
|
|
|
|
self.assertRaises(TypeError, ext.parse_dsn, 42)
|
|
|
|
|
2018-02-20 20:33:12 +03:00
|
|
|
def test_str_subclass(self):
|
|
|
|
class MyString(str):
|
|
|
|
pass
|
|
|
|
|
|
|
|
res = ext.parse_dsn(MyString("dbname=test"))
|
|
|
|
self.assertEqual(res, {'dbname': 'test'})
|
|
|
|
|
2016-03-03 18:31:37 +03:00
|
|
|
|
|
|
|
class MakeDsnTestCase(ConnectingTestCase):
|
2016-03-03 19:58:24 +03:00
|
|
|
def test_empty_arguments(self):
|
|
|
|
self.assertEqual(ext.make_dsn(), '')
|
2016-03-03 18:31:37 +03:00
|
|
|
|
2016-03-03 19:09:33 +03:00
|
|
|
def test_empty_string(self):
|
|
|
|
dsn = ext.make_dsn('')
|
|
|
|
self.assertEqual(dsn, '')
|
|
|
|
|
2016-03-03 19:52:53 +03:00
|
|
|
def test_params_validation(self):
|
|
|
|
self.assertRaises(psycopg2.ProgrammingError,
|
|
|
|
ext.make_dsn, 'dbnamo=a')
|
|
|
|
self.assertRaises(psycopg2.ProgrammingError,
|
|
|
|
ext.make_dsn, dbnamo='a')
|
|
|
|
self.assertRaises(psycopg2.ProgrammingError,
|
|
|
|
ext.make_dsn, 'dbname=a', nosuchparam='b')
|
|
|
|
|
2016-03-03 18:31:37 +03:00
|
|
|
def test_empty_param(self):
|
2016-03-03 19:52:53 +03:00
|
|
|
dsn = ext.make_dsn(dbname='sony', password='')
|
2017-02-06 21:56:50 +03:00
|
|
|
self.assertDsnEqual(dsn, "dbname=sony password=''")
|
2016-03-03 18:31:37 +03:00
|
|
|
|
|
|
|
def test_escape(self):
|
2016-03-03 19:52:53 +03:00
|
|
|
dsn = ext.make_dsn(dbname='hello world')
|
2016-03-03 18:31:37 +03:00
|
|
|
self.assertEqual(dsn, "dbname='hello world'")
|
|
|
|
|
2016-03-03 19:52:53 +03:00
|
|
|
dsn = ext.make_dsn(dbname=r'back\slash')
|
2016-03-03 18:31:37 +03:00
|
|
|
self.assertEqual(dsn, r"dbname=back\\slash")
|
|
|
|
|
2016-03-03 19:52:53 +03:00
|
|
|
dsn = ext.make_dsn(dbname="quo'te")
|
2016-03-03 18:31:37 +03:00
|
|
|
self.assertEqual(dsn, r"dbname=quo\'te")
|
|
|
|
|
2016-03-03 19:52:53 +03:00
|
|
|
dsn = ext.make_dsn(dbname="with\ttab")
|
2016-03-03 18:31:37 +03:00
|
|
|
self.assertEqual(dsn, "dbname='with\ttab'")
|
|
|
|
|
2016-03-03 19:52:53 +03:00
|
|
|
dsn = ext.make_dsn(dbname=r"\every thing'")
|
2016-03-03 18:31:37 +03:00
|
|
|
self.assertEqual(dsn, r"dbname='\\every thing\''")
|
|
|
|
|
2016-03-03 19:52:53 +03:00
|
|
|
def test_database_is_a_keyword(self):
|
|
|
|
self.assertEqual(ext.make_dsn(database='sigh'), "dbname=sigh")
|
|
|
|
|
2016-03-03 18:31:37 +03:00
|
|
|
def test_params_merging(self):
|
2016-03-03 19:52:53 +03:00
|
|
|
dsn = ext.make_dsn('dbname=foo host=bar', host='baz')
|
2017-02-06 21:56:50 +03:00
|
|
|
self.assertDsnEqual(dsn, 'dbname=foo host=baz')
|
2016-03-03 18:31:37 +03:00
|
|
|
|
|
|
|
dsn = ext.make_dsn('dbname=foo', user='postgres')
|
2017-02-06 21:56:50 +03:00
|
|
|
self.assertDsnEqual(dsn, 'dbname=foo user=postgres')
|
2016-03-03 18:31:37 +03:00
|
|
|
|
|
|
|
def test_no_dsn_munging(self):
|
2016-03-03 19:52:53 +03:00
|
|
|
dsnin = 'dbname=a host=b user=c password=d'
|
|
|
|
dsn = ext.make_dsn(dsnin)
|
|
|
|
self.assertEqual(dsn, dsnin)
|
2016-03-03 18:31:37 +03:00
|
|
|
|
2017-03-01 23:12:13 +03:00
|
|
|
def test_null_args(self):
|
|
|
|
dsn = ext.make_dsn("dbname=foo", user="bar", password=None)
|
|
|
|
self.assertDsnEqual(dsn, "dbname=foo user=bar")
|
|
|
|
|
2016-03-03 18:31:37 +03:00
|
|
|
@skip_before_libpq(9, 2)
|
|
|
|
def test_url_is_cool(self):
|
2016-03-03 19:52:53 +03:00
|
|
|
url = 'postgresql://tester:secret@/test?application_name=wat'
|
|
|
|
dsn = ext.make_dsn(url)
|
|
|
|
self.assertEqual(dsn, url)
|
2016-03-03 18:31:37 +03:00
|
|
|
|
2016-03-03 19:52:53 +03:00
|
|
|
dsn = ext.make_dsn(url, application_name='woot')
|
2017-02-06 21:56:50 +03:00
|
|
|
self.assertDsnEqual(dsn,
|
2016-03-03 18:31:37 +03:00
|
|
|
'dbname=test user=tester password=secret application_name=woot')
|
2015-10-01 15:20:11 +03:00
|
|
|
|
2016-03-03 19:52:53 +03:00
|
|
|
self.assertRaises(psycopg2.ProgrammingError,
|
|
|
|
ext.make_dsn, 'postgresql://tester:secret@/test?nosuch=param')
|
|
|
|
self.assertRaises(psycopg2.ProgrammingError,
|
|
|
|
ext.make_dsn, url, nosuch="param")
|
2015-10-01 15:20:11 +03:00
|
|
|
|
2015-10-30 15:02:45 +03:00
|
|
|
@skip_before_libpq(9, 3)
|
2015-10-30 13:38:28 +03:00
|
|
|
def test_get_dsn_parameters(self):
|
2015-10-30 13:10:41 +03:00
|
|
|
conn = self.connect()
|
|
|
|
d = conn.get_dsn_parameters()
|
|
|
|
self.assertEqual(d['dbname'], dbname) # the only param we can check reliably
|
2016-12-24 02:18:22 +03:00
|
|
|
self.assert_('password' not in d, d)
|
2015-10-30 13:10:41 +03:00
|
|
|
|
2010-11-28 18:03:34 +03:00
|
|
|
|
2013-04-07 03:23:30 +04:00
|
|
|
class IsolationLevelsTestCase(ConnectingTestCase):
|
2010-11-18 03:20:39 +03:00
|
|
|
|
|
|
|
def setUp(self):
|
2013-04-07 03:23:30 +04:00
|
|
|
ConnectingTestCase.setUp(self)
|
|
|
|
|
2010-11-18 03:20:39 +03:00
|
|
|
conn = self.connect()
|
|
|
|
cur = conn.cursor()
|
2010-11-19 06:55:37 +03:00
|
|
|
try:
|
|
|
|
cur.execute("drop table isolevel;")
|
|
|
|
except psycopg2.ProgrammingError:
|
|
|
|
conn.rollback()
|
2020-07-08 01:48:44 +03:00
|
|
|
try:
|
|
|
|
cur.execute("create table isolevel (id integer);")
|
|
|
|
conn.commit()
|
|
|
|
finally:
|
|
|
|
conn.close()
|
2010-11-18 03:20:39 +03:00
|
|
|
|
2010-04-21 21:40:05 +04:00
|
|
|
def test_isolation_level(self):
|
|
|
|
conn = self.connect()
|
|
|
|
self.assertEqual(
|
|
|
|
conn.isolation_level,
|
2017-02-16 05:07:05 +03:00
|
|
|
ext.ISOLATION_LEVEL_DEFAULT)
|
2010-04-21 21:40:05 +04:00
|
|
|
|
|
|
|
def test_encoding(self):
|
|
|
|
conn = self.connect()
|
2017-02-16 05:07:05 +03:00
|
|
|
self.assert_(conn.encoding in ext.encodings)
|
2010-04-21 21:40:05 +04:00
|
|
|
|
2010-11-18 03:20:39 +03:00
|
|
|
def test_set_isolation_level(self):
|
|
|
|
conn = self.connect()
|
2011-05-11 15:51:44 +04:00
|
|
|
curs = conn.cursor()
|
2010-11-18 03:20:39 +03:00
|
|
|
|
2011-06-03 12:31:06 +04:00
|
|
|
levels = [
|
2016-10-11 02:10:53 +03:00
|
|
|
('read uncommitted',
|
2017-02-16 05:07:05 +03:00
|
|
|
ext.ISOLATION_LEVEL_READ_UNCOMMITTED),
|
|
|
|
('read committed', ext.ISOLATION_LEVEL_READ_COMMITTED),
|
|
|
|
('repeatable read', ext.ISOLATION_LEVEL_REPEATABLE_READ),
|
|
|
|
('serializable', ext.ISOLATION_LEVEL_SERIALIZABLE),
|
2011-06-03 12:31:06 +04:00
|
|
|
]
|
2011-06-03 03:40:54 +04:00
|
|
|
for name, level in levels:
|
2011-05-11 15:51:44 +04:00
|
|
|
conn.set_isolation_level(level)
|
2011-06-03 03:40:54 +04:00
|
|
|
|
|
|
|
# the only values available on prehistoric PG versions
|
2018-10-13 05:28:42 +03:00
|
|
|
if conn.info.server_version < 80000:
|
2011-12-15 16:53:48 +04:00
|
|
|
if level in (
|
2017-02-16 05:07:05 +03:00
|
|
|
ext.ISOLATION_LEVEL_READ_UNCOMMITTED,
|
|
|
|
ext.ISOLATION_LEVEL_REPEATABLE_READ):
|
2011-12-15 16:53:48 +04:00
|
|
|
name, level = levels[levels.index((name, level)) + 1]
|
2011-06-03 03:40:54 +04:00
|
|
|
|
2011-05-11 15:51:44 +04:00
|
|
|
self.assertEqual(conn.isolation_level, level)
|
2010-11-18 03:20:39 +03:00
|
|
|
|
2011-05-11 15:51:44 +04:00
|
|
|
curs.execute('show transaction_isolation;')
|
|
|
|
got_name = curs.fetchone()[0]
|
2010-11-18 03:20:39 +03:00
|
|
|
|
2011-05-11 15:51:44 +04:00
|
|
|
self.assertEqual(name, got_name)
|
|
|
|
conn.commit()
|
2010-11-18 03:20:39 +03:00
|
|
|
|
|
|
|
self.assertRaises(ValueError, conn.set_isolation_level, -1)
|
2017-02-16 05:07:05 +03:00
|
|
|
self.assertRaises(ValueError, conn.set_isolation_level, 5)
|
2017-02-04 18:19:41 +03:00
|
|
|
|
2017-02-16 14:04:02 +03:00
|
|
|
def test_set_isolation_level_autocommit(self):
|
|
|
|
conn = self.connect()
|
|
|
|
curs = conn.cursor()
|
|
|
|
|
|
|
|
conn.set_isolation_level(ext.ISOLATION_LEVEL_AUTOCOMMIT)
|
|
|
|
self.assertEqual(conn.isolation_level, ext.ISOLATION_LEVEL_DEFAULT)
|
|
|
|
self.assert_(conn.autocommit)
|
|
|
|
|
|
|
|
conn.isolation_level = 'serializable'
|
|
|
|
self.assertEqual(conn.isolation_level, ext.ISOLATION_LEVEL_SERIALIZABLE)
|
|
|
|
self.assert_(conn.autocommit)
|
|
|
|
|
|
|
|
curs.execute('show transaction_isolation;')
|
|
|
|
self.assertEqual(curs.fetchone()[0], 'serializable')
|
|
|
|
|
2017-02-04 18:19:41 +03:00
|
|
|
def test_set_isolation_level_default(self):
|
|
|
|
conn = self.connect()
|
|
|
|
curs = conn.cursor()
|
|
|
|
|
|
|
|
conn.autocommit = True
|
|
|
|
curs.execute("set default_transaction_isolation to 'read committed'")
|
|
|
|
|
|
|
|
conn.autocommit = False
|
2017-02-16 05:07:05 +03:00
|
|
|
conn.set_isolation_level(ext.ISOLATION_LEVEL_SERIALIZABLE)
|
2017-02-04 18:19:41 +03:00
|
|
|
self.assertEqual(conn.isolation_level,
|
2017-02-16 05:07:05 +03:00
|
|
|
ext.ISOLATION_LEVEL_SERIALIZABLE)
|
2017-02-04 18:19:41 +03:00
|
|
|
curs.execute("show transaction_isolation")
|
|
|
|
self.assertEqual(curs.fetchone()[0], "serializable")
|
|
|
|
|
|
|
|
conn.rollback()
|
2017-02-16 05:07:05 +03:00
|
|
|
conn.set_isolation_level(ext.ISOLATION_LEVEL_DEFAULT)
|
2017-02-04 18:19:41 +03:00
|
|
|
curs.execute("show transaction_isolation")
|
|
|
|
self.assertEqual(curs.fetchone()[0], "read committed")
|
2010-11-18 03:20:39 +03:00
|
|
|
|
|
|
|
def test_set_isolation_level_abort(self):
|
|
|
|
conn = self.connect()
|
|
|
|
cur = conn.cursor()
|
|
|
|
|
2017-02-16 05:07:05 +03:00
|
|
|
self.assertEqual(ext.TRANSACTION_STATUS_IDLE,
|
2018-10-13 05:28:42 +03:00
|
|
|
conn.info.transaction_status)
|
2010-11-18 03:20:39 +03:00
|
|
|
cur.execute("insert into isolevel values (10);")
|
2017-02-16 05:07:05 +03:00
|
|
|
self.assertEqual(ext.TRANSACTION_STATUS_INTRANS,
|
2018-10-13 05:28:42 +03:00
|
|
|
conn.info.transaction_status)
|
2010-11-18 03:20:39 +03:00
|
|
|
|
2017-02-16 05:40:21 +03:00
|
|
|
conn.set_isolation_level(
|
|
|
|
psycopg2.extensions.ISOLATION_LEVEL_SERIALIZABLE)
|
|
|
|
self.assertEqual(psycopg2.extensions.TRANSACTION_STATUS_IDLE,
|
2018-10-13 05:28:42 +03:00
|
|
|
conn.info.transaction_status)
|
2017-02-16 05:40:21 +03:00
|
|
|
cur.execute("select count(*) from isolevel;")
|
|
|
|
self.assertEqual(0, cur.fetchone()[0])
|
|
|
|
|
|
|
|
cur.execute("insert into isolevel values (10);")
|
|
|
|
self.assertEqual(psycopg2.extensions.TRANSACTION_STATUS_INTRANS,
|
2018-10-13 05:28:42 +03:00
|
|
|
conn.info.transaction_status)
|
2017-02-16 05:40:21 +03:00
|
|
|
conn.set_isolation_level(
|
|
|
|
psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT)
|
|
|
|
self.assertEqual(psycopg2.extensions.TRANSACTION_STATUS_IDLE,
|
2018-10-13 05:28:42 +03:00
|
|
|
conn.info.transaction_status)
|
2017-02-16 05:40:21 +03:00
|
|
|
cur.execute("select count(*) from isolevel;")
|
|
|
|
self.assertEqual(0, cur.fetchone()[0])
|
|
|
|
|
|
|
|
cur.execute("insert into isolevel values (10);")
|
|
|
|
self.assertEqual(psycopg2.extensions.TRANSACTION_STATUS_IDLE,
|
2018-10-13 05:28:42 +03:00
|
|
|
conn.info.transaction_status)
|
2017-02-16 05:40:21 +03:00
|
|
|
conn.set_isolation_level(
|
|
|
|
psycopg2.extensions.ISOLATION_LEVEL_READ_COMMITTED)
|
|
|
|
self.assertEqual(psycopg2.extensions.TRANSACTION_STATUS_IDLE,
|
2018-10-13 05:28:42 +03:00
|
|
|
conn.info.transaction_status)
|
2017-02-16 05:40:21 +03:00
|
|
|
cur.execute("select count(*) from isolevel;")
|
|
|
|
self.assertEqual(1, cur.fetchone()[0])
|
2017-02-04 18:19:41 +03:00
|
|
|
self.assertEqual(conn.isolation_level,
|
2017-02-16 05:40:21 +03:00
|
|
|
psycopg2.extensions.ISOLATION_LEVEL_READ_COMMITTED)
|
2010-11-18 03:20:39 +03:00
|
|
|
|
2010-11-17 04:04:34 +03:00
|
|
|
def test_isolation_level_autocommit(self):
|
|
|
|
cnn1 = self.connect()
|
|
|
|
cnn2 = self.connect()
|
2017-02-16 05:07:05 +03:00
|
|
|
cnn2.set_isolation_level(ext.ISOLATION_LEVEL_AUTOCOMMIT)
|
2010-11-17 04:04:34 +03:00
|
|
|
|
|
|
|
cur1 = cnn1.cursor()
|
|
|
|
cur1.execute("select count(*) from isolevel;")
|
|
|
|
self.assertEqual(0, cur1.fetchone()[0])
|
|
|
|
cnn1.commit()
|
|
|
|
|
|
|
|
cur2 = cnn2.cursor()
|
|
|
|
cur2.execute("insert into isolevel values (10);")
|
|
|
|
|
2010-11-18 03:20:39 +03:00
|
|
|
cur1.execute("select count(*) from isolevel;")
|
2010-11-17 04:04:34 +03:00
|
|
|
self.assertEqual(1, cur1.fetchone()[0])
|
|
|
|
|
|
|
|
def test_isolation_level_read_committed(self):
|
|
|
|
cnn1 = self.connect()
|
|
|
|
cnn2 = self.connect()
|
2017-02-16 05:07:05 +03:00
|
|
|
cnn2.set_isolation_level(ext.ISOLATION_LEVEL_READ_COMMITTED)
|
2010-11-17 04:04:34 +03:00
|
|
|
|
|
|
|
cur1 = cnn1.cursor()
|
|
|
|
cur1.execute("select count(*) from isolevel;")
|
|
|
|
self.assertEqual(0, cur1.fetchone()[0])
|
|
|
|
cnn1.commit()
|
|
|
|
|
|
|
|
cur2 = cnn2.cursor()
|
|
|
|
cur2.execute("insert into isolevel values (10);")
|
2010-11-18 03:20:39 +03:00
|
|
|
cur1.execute("insert into isolevel values (20);")
|
|
|
|
|
2010-11-17 04:04:34 +03:00
|
|
|
cur2.execute("select count(*) from isolevel;")
|
|
|
|
self.assertEqual(1, cur2.fetchone()[0])
|
|
|
|
cnn1.commit()
|
|
|
|
cur2.execute("select count(*) from isolevel;")
|
|
|
|
self.assertEqual(2, cur2.fetchone()[0])
|
|
|
|
|
2010-11-18 03:20:39 +03:00
|
|
|
cur1.execute("select count(*) from isolevel;")
|
|
|
|
self.assertEqual(1, cur1.fetchone()[0])
|
|
|
|
cnn2.commit()
|
|
|
|
cur1.execute("select count(*) from isolevel;")
|
|
|
|
self.assertEqual(2, cur1.fetchone()[0])
|
|
|
|
|
2010-11-17 04:04:34 +03:00
|
|
|
def test_isolation_level_serializable(self):
|
|
|
|
cnn1 = self.connect()
|
|
|
|
cnn2 = self.connect()
|
2017-02-16 05:07:05 +03:00
|
|
|
cnn2.set_isolation_level(ext.ISOLATION_LEVEL_SERIALIZABLE)
|
2010-11-17 04:04:34 +03:00
|
|
|
|
|
|
|
cur1 = cnn1.cursor()
|
|
|
|
cur1.execute("select count(*) from isolevel;")
|
|
|
|
self.assertEqual(0, cur1.fetchone()[0])
|
|
|
|
cnn1.commit()
|
|
|
|
|
|
|
|
cur2 = cnn2.cursor()
|
|
|
|
cur2.execute("insert into isolevel values (10);")
|
2010-11-18 03:20:39 +03:00
|
|
|
cur1.execute("insert into isolevel values (20);")
|
|
|
|
|
2010-11-17 04:04:34 +03:00
|
|
|
cur2.execute("select count(*) from isolevel;")
|
|
|
|
self.assertEqual(1, cur2.fetchone()[0])
|
|
|
|
cnn1.commit()
|
|
|
|
cur2.execute("select count(*) from isolevel;")
|
|
|
|
self.assertEqual(1, cur2.fetchone()[0])
|
|
|
|
|
2010-11-18 03:20:39 +03:00
|
|
|
cur1.execute("select count(*) from isolevel;")
|
|
|
|
self.assertEqual(1, cur1.fetchone()[0])
|
2010-11-17 04:04:34 +03:00
|
|
|
cnn2.commit()
|
2010-11-18 03:20:39 +03:00
|
|
|
cur1.execute("select count(*) from isolevel;")
|
|
|
|
self.assertEqual(2, cur1.fetchone()[0])
|
|
|
|
|
2010-11-17 04:04:34 +03:00
|
|
|
cur2.execute("select count(*) from isolevel;")
|
|
|
|
self.assertEqual(2, cur2.fetchone()[0])
|
|
|
|
|
2011-11-17 03:51:05 +04:00
|
|
|
def test_isolation_level_closed(self):
|
|
|
|
cnn = self.connect()
|
|
|
|
cnn.close()
|
|
|
|
self.assertRaises(psycopg2.InterfaceError,
|
|
|
|
cnn.set_isolation_level, 0)
|
|
|
|
self.assertRaises(psycopg2.InterfaceError,
|
|
|
|
cnn.set_isolation_level, 1)
|
|
|
|
|
2017-02-16 05:07:05 +03:00
|
|
|
def test_setattr_isolation_level_int(self):
|
|
|
|
cur = self.conn.cursor()
|
|
|
|
self.conn.isolation_level = ext.ISOLATION_LEVEL_SERIALIZABLE
|
|
|
|
self.assertEqual(self.conn.isolation_level, ext.ISOLATION_LEVEL_SERIALIZABLE)
|
|
|
|
|
|
|
|
cur.execute("SHOW transaction_isolation;")
|
|
|
|
self.assertEqual(cur.fetchone()[0], 'serializable')
|
|
|
|
self.conn.rollback()
|
|
|
|
|
|
|
|
self.conn.isolation_level = ext.ISOLATION_LEVEL_REPEATABLE_READ
|
|
|
|
cur.execute("SHOW transaction_isolation;")
|
2018-10-13 05:28:42 +03:00
|
|
|
if self.conn.info.server_version > 80000:
|
2017-02-16 05:07:05 +03:00
|
|
|
self.assertEqual(self.conn.isolation_level,
|
|
|
|
ext.ISOLATION_LEVEL_REPEATABLE_READ)
|
|
|
|
self.assertEqual(cur.fetchone()[0], 'repeatable read')
|
|
|
|
else:
|
|
|
|
self.assertEqual(self.conn.isolation_level,
|
|
|
|
ext.ISOLATION_LEVEL_SERIALIZABLE)
|
|
|
|
self.assertEqual(cur.fetchone()[0], 'serializable')
|
|
|
|
self.conn.rollback()
|
|
|
|
|
|
|
|
self.conn.isolation_level = ext.ISOLATION_LEVEL_READ_COMMITTED
|
|
|
|
self.assertEqual(self.conn.isolation_level,
|
|
|
|
ext.ISOLATION_LEVEL_READ_COMMITTED)
|
|
|
|
cur.execute("SHOW transaction_isolation;")
|
|
|
|
self.assertEqual(cur.fetchone()[0], 'read committed')
|
|
|
|
self.conn.rollback()
|
|
|
|
|
|
|
|
self.conn.isolation_level = ext.ISOLATION_LEVEL_READ_UNCOMMITTED
|
|
|
|
cur.execute("SHOW transaction_isolation;")
|
2018-10-13 05:28:42 +03:00
|
|
|
if self.conn.info.server_version > 80000:
|
2017-02-16 05:07:05 +03:00
|
|
|
self.assertEqual(self.conn.isolation_level,
|
|
|
|
ext.ISOLATION_LEVEL_READ_UNCOMMITTED)
|
|
|
|
self.assertEqual(cur.fetchone()[0], 'read uncommitted')
|
|
|
|
else:
|
|
|
|
self.assertEqual(self.conn.isolation_level,
|
|
|
|
ext.ISOLATION_LEVEL_READ_COMMITTED)
|
|
|
|
self.assertEqual(cur.fetchone()[0], 'read committed')
|
|
|
|
self.conn.rollback()
|
|
|
|
|
|
|
|
self.assertEqual(ext.ISOLATION_LEVEL_DEFAULT, None)
|
|
|
|
self.conn.isolation_level = ext.ISOLATION_LEVEL_DEFAULT
|
|
|
|
self.assertEqual(self.conn.isolation_level, None)
|
|
|
|
cur.execute("SHOW transaction_isolation;")
|
|
|
|
isol = cur.fetchone()[0]
|
|
|
|
cur.execute("SHOW default_transaction_isolation;")
|
|
|
|
self.assertEqual(cur.fetchone()[0], isol)
|
|
|
|
|
|
|
|
def test_setattr_isolation_level_str(self):
|
|
|
|
cur = self.conn.cursor()
|
|
|
|
self.conn.isolation_level = "serializable"
|
|
|
|
self.assertEqual(self.conn.isolation_level, ext.ISOLATION_LEVEL_SERIALIZABLE)
|
|
|
|
|
|
|
|
cur.execute("SHOW transaction_isolation;")
|
|
|
|
self.assertEqual(cur.fetchone()[0], 'serializable')
|
|
|
|
self.conn.rollback()
|
|
|
|
|
|
|
|
self.conn.isolation_level = "repeatable read"
|
|
|
|
cur.execute("SHOW transaction_isolation;")
|
2018-10-13 05:28:42 +03:00
|
|
|
if self.conn.info.server_version > 80000:
|
2017-02-16 05:07:05 +03:00
|
|
|
self.assertEqual(self.conn.isolation_level,
|
|
|
|
ext.ISOLATION_LEVEL_REPEATABLE_READ)
|
|
|
|
self.assertEqual(cur.fetchone()[0], 'repeatable read')
|
|
|
|
else:
|
|
|
|
self.assertEqual(self.conn.isolation_level,
|
|
|
|
ext.ISOLATION_LEVEL_SERIALIZABLE)
|
|
|
|
self.assertEqual(cur.fetchone()[0], 'serializable')
|
|
|
|
self.conn.rollback()
|
|
|
|
|
|
|
|
self.conn.isolation_level = "read committed"
|
|
|
|
self.assertEqual(self.conn.isolation_level,
|
|
|
|
ext.ISOLATION_LEVEL_READ_COMMITTED)
|
|
|
|
cur.execute("SHOW transaction_isolation;")
|
|
|
|
self.assertEqual(cur.fetchone()[0], 'read committed')
|
|
|
|
self.conn.rollback()
|
|
|
|
|
|
|
|
self.conn.isolation_level = "read uncommitted"
|
|
|
|
cur.execute("SHOW transaction_isolation;")
|
2018-10-13 05:28:42 +03:00
|
|
|
if self.conn.info.server_version > 80000:
|
2017-02-16 05:07:05 +03:00
|
|
|
self.assertEqual(self.conn.isolation_level,
|
|
|
|
ext.ISOLATION_LEVEL_READ_UNCOMMITTED)
|
|
|
|
self.assertEqual(cur.fetchone()[0], 'read uncommitted')
|
|
|
|
else:
|
|
|
|
self.assertEqual(self.conn.isolation_level,
|
|
|
|
ext.ISOLATION_LEVEL_READ_COMMITTED)
|
|
|
|
self.assertEqual(cur.fetchone()[0], 'read committed')
|
|
|
|
self.conn.rollback()
|
|
|
|
|
|
|
|
self.conn.isolation_level = "default"
|
|
|
|
self.assertEqual(self.conn.isolation_level, None)
|
|
|
|
cur.execute("SHOW transaction_isolation;")
|
|
|
|
isol = cur.fetchone()[0]
|
|
|
|
cur.execute("SHOW default_transaction_isolation;")
|
|
|
|
self.assertEqual(cur.fetchone()[0], isol)
|
|
|
|
|
|
|
|
def test_setattr_isolation_level_invalid(self):
|
|
|
|
self.assertRaises(ValueError, setattr, self.conn, 'isolation_level', 0)
|
|
|
|
self.assertRaises(ValueError, setattr, self.conn, 'isolation_level', -1)
|
|
|
|
self.assertRaises(ValueError, setattr, self.conn, 'isolation_level', 5)
|
|
|
|
self.assertRaises(ValueError, setattr, self.conn, 'isolation_level', 'bah')
|
|
|
|
|
2018-10-13 00:37:50 +03:00
|
|
|
def test_attribs_segfault(self):
|
|
|
|
# bug #790
|
|
|
|
for i in range(10000):
|
|
|
|
self.conn.autocommit
|
|
|
|
self.conn.readonly
|
|
|
|
self.conn.deferrable
|
|
|
|
self.conn.isolation_level
|
2010-10-11 16:03:37 +04:00
|
|
|
|
|
|
|
|
2018-10-30 03:23:56 +03:00
|
|
|
@skip_if_tpc_disabled
|
2013-04-07 03:23:30 +04:00
|
|
|
class ConnectionTwoPhaseTests(ConnectingTestCase):
|
2010-10-11 16:03:37 +04:00
|
|
|
def setUp(self):
|
2013-04-07 03:23:30 +04:00
|
|
|
ConnectingTestCase.setUp(self)
|
2010-11-28 19:00:32 +03:00
|
|
|
|
2010-10-10 22:15:33 +04:00
|
|
|
self.make_test_table()
|
2010-10-11 16:03:37 +04:00
|
|
|
self.clear_test_xacts()
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
self.clear_test_xacts()
|
2013-04-07 03:23:30 +04:00
|
|
|
ConnectingTestCase.tearDown(self)
|
2010-11-28 19:00:32 +03:00
|
|
|
|
2010-10-11 16:03:37 +04:00
|
|
|
def clear_test_xacts(self):
|
|
|
|
"""Rollback all the prepared transaction in the testing db."""
|
|
|
|
cnn = self.connect()
|
|
|
|
cnn.set_isolation_level(0)
|
|
|
|
cur = cnn.cursor()
|
2010-11-19 06:55:37 +03:00
|
|
|
try:
|
|
|
|
cur.execute(
|
|
|
|
"select gid from pg_prepared_xacts where database = %s",
|
2011-02-11 01:59:31 +03:00
|
|
|
(dbname,))
|
2010-11-19 06:55:37 +03:00
|
|
|
except psycopg2.ProgrammingError:
|
|
|
|
cnn.rollback()
|
|
|
|
cnn.close()
|
|
|
|
return
|
|
|
|
|
2016-03-03 18:31:37 +03:00
|
|
|
gids = [r[0] for r in cur]
|
2010-10-11 16:03:37 +04:00
|
|
|
for gid in gids:
|
|
|
|
cur.execute("rollback prepared %s;", (gid,))
|
|
|
|
cnn.close()
|
|
|
|
|
2010-10-10 22:15:33 +04:00
|
|
|
def make_test_table(self):
|
|
|
|
cnn = self.connect()
|
|
|
|
cur = cnn.cursor()
|
2010-11-19 06:55:37 +03:00
|
|
|
try:
|
|
|
|
cur.execute("DROP TABLE test_tpc;")
|
|
|
|
except psycopg2.ProgrammingError:
|
|
|
|
cnn.rollback()
|
2020-05-08 01:26:24 +03:00
|
|
|
try:
|
|
|
|
cur.execute("CREATE TABLE test_tpc (data text);")
|
|
|
|
cnn.commit()
|
|
|
|
finally:
|
|
|
|
cnn.close()
|
2010-10-10 22:15:33 +04:00
|
|
|
|
|
|
|
def count_xacts(self):
|
|
|
|
"""Return the number of prepared xacts currently in the test db."""
|
|
|
|
cnn = self.connect()
|
|
|
|
cur = cnn.cursor()
|
|
|
|
cur.execute("""
|
|
|
|
select count(*) from pg_prepared_xacts
|
|
|
|
where database = %s;""",
|
2011-02-11 01:59:31 +03:00
|
|
|
(dbname,))
|
2010-10-10 22:15:33 +04:00
|
|
|
rv = cur.fetchone()[0]
|
|
|
|
cnn.close()
|
|
|
|
return rv
|
|
|
|
|
|
|
|
def count_test_records(self):
|
|
|
|
"""Return the number of records in the test table."""
|
|
|
|
cnn = self.connect()
|
|
|
|
cur = cnn.cursor()
|
|
|
|
cur.execute("select count(*) from test_tpc;")
|
|
|
|
rv = cur.fetchone()[0]
|
|
|
|
cnn.close()
|
|
|
|
return rv
|
|
|
|
|
|
|
|
def test_tpc_commit(self):
|
|
|
|
cnn = self.connect()
|
|
|
|
xid = cnn.xid(1, "gtrid", "bqual")
|
2017-02-16 05:07:05 +03:00
|
|
|
self.assertEqual(cnn.status, ext.STATUS_READY)
|
2010-10-10 22:15:33 +04:00
|
|
|
|
|
|
|
cnn.tpc_begin(xid)
|
2017-02-16 05:07:05 +03:00
|
|
|
self.assertEqual(cnn.status, ext.STATUS_BEGIN)
|
2010-10-10 22:15:33 +04:00
|
|
|
|
|
|
|
cur = cnn.cursor()
|
|
|
|
cur.execute("insert into test_tpc values ('test_tpc_commit');")
|
|
|
|
self.assertEqual(0, self.count_xacts())
|
|
|
|
self.assertEqual(0, self.count_test_records())
|
|
|
|
|
|
|
|
cnn.tpc_prepare()
|
2017-02-16 05:07:05 +03:00
|
|
|
self.assertEqual(cnn.status, ext.STATUS_PREPARED)
|
2010-10-10 22:15:33 +04:00
|
|
|
self.assertEqual(1, self.count_xacts())
|
|
|
|
self.assertEqual(0, self.count_test_records())
|
|
|
|
|
|
|
|
cnn.tpc_commit()
|
2017-02-16 05:07:05 +03:00
|
|
|
self.assertEqual(cnn.status, ext.STATUS_READY)
|
2010-10-10 22:15:33 +04:00
|
|
|
self.assertEqual(0, self.count_xacts())
|
|
|
|
self.assertEqual(1, self.count_test_records())
|
|
|
|
|
|
|
|
def test_tpc_commit_one_phase(self):
|
|
|
|
cnn = self.connect()
|
|
|
|
xid = cnn.xid(1, "gtrid", "bqual")
|
2017-02-16 05:07:05 +03:00
|
|
|
self.assertEqual(cnn.status, ext.STATUS_READY)
|
2010-10-10 22:15:33 +04:00
|
|
|
|
|
|
|
cnn.tpc_begin(xid)
|
2017-02-16 05:07:05 +03:00
|
|
|
self.assertEqual(cnn.status, ext.STATUS_BEGIN)
|
2010-10-10 22:15:33 +04:00
|
|
|
|
|
|
|
cur = cnn.cursor()
|
|
|
|
cur.execute("insert into test_tpc values ('test_tpc_commit_1p');")
|
|
|
|
self.assertEqual(0, self.count_xacts())
|
|
|
|
self.assertEqual(0, self.count_test_records())
|
|
|
|
|
|
|
|
cnn.tpc_commit()
|
2017-02-16 05:07:05 +03:00
|
|
|
self.assertEqual(cnn.status, ext.STATUS_READY)
|
2010-10-10 22:15:33 +04:00
|
|
|
self.assertEqual(0, self.count_xacts())
|
|
|
|
self.assertEqual(1, self.count_test_records())
|
|
|
|
|
|
|
|
def test_tpc_commit_recovered(self):
|
|
|
|
cnn = self.connect()
|
|
|
|
xid = cnn.xid(1, "gtrid", "bqual")
|
2017-02-16 05:07:05 +03:00
|
|
|
self.assertEqual(cnn.status, ext.STATUS_READY)
|
2010-10-10 22:15:33 +04:00
|
|
|
|
|
|
|
cnn.tpc_begin(xid)
|
2017-02-16 05:07:05 +03:00
|
|
|
self.assertEqual(cnn.status, ext.STATUS_BEGIN)
|
2010-10-10 22:15:33 +04:00
|
|
|
|
|
|
|
cur = cnn.cursor()
|
|
|
|
cur.execute("insert into test_tpc values ('test_tpc_commit_rec');")
|
|
|
|
self.assertEqual(0, self.count_xacts())
|
|
|
|
self.assertEqual(0, self.count_test_records())
|
|
|
|
|
|
|
|
cnn.tpc_prepare()
|
|
|
|
cnn.close()
|
|
|
|
self.assertEqual(1, self.count_xacts())
|
|
|
|
self.assertEqual(0, self.count_test_records())
|
|
|
|
|
|
|
|
cnn = self.connect()
|
|
|
|
xid = cnn.xid(1, "gtrid", "bqual")
|
|
|
|
cnn.tpc_commit(xid)
|
|
|
|
|
2017-02-16 05:07:05 +03:00
|
|
|
self.assertEqual(cnn.status, ext.STATUS_READY)
|
2010-10-10 22:15:33 +04:00
|
|
|
self.assertEqual(0, self.count_xacts())
|
|
|
|
self.assertEqual(1, self.count_test_records())
|
|
|
|
|
|
|
|
def test_tpc_rollback(self):
|
|
|
|
cnn = self.connect()
|
|
|
|
xid = cnn.xid(1, "gtrid", "bqual")
|
2017-02-16 05:07:05 +03:00
|
|
|
self.assertEqual(cnn.status, ext.STATUS_READY)
|
2010-10-10 22:15:33 +04:00
|
|
|
|
|
|
|
cnn.tpc_begin(xid)
|
2017-02-16 05:07:05 +03:00
|
|
|
self.assertEqual(cnn.status, ext.STATUS_BEGIN)
|
2010-10-10 22:15:33 +04:00
|
|
|
|
|
|
|
cur = cnn.cursor()
|
|
|
|
cur.execute("insert into test_tpc values ('test_tpc_rollback');")
|
|
|
|
self.assertEqual(0, self.count_xacts())
|
|
|
|
self.assertEqual(0, self.count_test_records())
|
|
|
|
|
|
|
|
cnn.tpc_prepare()
|
2017-02-16 05:07:05 +03:00
|
|
|
self.assertEqual(cnn.status, ext.STATUS_PREPARED)
|
2010-10-10 22:15:33 +04:00
|
|
|
self.assertEqual(1, self.count_xacts())
|
|
|
|
self.assertEqual(0, self.count_test_records())
|
|
|
|
|
|
|
|
cnn.tpc_rollback()
|
2017-02-16 05:07:05 +03:00
|
|
|
self.assertEqual(cnn.status, ext.STATUS_READY)
|
2010-10-10 22:15:33 +04:00
|
|
|
self.assertEqual(0, self.count_xacts())
|
|
|
|
self.assertEqual(0, self.count_test_records())
|
|
|
|
|
|
|
|
def test_tpc_rollback_one_phase(self):
|
|
|
|
cnn = self.connect()
|
|
|
|
xid = cnn.xid(1, "gtrid", "bqual")
|
2017-02-16 05:07:05 +03:00
|
|
|
self.assertEqual(cnn.status, ext.STATUS_READY)
|
2010-10-10 22:15:33 +04:00
|
|
|
|
|
|
|
cnn.tpc_begin(xid)
|
2017-02-16 05:07:05 +03:00
|
|
|
self.assertEqual(cnn.status, ext.STATUS_BEGIN)
|
2010-10-10 22:15:33 +04:00
|
|
|
|
|
|
|
cur = cnn.cursor()
|
|
|
|
cur.execute("insert into test_tpc values ('test_tpc_rollback_1p');")
|
|
|
|
self.assertEqual(0, self.count_xacts())
|
|
|
|
self.assertEqual(0, self.count_test_records())
|
|
|
|
|
|
|
|
cnn.tpc_rollback()
|
2017-02-16 05:07:05 +03:00
|
|
|
self.assertEqual(cnn.status, ext.STATUS_READY)
|
2010-10-10 22:15:33 +04:00
|
|
|
self.assertEqual(0, self.count_xacts())
|
|
|
|
self.assertEqual(0, self.count_test_records())
|
|
|
|
|
|
|
|
def test_tpc_rollback_recovered(self):
|
|
|
|
cnn = self.connect()
|
|
|
|
xid = cnn.xid(1, "gtrid", "bqual")
|
2017-02-16 05:07:05 +03:00
|
|
|
self.assertEqual(cnn.status, ext.STATUS_READY)
|
2010-10-10 22:15:33 +04:00
|
|
|
|
|
|
|
cnn.tpc_begin(xid)
|
2017-02-16 05:07:05 +03:00
|
|
|
self.assertEqual(cnn.status, ext.STATUS_BEGIN)
|
2010-10-10 22:15:33 +04:00
|
|
|
|
|
|
|
cur = cnn.cursor()
|
|
|
|
cur.execute("insert into test_tpc values ('test_tpc_commit_rec');")
|
|
|
|
self.assertEqual(0, self.count_xacts())
|
|
|
|
self.assertEqual(0, self.count_test_records())
|
|
|
|
|
|
|
|
cnn.tpc_prepare()
|
|
|
|
cnn.close()
|
|
|
|
self.assertEqual(1, self.count_xacts())
|
|
|
|
self.assertEqual(0, self.count_test_records())
|
|
|
|
|
|
|
|
cnn = self.connect()
|
|
|
|
xid = cnn.xid(1, "gtrid", "bqual")
|
|
|
|
cnn.tpc_rollback(xid)
|
|
|
|
|
2017-02-16 05:07:05 +03:00
|
|
|
self.assertEqual(cnn.status, ext.STATUS_READY)
|
2010-10-10 22:15:33 +04:00
|
|
|
self.assertEqual(0, self.count_xacts())
|
|
|
|
self.assertEqual(0, self.count_test_records())
|
|
|
|
|
2010-10-11 16:03:37 +04:00
|
|
|
def test_status_after_recover(self):
|
|
|
|
cnn = self.connect()
|
2017-02-16 05:07:05 +03:00
|
|
|
self.assertEqual(ext.STATUS_READY, cnn.status)
|
2016-03-03 18:31:37 +03:00
|
|
|
cnn.tpc_recover()
|
2017-02-16 05:07:05 +03:00
|
|
|
self.assertEqual(ext.STATUS_READY, cnn.status)
|
2010-10-11 16:03:37 +04:00
|
|
|
|
|
|
|
cur = cnn.cursor()
|
|
|
|
cur.execute("select 1")
|
2017-02-16 05:07:05 +03:00
|
|
|
self.assertEqual(ext.STATUS_BEGIN, cnn.status)
|
2016-03-03 18:31:37 +03:00
|
|
|
cnn.tpc_recover()
|
2017-02-16 05:07:05 +03:00
|
|
|
self.assertEqual(ext.STATUS_BEGIN, cnn.status)
|
2010-10-11 16:03:37 +04:00
|
|
|
|
|
|
|
def test_recovered_xids(self):
|
|
|
|
# insert a few test xns
|
|
|
|
cnn = self.connect()
|
|
|
|
cnn.set_isolation_level(0)
|
|
|
|
cur = cnn.cursor()
|
|
|
|
cur.execute("begin; prepare transaction '1-foo';")
|
|
|
|
cur.execute("begin; prepare transaction '2-bar';")
|
|
|
|
|
|
|
|
# read the values to return
|
|
|
|
cur.execute("""
|
|
|
|
select gid, prepared, owner, database
|
|
|
|
from pg_prepared_xacts
|
|
|
|
where database = %s;""",
|
2011-02-11 01:59:31 +03:00
|
|
|
(dbname,))
|
2010-10-11 16:03:37 +04:00
|
|
|
okvals = cur.fetchall()
|
|
|
|
okvals.sort()
|
|
|
|
|
|
|
|
cnn = self.connect()
|
|
|
|
xids = cnn.tpc_recover()
|
2016-03-03 18:31:37 +03:00
|
|
|
xids = [xid for xid in xids if xid.database == dbname]
|
2010-10-11 16:03:37 +04:00
|
|
|
xids.sort(key=attrgetter('gtrid'))
|
|
|
|
|
|
|
|
# check the values returned
|
|
|
|
self.assertEqual(len(okvals), len(xids))
|
2016-03-03 18:31:37 +03:00
|
|
|
for (xid, (gid, prepared, owner, database)) in zip(xids, okvals):
|
2010-10-11 16:03:37 +04:00
|
|
|
self.assertEqual(xid.gtrid, gid)
|
|
|
|
self.assertEqual(xid.prepared, prepared)
|
|
|
|
self.assertEqual(xid.owner, owner)
|
|
|
|
self.assertEqual(xid.database, database)
|
|
|
|
|
2010-10-11 01:54:24 +04:00
|
|
|
def test_xid_encoding(self):
|
|
|
|
cnn = self.connect()
|
|
|
|
xid = cnn.xid(42, "gtrid", "bqual")
|
|
|
|
cnn.tpc_begin(xid)
|
|
|
|
cnn.tpc_prepare()
|
|
|
|
|
|
|
|
cnn = self.connect()
|
|
|
|
cur = cnn.cursor()
|
|
|
|
cur.execute("select gid from pg_prepared_xacts where database = %s;",
|
2011-02-11 01:59:31 +03:00
|
|
|
(dbname,))
|
2010-10-11 01:54:24 +04:00
|
|
|
self.assertEqual('42_Z3RyaWQ=_YnF1YWw=', cur.fetchone()[0])
|
|
|
|
|
2017-02-02 04:53:50 +03:00
|
|
|
@slow
|
2010-10-11 21:27:07 +04:00
|
|
|
def test_xid_roundtrip(self):
|
|
|
|
for fid, gtrid, bqual in [
|
|
|
|
(0, "", ""),
|
|
|
|
(42, "gtrid", "bqual"),
|
|
|
|
(0x7fffffff, "x" * 64, "y" * 64),
|
|
|
|
]:
|
|
|
|
cnn = self.connect()
|
|
|
|
xid = cnn.xid(fid, gtrid, bqual)
|
|
|
|
cnn.tpc_begin(xid)
|
|
|
|
cnn.tpc_prepare()
|
|
|
|
cnn.close()
|
|
|
|
|
|
|
|
cnn = self.connect()
|
2016-03-03 18:31:37 +03:00
|
|
|
xids = [x for x in cnn.tpc_recover() if x.database == dbname]
|
2010-10-11 21:27:07 +04:00
|
|
|
self.assertEqual(1, len(xids))
|
|
|
|
xid = xids[0]
|
|
|
|
self.assertEqual(xid.format_id, fid)
|
|
|
|
self.assertEqual(xid.gtrid, gtrid)
|
|
|
|
self.assertEqual(xid.bqual, bqual)
|
|
|
|
|
|
|
|
cnn.tpc_rollback(xid)
|
|
|
|
|
2017-02-02 04:53:50 +03:00
|
|
|
@slow
|
2010-10-11 21:27:07 +04:00
|
|
|
def test_unparsed_roundtrip(self):
|
|
|
|
for tid in [
|
|
|
|
'',
|
|
|
|
'hello, world!',
|
|
|
|
'x' * 199, # PostgreSQL's limit in transaction id length
|
|
|
|
]:
|
|
|
|
cnn = self.connect()
|
|
|
|
cnn.tpc_begin(tid)
|
|
|
|
cnn.tpc_prepare()
|
|
|
|
cnn.close()
|
|
|
|
|
|
|
|
cnn = self.connect()
|
2016-03-03 18:31:37 +03:00
|
|
|
xids = [x for x in cnn.tpc_recover() if x.database == dbname]
|
2010-10-11 21:27:07 +04:00
|
|
|
self.assertEqual(1, len(xids))
|
|
|
|
xid = xids[0]
|
2010-10-12 03:12:27 +04:00
|
|
|
self.assertEqual(xid.format_id, None)
|
2010-10-11 21:27:07 +04:00
|
|
|
self.assertEqual(xid.gtrid, tid)
|
|
|
|
self.assertEqual(xid.bqual, None)
|
|
|
|
|
|
|
|
cnn.tpc_rollback(xid)
|
|
|
|
|
2010-10-14 04:27:20 +04:00
|
|
|
def test_xid_construction(self):
|
2019-03-16 21:56:56 +03:00
|
|
|
x1 = ext.Xid(74, 'foo', 'bar')
|
2010-10-14 04:27:20 +04:00
|
|
|
self.assertEqual(74, x1.format_id)
|
|
|
|
self.assertEqual('foo', x1.gtrid)
|
|
|
|
self.assertEqual('bar', x1.bqual)
|
|
|
|
|
|
|
|
def test_xid_from_string(self):
|
2019-03-16 21:56:56 +03:00
|
|
|
x2 = ext.Xid.from_string('42_Z3RyaWQ=_YnF1YWw=')
|
2010-10-14 04:27:20 +04:00
|
|
|
self.assertEqual(42, x2.format_id)
|
|
|
|
self.assertEqual('gtrid', x2.gtrid)
|
|
|
|
self.assertEqual('bqual', x2.bqual)
|
|
|
|
|
2019-03-16 21:56:56 +03:00
|
|
|
x3 = ext.Xid.from_string('99_xxx_yyy')
|
2010-10-14 04:27:20 +04:00
|
|
|
self.assertEqual(None, x3.format_id)
|
|
|
|
self.assertEqual('99_xxx_yyy', x3.gtrid)
|
|
|
|
self.assertEqual(None, x3.bqual)
|
|
|
|
|
|
|
|
def test_xid_to_string(self):
|
2019-03-16 21:56:56 +03:00
|
|
|
x1 = ext.Xid.from_string('42_Z3RyaWQ=_YnF1YWw=')
|
2010-10-14 04:27:20 +04:00
|
|
|
self.assertEqual(str(x1), '42_Z3RyaWQ=_YnF1YWw=')
|
|
|
|
|
2019-03-16 21:56:56 +03:00
|
|
|
x2 = ext.Xid.from_string('99_xxx_yyy')
|
2010-10-14 04:27:20 +04:00
|
|
|
self.assertEqual(str(x2), '99_xxx_yyy')
|
|
|
|
|
2010-10-15 11:27:07 +04:00
|
|
|
def test_xid_unicode(self):
|
|
|
|
cnn = self.connect()
|
|
|
|
x1 = cnn.xid(10, u'uni', u'code')
|
|
|
|
cnn.tpc_begin(x1)
|
|
|
|
cnn.tpc_prepare()
|
|
|
|
cnn.reset()
|
2016-03-03 18:31:37 +03:00
|
|
|
xid = [x for x in cnn.tpc_recover() if x.database == dbname][0]
|
2010-10-15 11:27:07 +04:00
|
|
|
self.assertEqual(10, xid.format_id)
|
|
|
|
self.assertEqual('uni', xid.gtrid)
|
|
|
|
self.assertEqual('code', xid.bqual)
|
|
|
|
|
|
|
|
def test_xid_unicode_unparsed(self):
|
|
|
|
# We don't expect people shooting snowmen as transaction ids,
|
|
|
|
# so if something explodes in an encode error I don't mind.
|
|
|
|
# Let's just check uniconde is accepted as type.
|
|
|
|
cnn = self.connect()
|
|
|
|
cnn.set_client_encoding('utf8')
|
|
|
|
cnn.tpc_begin(u"transaction-id")
|
|
|
|
cnn.tpc_prepare()
|
|
|
|
cnn.reset()
|
|
|
|
|
2016-03-03 18:31:37 +03:00
|
|
|
xid = [x for x in cnn.tpc_recover() if x.database == dbname][0]
|
2010-10-15 11:27:07 +04:00
|
|
|
self.assertEqual(None, xid.format_id)
|
|
|
|
self.assertEqual('transaction-id', xid.gtrid)
|
|
|
|
self.assertEqual(None, xid.bqual)
|
|
|
|
|
2010-11-28 15:15:26 +03:00
|
|
|
def test_cancel_fails_prepared(self):
|
|
|
|
cnn = self.connect()
|
|
|
|
cnn.tpc_begin('cancel')
|
|
|
|
cnn.tpc_prepare()
|
|
|
|
self.assertRaises(psycopg2.ProgrammingError, cnn.cancel)
|
|
|
|
|
2012-08-15 02:29:19 +04:00
|
|
|
def test_tpc_recover_non_dbapi_connection(self):
|
2019-03-16 21:56:56 +03:00
|
|
|
cnn = self.connect(connection_factory=psycopg2.extras.RealDictConnection)
|
2012-08-15 02:29:19 +04:00
|
|
|
cnn.tpc_begin('dict-connection')
|
|
|
|
cnn.tpc_prepare()
|
|
|
|
cnn.reset()
|
|
|
|
|
|
|
|
xids = cnn.tpc_recover()
|
2016-03-03 18:31:37 +03:00
|
|
|
xid = [x for x in xids if x.database == dbname][0]
|
2012-08-15 02:29:19 +04:00
|
|
|
self.assertEqual(None, xid.format_id)
|
|
|
|
self.assertEqual('dict-connection', xid.gtrid)
|
|
|
|
self.assertEqual(None, xid.bqual)
|
|
|
|
|
|
|
|
|
2013-04-07 03:23:30 +04:00
|
|
|
class TransactionControlTests(ConnectingTestCase):
|
2011-11-17 03:51:05 +04:00
|
|
|
def test_closed(self):
|
|
|
|
self.conn.close()
|
|
|
|
self.assertRaises(psycopg2.InterfaceError,
|
|
|
|
self.conn.set_session,
|
2017-02-16 05:07:05 +03:00
|
|
|
ext.ISOLATION_LEVEL_SERIALIZABLE)
|
2011-11-17 03:51:05 +04:00
|
|
|
|
2011-06-01 12:07:02 +04:00
|
|
|
def test_not_in_transaction(self):
|
|
|
|
cur = self.conn.cursor()
|
|
|
|
cur.execute("select 1")
|
|
|
|
self.assertRaises(psycopg2.ProgrammingError,
|
2011-06-08 17:22:11 +04:00
|
|
|
self.conn.set_session,
|
2017-02-16 05:07:05 +03:00
|
|
|
ext.ISOLATION_LEVEL_SERIALIZABLE)
|
2011-06-01 12:07:02 +04:00
|
|
|
|
|
|
|
def test_set_isolation_level(self):
|
|
|
|
cur = self.conn.cursor()
|
2011-06-08 17:22:11 +04:00
|
|
|
self.conn.set_session(
|
2017-02-16 05:07:05 +03:00
|
|
|
ext.ISOLATION_LEVEL_SERIALIZABLE)
|
2017-02-04 13:57:30 +03:00
|
|
|
cur.execute("SHOW transaction_isolation;")
|
2011-06-01 12:07:02 +04:00
|
|
|
self.assertEqual(cur.fetchone()[0], 'serializable')
|
|
|
|
self.conn.rollback()
|
|
|
|
|
2011-06-08 17:22:11 +04:00
|
|
|
self.conn.set_session(
|
2017-02-16 05:07:05 +03:00
|
|
|
ext.ISOLATION_LEVEL_REPEATABLE_READ)
|
2017-02-04 13:57:30 +03:00
|
|
|
cur.execute("SHOW transaction_isolation;")
|
2018-10-13 05:28:42 +03:00
|
|
|
if self.conn.info.server_version > 80000:
|
2011-06-01 12:07:02 +04:00
|
|
|
self.assertEqual(cur.fetchone()[0], 'repeatable read')
|
|
|
|
else:
|
|
|
|
self.assertEqual(cur.fetchone()[0], 'serializable')
|
|
|
|
self.conn.rollback()
|
|
|
|
|
2011-06-08 17:22:11 +04:00
|
|
|
self.conn.set_session(
|
2017-02-16 05:07:05 +03:00
|
|
|
isolation_level=ext.ISOLATION_LEVEL_READ_COMMITTED)
|
2017-02-04 13:57:30 +03:00
|
|
|
cur.execute("SHOW transaction_isolation;")
|
2011-06-01 12:07:02 +04:00
|
|
|
self.assertEqual(cur.fetchone()[0], 'read committed')
|
|
|
|
self.conn.rollback()
|
|
|
|
|
2011-06-08 17:22:11 +04:00
|
|
|
self.conn.set_session(
|
2017-02-16 05:07:05 +03:00
|
|
|
isolation_level=ext.ISOLATION_LEVEL_READ_UNCOMMITTED)
|
2017-02-04 13:57:30 +03:00
|
|
|
cur.execute("SHOW transaction_isolation;")
|
2018-10-13 05:28:42 +03:00
|
|
|
if self.conn.info.server_version > 80000:
|
2011-12-15 16:53:48 +04:00
|
|
|
self.assertEqual(cur.fetchone()[0], 'read uncommitted')
|
|
|
|
else:
|
|
|
|
self.assertEqual(cur.fetchone()[0], 'read committed')
|
2011-06-01 12:07:02 +04:00
|
|
|
self.conn.rollback()
|
|
|
|
|
|
|
|
def test_set_isolation_level_str(self):
|
|
|
|
cur = self.conn.cursor()
|
2011-06-08 17:22:11 +04:00
|
|
|
self.conn.set_session("serializable")
|
2017-02-04 13:57:30 +03:00
|
|
|
cur.execute("SHOW transaction_isolation;")
|
2011-06-01 12:07:02 +04:00
|
|
|
self.assertEqual(cur.fetchone()[0], 'serializable')
|
|
|
|
self.conn.rollback()
|
|
|
|
|
2011-06-08 17:22:11 +04:00
|
|
|
self.conn.set_session("repeatable read")
|
2017-02-04 13:57:30 +03:00
|
|
|
cur.execute("SHOW transaction_isolation;")
|
2018-10-13 05:28:42 +03:00
|
|
|
if self.conn.info.server_version > 80000:
|
2011-06-01 12:07:02 +04:00
|
|
|
self.assertEqual(cur.fetchone()[0], 'repeatable read')
|
|
|
|
else:
|
|
|
|
self.assertEqual(cur.fetchone()[0], 'serializable')
|
|
|
|
self.conn.rollback()
|
|
|
|
|
2011-06-08 17:22:11 +04:00
|
|
|
self.conn.set_session("read committed")
|
2017-02-04 13:57:30 +03:00
|
|
|
cur.execute("SHOW transaction_isolation;")
|
2011-06-01 12:07:02 +04:00
|
|
|
self.assertEqual(cur.fetchone()[0], 'read committed')
|
|
|
|
self.conn.rollback()
|
|
|
|
|
2011-06-08 17:22:11 +04:00
|
|
|
self.conn.set_session("read uncommitted")
|
2017-02-04 13:57:30 +03:00
|
|
|
cur.execute("SHOW transaction_isolation;")
|
2018-10-13 05:28:42 +03:00
|
|
|
if self.conn.info.server_version > 80000:
|
2011-06-01 12:07:02 +04:00
|
|
|
self.assertEqual(cur.fetchone()[0], 'read uncommitted')
|
|
|
|
else:
|
|
|
|
self.assertEqual(cur.fetchone()[0], 'read committed')
|
|
|
|
self.conn.rollback()
|
|
|
|
|
|
|
|
def test_bad_isolation_level(self):
|
2011-06-08 17:22:11 +04:00
|
|
|
self.assertRaises(ValueError, self.conn.set_session, 0)
|
|
|
|
self.assertRaises(ValueError, self.conn.set_session, 5)
|
|
|
|
self.assertRaises(ValueError, self.conn.set_session, 'whatever')
|
2011-06-01 12:07:02 +04:00
|
|
|
|
|
|
|
def test_set_read_only(self):
|
2017-02-16 14:04:02 +03:00
|
|
|
self.assert_(self.conn.readonly is None)
|
|
|
|
|
2011-06-01 12:07:02 +04:00
|
|
|
cur = self.conn.cursor()
|
2011-06-08 17:22:11 +04:00
|
|
|
self.conn.set_session(readonly=True)
|
2017-02-16 14:04:02 +03:00
|
|
|
self.assert_(self.conn.readonly is True)
|
2017-02-04 13:57:30 +03:00
|
|
|
cur.execute("SHOW transaction_read_only;")
|
2011-06-01 12:07:02 +04:00
|
|
|
self.assertEqual(cur.fetchone()[0], 'on')
|
|
|
|
self.conn.rollback()
|
2017-02-04 13:57:30 +03:00
|
|
|
cur.execute("SHOW transaction_read_only;")
|
2011-06-01 12:07:02 +04:00
|
|
|
self.assertEqual(cur.fetchone()[0], 'on')
|
|
|
|
self.conn.rollback()
|
|
|
|
|
2017-02-16 14:04:02 +03:00
|
|
|
self.conn.set_session(readonly=False)
|
|
|
|
self.assert_(self.conn.readonly is False)
|
|
|
|
cur.execute("SHOW transaction_read_only;")
|
|
|
|
self.assertEqual(cur.fetchone()[0], 'off')
|
|
|
|
self.conn.rollback()
|
|
|
|
|
|
|
|
def test_setattr_read_only(self):
|
2011-06-01 12:07:02 +04:00
|
|
|
cur = self.conn.cursor()
|
2017-02-16 14:04:02 +03:00
|
|
|
self.conn.readonly = True
|
|
|
|
self.assert_(self.conn.readonly is True)
|
|
|
|
cur.execute("SHOW transaction_read_only;")
|
|
|
|
self.assertEqual(cur.fetchone()[0], 'on')
|
|
|
|
self.assertRaises(self.conn.ProgrammingError,
|
|
|
|
setattr, self.conn, 'readonly', False)
|
|
|
|
self.assert_(self.conn.readonly is True)
|
|
|
|
self.conn.rollback()
|
2017-02-04 13:57:30 +03:00
|
|
|
cur.execute("SHOW transaction_read_only;")
|
2011-06-01 12:07:02 +04:00
|
|
|
self.assertEqual(cur.fetchone()[0], 'on')
|
|
|
|
self.conn.rollback()
|
|
|
|
|
2017-02-16 14:04:02 +03:00
|
|
|
cur = self.conn.cursor()
|
|
|
|
self.conn.readonly = None
|
|
|
|
self.assert_(self.conn.readonly is None)
|
|
|
|
cur.execute("SHOW transaction_read_only;")
|
|
|
|
self.assertEqual(cur.fetchone()[0], 'off') # assume defined by server
|
|
|
|
self.conn.rollback()
|
|
|
|
|
|
|
|
self.conn.readonly = False
|
|
|
|
self.assert_(self.conn.readonly is False)
|
2017-02-04 13:57:30 +03:00
|
|
|
cur.execute("SHOW transaction_read_only;")
|
2011-06-01 12:07:02 +04:00
|
|
|
self.assertEqual(cur.fetchone()[0], 'off')
|
|
|
|
self.conn.rollback()
|
|
|
|
|
|
|
|
def test_set_default(self):
|
|
|
|
cur = self.conn.cursor()
|
2017-02-04 13:57:30 +03:00
|
|
|
cur.execute("SHOW transaction_isolation;")
|
|
|
|
isolevel = cur.fetchone()[0]
|
|
|
|
cur.execute("SHOW transaction_read_only;")
|
|
|
|
readonly = cur.fetchone()[0]
|
2011-06-01 12:07:02 +04:00
|
|
|
self.conn.rollback()
|
|
|
|
|
2011-06-08 17:22:11 +04:00
|
|
|
self.conn.set_session(isolation_level='serializable', readonly=True)
|
|
|
|
self.conn.set_session(isolation_level='default', readonly='default')
|
2011-06-01 12:07:02 +04:00
|
|
|
|
2017-02-04 13:57:30 +03:00
|
|
|
cur.execute("SHOW transaction_isolation;")
|
|
|
|
self.assertEqual(cur.fetchone()[0], isolevel)
|
|
|
|
cur.execute("SHOW transaction_read_only;")
|
|
|
|
self.assertEqual(cur.fetchone()[0], readonly)
|
2011-06-01 12:07:02 +04:00
|
|
|
|
|
|
|
@skip_before_postgres(9, 1)
|
|
|
|
def test_set_deferrable(self):
|
2017-02-16 14:04:02 +03:00
|
|
|
self.assert_(self.conn.deferrable is None)
|
2011-06-01 12:07:02 +04:00
|
|
|
cur = self.conn.cursor()
|
2011-06-08 17:22:11 +04:00
|
|
|
self.conn.set_session(readonly=True, deferrable=True)
|
2017-02-16 14:04:02 +03:00
|
|
|
self.assert_(self.conn.deferrable is True)
|
2017-02-04 13:57:30 +03:00
|
|
|
cur.execute("SHOW transaction_read_only;")
|
2011-06-01 12:07:02 +04:00
|
|
|
self.assertEqual(cur.fetchone()[0], 'on')
|
2017-02-04 13:57:30 +03:00
|
|
|
cur.execute("SHOW transaction_deferrable;")
|
2011-06-01 12:07:02 +04:00
|
|
|
self.assertEqual(cur.fetchone()[0], 'on')
|
|
|
|
self.conn.rollback()
|
2017-02-04 13:57:30 +03:00
|
|
|
cur.execute("SHOW transaction_deferrable;")
|
2011-06-01 12:07:02 +04:00
|
|
|
self.assertEqual(cur.fetchone()[0], 'on')
|
|
|
|
self.conn.rollback()
|
|
|
|
|
2011-06-08 17:22:11 +04:00
|
|
|
self.conn.set_session(deferrable=False)
|
2017-02-16 14:04:02 +03:00
|
|
|
self.assert_(self.conn.deferrable is False)
|
2017-02-04 13:57:30 +03:00
|
|
|
cur.execute("SHOW transaction_read_only;")
|
2011-06-01 12:07:02 +04:00
|
|
|
self.assertEqual(cur.fetchone()[0], 'on')
|
2017-02-04 13:57:30 +03:00
|
|
|
cur.execute("SHOW transaction_deferrable;")
|
2011-06-01 12:07:02 +04:00
|
|
|
self.assertEqual(cur.fetchone()[0], 'off')
|
|
|
|
self.conn.rollback()
|
|
|
|
|
|
|
|
@skip_after_postgres(9, 1)
|
|
|
|
def test_set_deferrable_error(self):
|
|
|
|
self.assertRaises(psycopg2.ProgrammingError,
|
2011-06-08 17:22:11 +04:00
|
|
|
self.conn.set_session, readonly=True, deferrable=True)
|
2017-02-16 14:04:02 +03:00
|
|
|
self.assertRaises(psycopg2.ProgrammingError,
|
|
|
|
setattr, self.conn, 'deferrable', True)
|
|
|
|
|
|
|
|
@skip_before_postgres(9, 1)
|
|
|
|
def test_setattr_deferrable(self):
|
|
|
|
cur = self.conn.cursor()
|
|
|
|
self.conn.deferrable = True
|
|
|
|
self.assert_(self.conn.deferrable is True)
|
|
|
|
cur.execute("SHOW transaction_deferrable;")
|
|
|
|
self.assertEqual(cur.fetchone()[0], 'on')
|
|
|
|
self.assertRaises(self.conn.ProgrammingError,
|
|
|
|
setattr, self.conn, 'deferrable', False)
|
|
|
|
self.assert_(self.conn.deferrable is True)
|
|
|
|
self.conn.rollback()
|
|
|
|
cur.execute("SHOW transaction_deferrable;")
|
|
|
|
self.assertEqual(cur.fetchone()[0], 'on')
|
|
|
|
self.conn.rollback()
|
|
|
|
|
|
|
|
cur = self.conn.cursor()
|
|
|
|
self.conn.deferrable = None
|
|
|
|
self.assert_(self.conn.deferrable is None)
|
|
|
|
cur.execute("SHOW transaction_deferrable;")
|
|
|
|
self.assertEqual(cur.fetchone()[0], 'off') # assume defined by server
|
|
|
|
self.conn.rollback()
|
|
|
|
|
|
|
|
self.conn.deferrable = False
|
|
|
|
self.assert_(self.conn.deferrable is False)
|
|
|
|
cur.execute("SHOW transaction_deferrable;")
|
|
|
|
self.assertEqual(cur.fetchone()[0], 'off')
|
|
|
|
self.conn.rollback()
|
|
|
|
|
|
|
|
def test_mixing_session_attribs(self):
|
|
|
|
cur = self.conn.cursor()
|
|
|
|
self.conn.autocommit = True
|
|
|
|
self.conn.readonly = True
|
|
|
|
|
|
|
|
cur.execute("SHOW transaction_read_only;")
|
|
|
|
self.assertEqual(cur.fetchone()[0], 'on')
|
|
|
|
|
|
|
|
cur.execute("SHOW default_transaction_read_only;")
|
|
|
|
self.assertEqual(cur.fetchone()[0], 'on')
|
|
|
|
|
|
|
|
self.conn.autocommit = False
|
|
|
|
cur.execute("SHOW transaction_read_only;")
|
|
|
|
self.assertEqual(cur.fetchone()[0], 'on')
|
|
|
|
|
|
|
|
cur.execute("SHOW default_transaction_read_only;")
|
|
|
|
self.assertEqual(cur.fetchone()[0], 'off')
|
2011-06-01 12:07:02 +04:00
|
|
|
|
2018-05-20 21:18:42 +03:00
|
|
|
def test_idempotence_check(self):
|
|
|
|
self.conn.autocommit = False
|
|
|
|
self.conn.readonly = True
|
|
|
|
self.conn.autocommit = True
|
|
|
|
self.conn.readonly = True
|
|
|
|
|
|
|
|
cur = self.conn.cursor()
|
|
|
|
cur.execute("SHOW transaction_read_only")
|
|
|
|
self.assertEqual(cur.fetchone()[0], 'on')
|
|
|
|
|
|
|
|
|
|
|
|
class TestEncryptPassword(ConnectingTestCase):
|
2017-07-17 08:02:56 +03:00
|
|
|
@skip_before_postgres(10)
|
|
|
|
def test_encrypt_password_post_9_6(self):
|
|
|
|
# MD5 algorithm
|
|
|
|
self.assertEqual(
|
2017-09-14 21:12:37 +03:00
|
|
|
ext.encrypt_password('psycopg2', 'ashesh', self.conn, 'md5'),
|
2018-08-17 02:55:40 +03:00
|
|
|
'md594839d658c28a357126f105b9cb14cfc')
|
2017-07-17 08:02:56 +03:00
|
|
|
|
2018-05-20 22:50:04 +03:00
|
|
|
# keywords
|
|
|
|
self.assertEqual(
|
|
|
|
ext.encrypt_password(
|
|
|
|
password='psycopg2', user='ashesh',
|
|
|
|
scope=self.conn, algorithm='md5'),
|
2018-08-17 02:55:40 +03:00
|
|
|
'md594839d658c28a357126f105b9cb14cfc')
|
2017-07-17 08:02:56 +03:00
|
|
|
|
2019-02-17 00:56:44 +03:00
|
|
|
@skip_before_libpq(10)
|
2018-08-17 02:55:40 +03:00
|
|
|
@skip_before_postgres(10)
|
|
|
|
def test_encrypt_server(self):
|
|
|
|
cur = self.conn.cursor()
|
|
|
|
cur.execute("SHOW password_encryption;")
|
|
|
|
server_encryption_algorithm = cur.fetchone()[0]
|
2017-07-17 08:02:56 +03:00
|
|
|
|
2018-08-17 02:55:40 +03:00
|
|
|
enc_password = ext.encrypt_password(
|
|
|
|
'psycopg2', 'ashesh', self.conn)
|
|
|
|
|
|
|
|
if server_encryption_algorithm == 'md5':
|
|
|
|
self.assertEqual(
|
|
|
|
enc_password, 'md594839d658c28a357126f105b9cb14cfc')
|
|
|
|
elif server_encryption_algorithm == 'scram-sha-256':
|
|
|
|
self.assertEqual(enc_password[:14], 'SCRAM-SHA-256$')
|
2018-05-20 22:50:04 +03:00
|
|
|
|
2017-07-17 08:02:56 +03:00
|
|
|
self.assertEqual(
|
2018-08-17 02:55:40 +03:00
|
|
|
ext.encrypt_password(
|
|
|
|
'psycopg2', 'ashesh', self.conn, 'scram-sha-256'
|
|
|
|
)[:14], 'SCRAM-SHA-256$')
|
2017-07-17 08:02:56 +03:00
|
|
|
|
2018-05-20 21:18:42 +03:00
|
|
|
self.assertRaises(psycopg2.ProgrammingError,
|
|
|
|
ext.encrypt_password, 'psycopg2', 'ashesh', self.conn, 'abc')
|
2018-01-11 04:59:49 +03:00
|
|
|
|
2018-05-20 22:50:04 +03:00
|
|
|
def test_encrypt_md5(self):
|
|
|
|
self.assertEqual(
|
|
|
|
ext.encrypt_password('psycopg2', 'ashesh', algorithm='md5'),
|
2018-08-17 02:55:40 +03:00
|
|
|
'md594839d658c28a357126f105b9cb14cfc')
|
2018-05-20 22:50:04 +03:00
|
|
|
|
2018-08-17 02:55:40 +03:00
|
|
|
@skip_before_libpq(10)
|
|
|
|
def test_encrypt_bad_libpq_10(self):
|
|
|
|
self.assertRaises(psycopg2.ProgrammingError,
|
|
|
|
ext.encrypt_password, 'psycopg2', 'ashesh', self.conn, 'abc')
|
|
|
|
|
|
|
|
@skip_after_libpq(10)
|
|
|
|
def test_encrypt_bad_before_libpq_10(self):
|
|
|
|
self.assertRaises(psycopg2.NotSupportedError,
|
|
|
|
ext.encrypt_password, 'psycopg2', 'ashesh', self.conn, 'abc')
|
|
|
|
|
|
|
|
@skip_before_libpq(10)
|
2018-05-20 22:50:04 +03:00
|
|
|
def test_encrypt_scram(self):
|
2018-08-17 02:55:40 +03:00
|
|
|
self.assert_(
|
|
|
|
ext.encrypt_password(
|
|
|
|
'psycopg2', 'ashesh', self.conn, 'scram-sha-256')
|
|
|
|
.startswith('SCRAM-SHA-256$'))
|
|
|
|
|
|
|
|
@skip_after_libpq(10)
|
|
|
|
def test_encrypt_scram_pre_10(self):
|
|
|
|
self.assertRaises(psycopg2.NotSupportedError,
|
|
|
|
ext.encrypt_password,
|
|
|
|
password='psycopg2', user='ashesh',
|
|
|
|
scope=self.conn, algorithm='scram-sha-256')
|
2018-05-20 22:50:04 +03:00
|
|
|
|
2018-05-20 23:18:36 +03:00
|
|
|
def test_bad_types(self):
|
|
|
|
self.assertRaises(TypeError, ext.encrypt_password)
|
|
|
|
self.assertRaises(TypeError, ext.encrypt_password,
|
|
|
|
'password', 42, self.conn, 'md5')
|
|
|
|
self.assertRaises(TypeError, ext.encrypt_password,
|
|
|
|
42, 'user', self.conn, 'md5')
|
|
|
|
self.assertRaises(TypeError, ext.encrypt_password,
|
|
|
|
42, 'user', 'wat', 'abc')
|
|
|
|
self.assertRaises(TypeError, ext.encrypt_password,
|
|
|
|
'password', 'user', 'wat', 42)
|
|
|
|
|
2011-06-01 12:07:02 +04:00
|
|
|
|
2013-04-07 03:23:30 +04:00
|
|
|
class AutocommitTests(ConnectingTestCase):
|
2011-11-17 03:51:05 +04:00
|
|
|
def test_closed(self):
|
|
|
|
self.conn.close()
|
|
|
|
self.assertRaises(psycopg2.InterfaceError,
|
|
|
|
setattr, self.conn, 'autocommit', True)
|
|
|
|
|
|
|
|
# The getter doesn't have a guard. We may change this in future
|
|
|
|
# to make it consistent with other methods; meanwhile let's just check
|
|
|
|
# it doesn't explode.
|
|
|
|
try:
|
|
|
|
self.assert_(self.conn.autocommit in (True, False))
|
|
|
|
except psycopg2.InterfaceError:
|
|
|
|
pass
|
|
|
|
|
2011-06-02 04:16:22 +04:00
|
|
|
def test_default_no_autocommit(self):
|
|
|
|
self.assert_(not self.conn.autocommit)
|
2017-02-16 05:07:05 +03:00
|
|
|
self.assertEqual(self.conn.status, ext.STATUS_READY)
|
2018-10-13 05:28:42 +03:00
|
|
|
self.assertEqual(self.conn.info.transaction_status,
|
2017-02-16 05:07:05 +03:00
|
|
|
ext.TRANSACTION_STATUS_IDLE)
|
2011-06-02 04:16:22 +04:00
|
|
|
|
|
|
|
cur = self.conn.cursor()
|
|
|
|
cur.execute('select 1;')
|
2017-02-16 05:07:05 +03:00
|
|
|
self.assertEqual(self.conn.status, ext.STATUS_BEGIN)
|
2018-10-13 05:28:42 +03:00
|
|
|
self.assertEqual(self.conn.info.transaction_status,
|
2017-02-16 05:07:05 +03:00
|
|
|
ext.TRANSACTION_STATUS_INTRANS)
|
2011-06-02 04:16:22 +04:00
|
|
|
|
|
|
|
self.conn.rollback()
|
2017-02-16 05:07:05 +03:00
|
|
|
self.assertEqual(self.conn.status, ext.STATUS_READY)
|
2018-10-13 05:28:42 +03:00
|
|
|
self.assertEqual(self.conn.info.transaction_status,
|
2017-02-16 05:07:05 +03:00
|
|
|
ext.TRANSACTION_STATUS_IDLE)
|
2011-06-02 04:16:22 +04:00
|
|
|
|
|
|
|
def test_set_autocommit(self):
|
|
|
|
self.conn.autocommit = True
|
|
|
|
self.assert_(self.conn.autocommit)
|
2017-02-16 05:07:05 +03:00
|
|
|
self.assertEqual(self.conn.status, ext.STATUS_READY)
|
2018-10-13 05:28:42 +03:00
|
|
|
self.assertEqual(self.conn.info.transaction_status,
|
2017-02-16 05:07:05 +03:00
|
|
|
ext.TRANSACTION_STATUS_IDLE)
|
2011-06-02 04:16:22 +04:00
|
|
|
|
|
|
|
cur = self.conn.cursor()
|
|
|
|
cur.execute('select 1;')
|
2017-02-16 05:07:05 +03:00
|
|
|
self.assertEqual(self.conn.status, ext.STATUS_READY)
|
2018-10-13 05:28:42 +03:00
|
|
|
self.assertEqual(self.conn.info.transaction_status,
|
2017-02-16 05:07:05 +03:00
|
|
|
ext.TRANSACTION_STATUS_IDLE)
|
2011-06-02 04:16:22 +04:00
|
|
|
|
|
|
|
self.conn.autocommit = False
|
|
|
|
self.assert_(not self.conn.autocommit)
|
2017-02-16 05:07:05 +03:00
|
|
|
self.assertEqual(self.conn.status, ext.STATUS_READY)
|
2018-10-13 05:28:42 +03:00
|
|
|
self.assertEqual(self.conn.info.transaction_status,
|
2017-02-16 05:07:05 +03:00
|
|
|
ext.TRANSACTION_STATUS_IDLE)
|
2011-06-02 04:16:22 +04:00
|
|
|
|
|
|
|
cur.execute('select 1;')
|
2017-02-16 05:07:05 +03:00
|
|
|
self.assertEqual(self.conn.status, ext.STATUS_BEGIN)
|
2018-10-13 05:28:42 +03:00
|
|
|
self.assertEqual(self.conn.info.transaction_status,
|
2017-02-16 05:07:05 +03:00
|
|
|
ext.TRANSACTION_STATUS_INTRANS)
|
2011-06-02 04:16:22 +04:00
|
|
|
|
|
|
|
def test_set_intrans_error(self):
|
|
|
|
cur = self.conn.cursor()
|
|
|
|
cur.execute('select 1;')
|
|
|
|
self.assertRaises(psycopg2.ProgrammingError,
|
|
|
|
setattr, self.conn, 'autocommit', True)
|
|
|
|
|
2011-06-08 17:22:11 +04:00
|
|
|
def test_set_session_autocommit(self):
|
|
|
|
self.conn.set_session(autocommit=True)
|
2011-06-02 04:16:22 +04:00
|
|
|
self.assert_(self.conn.autocommit)
|
2017-02-16 05:07:05 +03:00
|
|
|
self.assertEqual(self.conn.status, ext.STATUS_READY)
|
2018-10-13 05:28:42 +03:00
|
|
|
self.assertEqual(self.conn.info.transaction_status,
|
2017-02-16 05:07:05 +03:00
|
|
|
ext.TRANSACTION_STATUS_IDLE)
|
2011-06-02 04:16:22 +04:00
|
|
|
|
|
|
|
cur = self.conn.cursor()
|
|
|
|
cur.execute('select 1;')
|
2017-02-16 05:07:05 +03:00
|
|
|
self.assertEqual(self.conn.status, ext.STATUS_READY)
|
2018-10-13 05:28:42 +03:00
|
|
|
self.assertEqual(self.conn.info.transaction_status,
|
2017-02-16 05:07:05 +03:00
|
|
|
ext.TRANSACTION_STATUS_IDLE)
|
2011-06-02 04:16:22 +04:00
|
|
|
|
2011-06-08 17:22:11 +04:00
|
|
|
self.conn.set_session(autocommit=False)
|
2011-06-02 04:16:22 +04:00
|
|
|
self.assert_(not self.conn.autocommit)
|
2017-02-16 05:07:05 +03:00
|
|
|
self.assertEqual(self.conn.status, ext.STATUS_READY)
|
2018-10-13 05:28:42 +03:00
|
|
|
self.assertEqual(self.conn.info.transaction_status,
|
2017-02-16 05:07:05 +03:00
|
|
|
ext.TRANSACTION_STATUS_IDLE)
|
2011-06-02 04:16:22 +04:00
|
|
|
|
|
|
|
cur.execute('select 1;')
|
2017-02-16 05:07:05 +03:00
|
|
|
self.assertEqual(self.conn.status, ext.STATUS_BEGIN)
|
2018-10-13 05:28:42 +03:00
|
|
|
self.assertEqual(self.conn.info.transaction_status,
|
2017-02-16 05:07:05 +03:00
|
|
|
ext.TRANSACTION_STATUS_INTRANS)
|
2011-06-02 04:16:22 +04:00
|
|
|
self.conn.rollback()
|
|
|
|
|
2011-06-08 17:22:11 +04:00
|
|
|
self.conn.set_session('serializable', readonly=True, autocommit=True)
|
2011-06-02 04:16:22 +04:00
|
|
|
self.assert_(self.conn.autocommit)
|
|
|
|
cur.execute('select 1;')
|
2017-02-16 05:07:05 +03:00
|
|
|
self.assertEqual(self.conn.status, ext.STATUS_READY)
|
2018-10-13 05:28:42 +03:00
|
|
|
self.assertEqual(self.conn.info.transaction_status,
|
2017-02-16 05:07:05 +03:00
|
|
|
ext.TRANSACTION_STATUS_IDLE)
|
2017-02-04 13:57:30 +03:00
|
|
|
cur.execute("SHOW transaction_isolation;")
|
2011-06-02 04:16:22 +04:00
|
|
|
self.assertEqual(cur.fetchone()[0], 'serializable')
|
2017-02-04 13:57:30 +03:00
|
|
|
cur.execute("SHOW transaction_read_only;")
|
2011-06-02 04:16:22 +04:00
|
|
|
self.assertEqual(cur.fetchone()[0], 'on')
|
|
|
|
|
|
|
|
|
2017-03-15 19:00:40 +03:00
|
|
|
class PasswordLeakTestCase(ConnectingTestCase):
|
|
|
|
def setUp(self):
|
|
|
|
super(PasswordLeakTestCase, self).setUp()
|
|
|
|
PasswordLeakTestCase.dsn = None
|
|
|
|
|
|
|
|
class GrassingConnection(ext.connection):
|
|
|
|
"""A connection snitching the dsn away.
|
|
|
|
|
|
|
|
This connection passes the dsn to the test case class even if init
|
|
|
|
fails (e.g. connection error). Test that we mangle the dsn ok anyway.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
try:
|
|
|
|
super(PasswordLeakTestCase.GrassingConnection, self).__init__(
|
|
|
|
*args, **kwargs)
|
|
|
|
finally:
|
|
|
|
# The connection is not initialized entirely, however the C
|
|
|
|
# code should have set the dsn, and it should have scrubbed
|
|
|
|
# the password away
|
|
|
|
PasswordLeakTestCase.dsn = self.dsn
|
|
|
|
|
|
|
|
def test_leak(self):
|
|
|
|
self.assertRaises(psycopg2.DatabaseError,
|
|
|
|
self.GrassingConnection, "dbname=nosuch password=whateva")
|
2017-03-16 06:33:19 +03:00
|
|
|
self.assertDsnEqual(self.dsn, "dbname=nosuch password=xxx")
|
2017-03-15 19:00:40 +03:00
|
|
|
|
2017-03-16 06:33:19 +03:00
|
|
|
@skip_before_libpq(9, 2)
|
2017-03-15 19:00:40 +03:00
|
|
|
def test_url_leak(self):
|
|
|
|
self.assertRaises(psycopg2.DatabaseError,
|
|
|
|
self.GrassingConnection,
|
|
|
|
"postgres://someone:whateva@localhost/nosuch")
|
|
|
|
|
2017-03-16 06:33:19 +03:00
|
|
|
self.assertDsnEqual(self.dsn,
|
|
|
|
"user=someone password=xxx host=localhost dbname=nosuch")
|
2017-03-15 19:00:40 +03:00
|
|
|
|
|
|
|
|
2017-06-15 19:22:32 +03:00
|
|
|
class SignalTestCase(ConnectingTestCase):
|
|
|
|
@slow
|
2017-07-22 04:09:24 +03:00
|
|
|
@skip_before_postgres(8, 2)
|
2017-06-15 19:22:32 +03:00
|
|
|
def test_bug_551_returning(self):
|
|
|
|
# Raise an exception trying to decode 'id'
|
|
|
|
self._test_bug_551(query="""
|
|
|
|
INSERT INTO test551 (num) VALUES (%s) RETURNING id
|
|
|
|
""")
|
|
|
|
|
|
|
|
@slow
|
|
|
|
def test_bug_551_no_returning(self):
|
|
|
|
# Raise an exception trying to decode 'INSERT 0 1'
|
|
|
|
self._test_bug_551(query="""
|
|
|
|
INSERT INTO test551 (num) VALUES (%s)
|
|
|
|
""")
|
|
|
|
|
|
|
|
def _test_bug_551(self, query):
|
|
|
|
script = ("""\
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import time
|
|
|
|
import signal
|
2018-01-29 03:45:40 +03:00
|
|
|
import warnings
|
2017-06-15 19:22:32 +03:00
|
|
|
import threading
|
|
|
|
|
2018-01-29 03:45:40 +03:00
|
|
|
# ignore wheel deprecation warning
|
|
|
|
with warnings.catch_warnings():
|
|
|
|
warnings.simplefilter('ignore')
|
|
|
|
import psycopg2
|
2017-06-15 19:22:32 +03:00
|
|
|
|
|
|
|
def handle_sigabort(sig, frame):
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
def killer():
|
|
|
|
time.sleep(0.5)
|
|
|
|
os.kill(os.getpid(), signal.SIGABRT)
|
|
|
|
|
|
|
|
signal.signal(signal.SIGABRT, handle_sigabort)
|
|
|
|
|
|
|
|
conn = psycopg2.connect(%(dsn)r)
|
|
|
|
|
|
|
|
cur = conn.cursor()
|
|
|
|
|
|
|
|
cur.execute("create table test551 (id serial, num varchar(50))")
|
|
|
|
|
|
|
|
t = threading.Thread(target=killer)
|
|
|
|
t.daemon = True
|
|
|
|
t.start()
|
|
|
|
|
|
|
|
while True:
|
|
|
|
cur.execute(%(query)r, ("Hello, world!",))
|
|
|
|
""" % {'dsn': dsn, 'query': query})
|
|
|
|
|
2017-11-29 07:58:41 +03:00
|
|
|
proc = sp.Popen([sys.executable, '-c', script],
|
2017-06-15 19:22:32 +03:00
|
|
|
stdout=sp.PIPE, stderr=sp.PIPE)
|
|
|
|
(out, err) = proc.communicate()
|
2017-06-15 19:39:00 +03:00
|
|
|
self.assertNotEqual(proc.returncode, 0)
|
2017-06-17 05:20:09 +03:00
|
|
|
# Strip [NNN refs] from output
|
|
|
|
err = re.sub(br'\[[^\]]+\]', b'', err).strip()
|
2017-06-15 19:22:32 +03:00
|
|
|
self.assert_(not err, err)
|
|
|
|
|
|
|
|
|
2018-10-12 00:42:52 +03:00
|
|
|
class TestConnectionInfo(ConnectingTestCase):
|
2018-10-12 05:25:35 +03:00
|
|
|
def setUp(self):
|
|
|
|
ConnectingTestCase.setUp(self)
|
|
|
|
|
|
|
|
class BrokenConn(psycopg2.extensions.connection):
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
# don't call superclass
|
|
|
|
pass
|
|
|
|
|
|
|
|
# A "broken" connection
|
|
|
|
self.bconn = self.connect(connection_factory=BrokenConn)
|
|
|
|
|
|
|
|
def test_dbname(self):
|
|
|
|
self.assert_(isinstance(self.conn.info.dbname, str))
|
|
|
|
self.assert_(self.bconn.info.dbname is None)
|
|
|
|
|
|
|
|
def test_user(self):
|
2018-10-12 06:18:07 +03:00
|
|
|
cur = self.conn.cursor()
|
|
|
|
cur.execute("select user")
|
|
|
|
self.assertEqual(self.conn.info.user, cur.fetchone()[0])
|
2018-10-12 05:25:35 +03:00
|
|
|
self.assert_(self.bconn.info.user is None)
|
|
|
|
|
|
|
|
def test_password(self):
|
|
|
|
self.assert_(isinstance(self.conn.info.password, str))
|
|
|
|
self.assert_(self.bconn.info.password is None)
|
|
|
|
|
2018-10-05 10:23:13 +03:00
|
|
|
def test_host(self):
|
|
|
|
expected = dbhost if dbhost else "/"
|
2018-10-12 00:42:52 +03:00
|
|
|
self.assertIn(expected, self.conn.info.host)
|
2018-10-12 05:25:35 +03:00
|
|
|
self.assert_(self.bconn.info.host is None)
|
2018-10-05 10:23:13 +03:00
|
|
|
|
|
|
|
def test_host_readonly(self):
|
|
|
|
with self.assertRaises(AttributeError):
|
2018-10-12 00:42:52 +03:00
|
|
|
self.conn.info.host = 'override'
|
2018-10-05 10:23:13 +03:00
|
|
|
|
2018-10-12 05:25:35 +03:00
|
|
|
def test_port(self):
|
|
|
|
self.assert_(isinstance(self.conn.info.port, int))
|
|
|
|
self.assert_(self.bconn.info.port is None)
|
|
|
|
|
|
|
|
def test_options(self):
|
|
|
|
self.assert_(isinstance(self.conn.info.options, str))
|
|
|
|
self.assert_(self.bconn.info.options is None)
|
|
|
|
|
2019-02-17 05:37:09 +03:00
|
|
|
@skip_before_libpq(9, 3)
|
|
|
|
def test_dsn_parameters(self):
|
|
|
|
d = self.conn.info.dsn_parameters
|
|
|
|
self.assert_(isinstance(d, dict))
|
|
|
|
self.assertEqual(d['dbname'], dbname) # the only param we can check reliably
|
|
|
|
self.assert_('password' not in d, d)
|
|
|
|
|
2018-10-12 05:25:35 +03:00
|
|
|
def test_status(self):
|
|
|
|
self.assertEqual(self.conn.info.status, 0)
|
|
|
|
self.assertEqual(self.bconn.info.status, 1)
|
|
|
|
|
2018-10-12 06:18:07 +03:00
|
|
|
def test_transaction_status(self):
|
|
|
|
self.assertEqual(self.conn.info.transaction_status, 0)
|
|
|
|
cur = self.conn.cursor()
|
|
|
|
cur.execute("select 1")
|
|
|
|
self.assertEqual(self.conn.info.transaction_status, 2)
|
|
|
|
self.assertEqual(self.bconn.info.transaction_status, 4)
|
|
|
|
|
2018-10-13 04:40:17 +03:00
|
|
|
def test_parameter_status(self):
|
|
|
|
cur = self.conn.cursor()
|
|
|
|
try:
|
|
|
|
cur.execute("show server_version")
|
|
|
|
except psycopg2.DatabaseError:
|
|
|
|
self.assertIsInstance(
|
|
|
|
self.conn.info.parameter_status('server_version'), str)
|
|
|
|
else:
|
|
|
|
self.assertEqual(
|
|
|
|
self.conn.info.parameter_status('server_version'),
|
|
|
|
cur.fetchone()[0])
|
|
|
|
|
|
|
|
self.assertIsNone(self.conn.info.parameter_status('wat'))
|
|
|
|
self.assertIsNone(self.bconn.info.parameter_status('server_version'))
|
|
|
|
|
2018-10-12 06:18:07 +03:00
|
|
|
def test_protocol_version(self):
|
|
|
|
self.assertEqual(self.conn.info.protocol_version, 3)
|
|
|
|
self.assertEqual(self.bconn.info.protocol_version, 0)
|
|
|
|
|
|
|
|
def test_server_version(self):
|
|
|
|
cur = self.conn.cursor()
|
|
|
|
try:
|
|
|
|
cur.execute("show server_version_num")
|
|
|
|
except psycopg2.DatabaseError:
|
|
|
|
self.assert_(isinstance(self.conn.info.server_version, int))
|
|
|
|
else:
|
|
|
|
self.assertEqual(
|
|
|
|
self.conn.info.server_version, int(cur.fetchone()[0]))
|
|
|
|
|
|
|
|
self.assertEqual(self.bconn.info.server_version, 0)
|
|
|
|
|
2018-10-13 02:47:04 +03:00
|
|
|
def test_error_message(self):
|
|
|
|
self.assertIsNone(self.conn.info.error_message)
|
|
|
|
self.assertIsNotNone(self.bconn.info.error_message)
|
|
|
|
|
|
|
|
cur = self.conn.cursor()
|
|
|
|
try:
|
|
|
|
cur.execute("select 1 from nosuchtable")
|
|
|
|
except psycopg2.DatabaseError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
self.assert_('nosuchtable' in self.conn.info.error_message)
|
|
|
|
|
2018-10-13 03:36:07 +03:00
|
|
|
def test_socket(self):
|
|
|
|
self.assert_(self.conn.info.socket >= 0)
|
|
|
|
self.assert_(self.bconn.info.socket < 0)
|
|
|
|
|
|
|
|
def test_backend_pid(self):
|
|
|
|
cur = self.conn.cursor()
|
|
|
|
try:
|
|
|
|
cur.execute("select pg_backend_pid()")
|
|
|
|
except psycopg2.DatabaseError:
|
|
|
|
self.assert_(self.conn.info.backend_pid > 0)
|
|
|
|
else:
|
|
|
|
self.assertEqual(
|
|
|
|
self.conn.info.backend_pid, int(cur.fetchone()[0]))
|
|
|
|
|
|
|
|
self.assert_(self.bconn.info.backend_pid == 0)
|
|
|
|
|
|
|
|
def test_needs_password(self):
|
|
|
|
self.assertIs(self.conn.info.needs_password, False)
|
|
|
|
self.assertIs(self.bconn.info.needs_password, False)
|
|
|
|
|
|
|
|
def test_used_password(self):
|
|
|
|
self.assertIsInstance(self.conn.info.used_password, bool)
|
|
|
|
self.assertIs(self.bconn.info.used_password, False)
|
|
|
|
|
2018-10-13 05:09:39 +03:00
|
|
|
@skip_before_libpq(9, 5)
|
2018-10-13 03:36:07 +03:00
|
|
|
def test_ssl_in_use(self):
|
|
|
|
self.assertIsInstance(self.conn.info.ssl_in_use, bool)
|
|
|
|
self.assertIs(self.bconn.info.ssl_in_use, False)
|
|
|
|
|
2018-10-13 05:09:39 +03:00
|
|
|
@skip_after_libpq(9, 5)
|
|
|
|
def test_ssl_not_supported(self):
|
|
|
|
with self.assertRaises(psycopg2.NotSupportedError):
|
|
|
|
self.conn.info.ssl_in_use
|
|
|
|
with self.assertRaises(psycopg2.NotSupportedError):
|
|
|
|
self.conn.info.ssl_attribute_names
|
|
|
|
with self.assertRaises(psycopg2.NotSupportedError):
|
|
|
|
self.conn.info.ssl_attribute('wat')
|
|
|
|
|
|
|
|
@skip_before_libpq(9, 5)
|
2018-10-13 04:21:38 +03:00
|
|
|
def test_ssl_attribute(self):
|
|
|
|
attribs = self.conn.info.ssl_attribute_names
|
|
|
|
self.assert_(attribs)
|
|
|
|
if self.conn.info.ssl_in_use:
|
|
|
|
for attrib in attribs:
|
|
|
|
self.assertIsInstance(self.conn.info.ssl_attribute(attrib), str)
|
|
|
|
else:
|
|
|
|
for attrib in attribs:
|
|
|
|
self.assertIsNone(self.conn.info.ssl_attribute(attrib))
|
|
|
|
|
|
|
|
self.assertIsNone(self.conn.info.ssl_attribute('wat'))
|
2018-10-05 10:23:13 +03:00
|
|
|
|
2018-10-13 04:21:38 +03:00
|
|
|
for attrib in attribs:
|
|
|
|
self.assertIsNone(self.bconn.info.ssl_attribute(attrib))
|
2018-10-05 10:23:13 +03:00
|
|
|
|
|
|
|
|
2008-01-19 06:32:42 +03:00
|
|
|
def test_suite():
|
|
|
|
return unittest.TestLoader().loadTestsFromName(__name__)
|
|
|
|
|
2018-10-23 02:39:14 +03:00
|
|
|
|
2010-02-13 01:34:53 +03:00
|
|
|
if __name__ == "__main__":
|
2010-04-21 16:22:09 +04:00
|
|
|
unittest.main()
|