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
|
|
|
|
#
|
|
|
|
# Copyright (C) 2008-2011 James Henstridge <james@jamesh.id.au>
|
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
|
2011-04-08 14:27:45 +04:00
|
|
|
import os
|
2010-11-28 16:42:06 +03:00
|
|
|
import time
|
|
|
|
import threading
|
2011-06-01 12:07:02 +04:00
|
|
|
from testutils import unittest, decorate_all_tests
|
|
|
|
from testutils import skip_before_postgres, skip_after_postgres
|
2010-10-11 16:03:37 +04:00
|
|
|
from operator import attrgetter
|
|
|
|
|
2008-01-19 06:32:42 +03:00
|
|
|
import psycopg2
|
2010-04-21 21:40:05 +04:00
|
|
|
import psycopg2.extensions
|
2011-02-11 01:59:31 +03:00
|
|
|
from testconfig import dsn, dbname
|
2008-01-19 06:32:42 +03:00
|
|
|
|
|
|
|
class ConnectionTests(unittest.TestCase):
|
|
|
|
|
2010-11-28 19:00:32 +03:00
|
|
|
def setUp(self):
|
2011-02-11 01:59:31 +03:00
|
|
|
self.conn = psycopg2.connect(dsn)
|
2010-11-28 19:00:32 +03:00
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
if not self.conn.closed:
|
|
|
|
self.conn.close()
|
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)
|
|
|
|
|
2009-08-09 18:19:08 +04:00
|
|
|
def test_reset(self):
|
2010-11-28 19:00:32 +03:00
|
|
|
conn = self.conn
|
2009-08-09 18:19:08 +04:00
|
|
|
# switch isolation level, then reset
|
|
|
|
level = conn.isolation_level
|
|
|
|
conn.set_isolation_level(0)
|
|
|
|
self.assertEqual(conn.isolation_level, 0)
|
|
|
|
conn.reset()
|
|
|
|
# now the isolation level should be equal to saved one
|
|
|
|
self.assertEqual(conn.isolation_level, level)
|
|
|
|
|
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()
|
|
|
|
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()
|
|
|
|
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);")
|
|
|
|
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])
|
|
|
|
|
|
|
|
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()
|
|
|
|
for i in range(0, 100, 10):
|
|
|
|
sql = " ".join(["create temp table table%d (id serial);" % j for j in range(i, i+10)])
|
|
|
|
cur.execute(sql)
|
|
|
|
|
|
|
|
self.assertEqual(50, len(conn.notices))
|
|
|
|
self.assert_('table50' in conn.notices[0], conn.notices[0])
|
|
|
|
self.assert_('table51' in conn.notices[1], conn.notices[1])
|
|
|
|
self.assert_('table98' in conn.notices[-2], conn.notices[-2])
|
|
|
|
self.assert_('table99' in conn.notices[-1], conn.notices[-1])
|
|
|
|
|
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):
|
2010-11-28 19:00:32 +03:00
|
|
|
self.assert_(self.conn.protocol_version in (2,3),
|
|
|
|
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
|
2010-11-19 06:55:37 +03:00
|
|
|
if cnn.server_version >= 80100:
|
|
|
|
return self.skipTest("tpc is supported")
|
|
|
|
|
|
|
|
self.assertRaises(psycopg2.NotSupportedError,
|
|
|
|
cnn.xid, 42, "foo", "bar")
|
|
|
|
|
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():
|
2011-02-11 01:59:31 +03:00
|
|
|
cnn = psycopg2.connect(dsn)
|
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()
|
|
|
|
psycopg2.extensions.register_type(psycopg2.extensions.UNICODE, cur)
|
|
|
|
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:
|
|
|
|
self.conn = psycopg2.connect(dsn)
|
|
|
|
finally:
|
|
|
|
if oldenc is not None:
|
|
|
|
os.environ['PGCLIENTENCODING'] = oldenc
|
|
|
|
else:
|
|
|
|
del os.environ['PGCLIENTENCODING']
|
|
|
|
|
2011-01-03 13:52:38 +03:00
|
|
|
def test_weakref(self):
|
|
|
|
from weakref import ref
|
2011-12-27 01:35:33 +04:00
|
|
|
import gc
|
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)
|
|
|
|
|
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()
|
|
|
|
i = 1
|
|
|
|
for i in range(1000):
|
|
|
|
cur.execute("select %s;",(i,))
|
|
|
|
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))
|
|
|
|
|
2010-11-28 18:03:34 +03:00
|
|
|
|
2010-11-18 03:20:39 +03:00
|
|
|
class IsolationLevelsTestCase(unittest.TestCase):
|
|
|
|
|
|
|
|
def setUp(self):
|
2010-11-28 19:00:32 +03:00
|
|
|
self._conns = []
|
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()
|
2010-11-18 03:20:39 +03:00
|
|
|
cur.execute("create table isolevel (id integer);")
|
|
|
|
conn.commit()
|
|
|
|
conn.close()
|
|
|
|
|
2010-11-28 19:00:32 +03:00
|
|
|
def tearDown(self):
|
|
|
|
# close the connections used in the test
|
|
|
|
for conn in self._conns:
|
|
|
|
if not conn.closed:
|
|
|
|
conn.close()
|
|
|
|
|
2010-11-18 03:20:39 +03:00
|
|
|
def connect(self):
|
2011-02-11 01:59:31 +03:00
|
|
|
conn = psycopg2.connect(dsn)
|
2010-11-28 19:00:32 +03:00
|
|
|
self._conns.append(conn)
|
|
|
|
return conn
|
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,
|
|
|
|
psycopg2.extensions.ISOLATION_LEVEL_READ_COMMITTED)
|
|
|
|
|
|
|
|
def test_encoding(self):
|
|
|
|
conn = self.connect()
|
|
|
|
self.assert_(conn.encoding in psycopg2.extensions.encodings)
|
|
|
|
|
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 = [
|
2011-05-11 15:51:44 +04:00
|
|
|
(None, psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT),
|
2011-12-15 16:53:48 +04:00
|
|
|
('read uncommitted', psycopg2.extensions.ISOLATION_LEVEL_READ_UNCOMMITTED),
|
2011-05-11 15:51:44 +04:00
|
|
|
('read committed', psycopg2.extensions.ISOLATION_LEVEL_READ_COMMITTED),
|
|
|
|
('repeatable read', psycopg2.extensions.ISOLATION_LEVEL_REPEATABLE_READ),
|
|
|
|
('serializable', psycopg2.extensions.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
|
|
|
|
if conn.server_version < 80000:
|
2011-12-15 16:53:48 +04:00
|
|
|
if level in (
|
|
|
|
psycopg2.extensions.ISOLATION_LEVEL_READ_UNCOMMITTED,
|
|
|
|
psycopg2.extensions.ISOLATION_LEVEL_REPEATABLE_READ):
|
|
|
|
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
|
|
|
if name is None:
|
|
|
|
curs.execute('show default_transaction_isolation;')
|
|
|
|
name = curs.fetchone()[0]
|
|
|
|
|
|
|
|
self.assertEqual(name, got_name)
|
|
|
|
conn.commit()
|
2010-11-18 03:20:39 +03:00
|
|
|
|
|
|
|
self.assertRaises(ValueError, conn.set_isolation_level, -1)
|
2011-05-11 15:51:44 +04:00
|
|
|
self.assertRaises(ValueError, conn.set_isolation_level, 5)
|
2010-11-18 03:20:39 +03:00
|
|
|
|
|
|
|
def test_set_isolation_level_abort(self):
|
|
|
|
conn = self.connect()
|
|
|
|
cur = conn.cursor()
|
|
|
|
|
|
|
|
self.assertEqual(psycopg2.extensions.TRANSACTION_STATUS_IDLE,
|
|
|
|
conn.get_transaction_status())
|
|
|
|
cur.execute("insert into isolevel values (10);")
|
|
|
|
self.assertEqual(psycopg2.extensions.TRANSACTION_STATUS_INTRANS,
|
|
|
|
conn.get_transaction_status())
|
|
|
|
|
|
|
|
conn.set_isolation_level(
|
|
|
|
psycopg2.extensions.ISOLATION_LEVEL_SERIALIZABLE)
|
|
|
|
self.assertEqual(psycopg2.extensions.TRANSACTION_STATUS_IDLE,
|
|
|
|
conn.get_transaction_status())
|
|
|
|
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,
|
|
|
|
conn.get_transaction_status())
|
|
|
|
conn.set_isolation_level(
|
|
|
|
psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT)
|
|
|
|
self.assertEqual(psycopg2.extensions.TRANSACTION_STATUS_IDLE,
|
|
|
|
conn.get_transaction_status())
|
|
|
|
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,
|
|
|
|
conn.get_transaction_status())
|
|
|
|
conn.set_isolation_level(
|
|
|
|
psycopg2.extensions.ISOLATION_LEVEL_READ_COMMITTED)
|
|
|
|
self.assertEqual(psycopg2.extensions.TRANSACTION_STATUS_IDLE,
|
|
|
|
conn.get_transaction_status())
|
|
|
|
cur.execute("select count(*) from isolevel;")
|
|
|
|
self.assertEqual(1, cur.fetchone()[0])
|
|
|
|
|
2010-11-17 04:04:34 +03:00
|
|
|
def test_isolation_level_autocommit(self):
|
|
|
|
cnn1 = self.connect()
|
|
|
|
cnn2 = self.connect()
|
|
|
|
cnn2.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT)
|
|
|
|
|
|
|
|
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()
|
|
|
|
cnn2.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_READ_COMMITTED)
|
|
|
|
|
|
|
|
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()
|
|
|
|
cnn2.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_SERIALIZABLE)
|
|
|
|
|
|
|
|
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, getattr,
|
|
|
|
cnn, 'isolation_level')
|
|
|
|
self.assertRaises(psycopg2.InterfaceError,
|
|
|
|
cnn.set_isolation_level, 0)
|
|
|
|
self.assertRaises(psycopg2.InterfaceError,
|
|
|
|
cnn.set_isolation_level, 1)
|
|
|
|
|
2010-10-11 16:03:37 +04:00
|
|
|
|
|
|
|
class ConnectionTwoPhaseTests(unittest.TestCase):
|
|
|
|
def setUp(self):
|
2010-11-28 19:00:32 +03:00
|
|
|
self._conns = []
|
|
|
|
|
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()
|
|
|
|
|
2010-11-28 19:00:32 +03:00
|
|
|
# close the connections used in the test
|
|
|
|
for conn in self._conns:
|
|
|
|
if not conn.closed:
|
|
|
|
conn.close()
|
|
|
|
|
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
|
|
|
|
|
2010-10-11 16:03:37 +04:00
|
|
|
gids = [ r[0] for r in cur ]
|
|
|
|
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()
|
2010-10-10 22:15:33 +04:00
|
|
|
cur.execute("CREATE TABLE test_tpc (data text);")
|
|
|
|
cnn.commit()
|
|
|
|
cnn.close()
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2010-10-11 16:03:37 +04:00
|
|
|
def connect(self):
|
2011-02-11 01:59:31 +03:00
|
|
|
conn = psycopg2.connect(dsn)
|
2010-11-28 19:00:32 +03:00
|
|
|
self._conns.append(conn)
|
|
|
|
return conn
|
2010-10-11 16:03:37 +04:00
|
|
|
|
2010-10-10 22:15:33 +04:00
|
|
|
def test_tpc_commit(self):
|
|
|
|
cnn = self.connect()
|
|
|
|
xid = cnn.xid(1, "gtrid", "bqual")
|
|
|
|
self.assertEqual(cnn.status, psycopg2.extensions.STATUS_READY)
|
|
|
|
|
|
|
|
cnn.tpc_begin(xid)
|
|
|
|
self.assertEqual(cnn.status, psycopg2.extensions.STATUS_BEGIN)
|
|
|
|
|
|
|
|
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()
|
|
|
|
self.assertEqual(cnn.status, psycopg2.extensions.STATUS_PREPARED)
|
|
|
|
self.assertEqual(1, self.count_xacts())
|
|
|
|
self.assertEqual(0, self.count_test_records())
|
|
|
|
|
|
|
|
cnn.tpc_commit()
|
|
|
|
self.assertEqual(cnn.status, psycopg2.extensions.STATUS_READY)
|
|
|
|
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")
|
|
|
|
self.assertEqual(cnn.status, psycopg2.extensions.STATUS_READY)
|
|
|
|
|
|
|
|
cnn.tpc_begin(xid)
|
|
|
|
self.assertEqual(cnn.status, psycopg2.extensions.STATUS_BEGIN)
|
|
|
|
|
|
|
|
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()
|
|
|
|
self.assertEqual(cnn.status, psycopg2.extensions.STATUS_READY)
|
|
|
|
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")
|
|
|
|
self.assertEqual(cnn.status, psycopg2.extensions.STATUS_READY)
|
|
|
|
|
|
|
|
cnn.tpc_begin(xid)
|
|
|
|
self.assertEqual(cnn.status, psycopg2.extensions.STATUS_BEGIN)
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
self.assertEqual(cnn.status, psycopg2.extensions.STATUS_READY)
|
|
|
|
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")
|
|
|
|
self.assertEqual(cnn.status, psycopg2.extensions.STATUS_READY)
|
|
|
|
|
|
|
|
cnn.tpc_begin(xid)
|
|
|
|
self.assertEqual(cnn.status, psycopg2.extensions.STATUS_BEGIN)
|
|
|
|
|
|
|
|
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()
|
|
|
|
self.assertEqual(cnn.status, psycopg2.extensions.STATUS_PREPARED)
|
|
|
|
self.assertEqual(1, self.count_xacts())
|
|
|
|
self.assertEqual(0, self.count_test_records())
|
|
|
|
|
|
|
|
cnn.tpc_rollback()
|
|
|
|
self.assertEqual(cnn.status, psycopg2.extensions.STATUS_READY)
|
|
|
|
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")
|
|
|
|
self.assertEqual(cnn.status, psycopg2.extensions.STATUS_READY)
|
|
|
|
|
|
|
|
cnn.tpc_begin(xid)
|
|
|
|
self.assertEqual(cnn.status, psycopg2.extensions.STATUS_BEGIN)
|
|
|
|
|
|
|
|
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()
|
|
|
|
self.assertEqual(cnn.status, psycopg2.extensions.STATUS_READY)
|
|
|
|
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")
|
|
|
|
self.assertEqual(cnn.status, psycopg2.extensions.STATUS_READY)
|
|
|
|
|
|
|
|
cnn.tpc_begin(xid)
|
|
|
|
self.assertEqual(cnn.status, psycopg2.extensions.STATUS_BEGIN)
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
self.assertEqual(cnn.status, psycopg2.extensions.STATUS_READY)
|
|
|
|
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()
|
|
|
|
self.assertEqual(psycopg2.extensions.STATUS_READY, cnn.status)
|
|
|
|
xns = cnn.tpc_recover()
|
|
|
|
self.assertEqual(psycopg2.extensions.STATUS_READY, cnn.status)
|
|
|
|
|
|
|
|
cur = cnn.cursor()
|
|
|
|
cur.execute("select 1")
|
|
|
|
self.assertEqual(psycopg2.extensions.STATUS_BEGIN, cnn.status)
|
|
|
|
xns = cnn.tpc_recover()
|
|
|
|
self.assertEqual(psycopg2.extensions.STATUS_BEGIN, cnn.status)
|
|
|
|
|
|
|
|
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()
|
2011-02-11 01:59:31 +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))
|
|
|
|
for (xid, (gid, prepared, owner, database)) in zip (xids, okvals):
|
|
|
|
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])
|
|
|
|
|
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()
|
|
|
|
xids = [ xid for xid in cnn.tpc_recover()
|
2011-02-11 01:59:31 +03:00
|
|
|
if xid.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)
|
|
|
|
|
|
|
|
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()
|
|
|
|
xids = [ xid for xid in cnn.tpc_recover()
|
2011-02-11 01:59:31 +03:00
|
|
|
if xid.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):
|
|
|
|
from psycopg2.extensions import Xid
|
|
|
|
|
|
|
|
x1 = Xid(74, 'foo', 'bar')
|
|
|
|
self.assertEqual(74, x1.format_id)
|
|
|
|
self.assertEqual('foo', x1.gtrid)
|
|
|
|
self.assertEqual('bar', x1.bqual)
|
|
|
|
|
|
|
|
def test_xid_from_string(self):
|
|
|
|
from psycopg2.extensions import Xid
|
|
|
|
|
|
|
|
x2 = Xid.from_string('42_Z3RyaWQ=_YnF1YWw=')
|
|
|
|
self.assertEqual(42, x2.format_id)
|
|
|
|
self.assertEqual('gtrid', x2.gtrid)
|
|
|
|
self.assertEqual('bqual', x2.bqual)
|
|
|
|
|
|
|
|
x3 = Xid.from_string('99_xxx_yyy')
|
|
|
|
self.assertEqual(None, x3.format_id)
|
|
|
|
self.assertEqual('99_xxx_yyy', x3.gtrid)
|
|
|
|
self.assertEqual(None, x3.bqual)
|
|
|
|
|
|
|
|
def test_xid_to_string(self):
|
|
|
|
from psycopg2.extensions import Xid
|
|
|
|
|
|
|
|
x1 = Xid.from_string('42_Z3RyaWQ=_YnF1YWw=')
|
|
|
|
self.assertEqual(str(x1), '42_Z3RyaWQ=_YnF1YWw=')
|
|
|
|
|
|
|
|
x2 = Xid.from_string('99_xxx_yyy')
|
|
|
|
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()
|
|
|
|
xid = [ xid for xid in cnn.tpc_recover()
|
2011-02-11 01:59:31 +03:00
|
|
|
if xid.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()
|
|
|
|
|
|
|
|
xid = [ xid for xid in cnn.tpc_recover()
|
2011-02-11 01:59:31 +03:00
|
|
|
if xid.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)
|
|
|
|
|
2011-01-10 02:44:58 +03:00
|
|
|
from testutils import skip_if_tpc_disabled
|
2010-11-19 06:55:37 +03:00
|
|
|
decorate_all_tests(ConnectionTwoPhaseTests, skip_if_tpc_disabled)
|
|
|
|
|
2010-10-11 16:03:37 +04:00
|
|
|
|
2011-06-01 12:07:02 +04:00
|
|
|
class TransactionControlTests(unittest.TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
self.conn = psycopg2.connect(dsn)
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
if not self.conn.closed:
|
|
|
|
self.conn.close()
|
|
|
|
|
2011-11-17 03:51:05 +04:00
|
|
|
def test_closed(self):
|
|
|
|
self.conn.close()
|
|
|
|
self.assertRaises(psycopg2.InterfaceError,
|
|
|
|
self.conn.set_session,
|
|
|
|
psycopg2.extensions.ISOLATION_LEVEL_SERIALIZABLE)
|
|
|
|
|
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,
|
2011-06-01 12:07:02 +04:00
|
|
|
psycopg2.extensions.ISOLATION_LEVEL_SERIALIZABLE)
|
|
|
|
|
|
|
|
def test_set_isolation_level(self):
|
|
|
|
cur = self.conn.cursor()
|
2011-06-08 17:22:11 +04:00
|
|
|
self.conn.set_session(
|
2011-06-01 12:07:02 +04:00
|
|
|
psycopg2.extensions.ISOLATION_LEVEL_SERIALIZABLE)
|
|
|
|
cur.execute("SHOW default_transaction_isolation;")
|
|
|
|
self.assertEqual(cur.fetchone()[0], 'serializable')
|
|
|
|
self.conn.rollback()
|
|
|
|
|
2011-06-08 17:22:11 +04:00
|
|
|
self.conn.set_session(
|
2011-06-01 12:07:02 +04:00
|
|
|
psycopg2.extensions.ISOLATION_LEVEL_REPEATABLE_READ)
|
|
|
|
cur.execute("SHOW default_transaction_isolation;")
|
|
|
|
if self.conn.server_version > 80000:
|
|
|
|
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(
|
2011-06-01 12:07:02 +04:00
|
|
|
isolation_level=psycopg2.extensions.ISOLATION_LEVEL_READ_COMMITTED)
|
|
|
|
cur.execute("SHOW default_transaction_isolation;")
|
|
|
|
self.assertEqual(cur.fetchone()[0], 'read committed')
|
|
|
|
self.conn.rollback()
|
|
|
|
|
2011-06-08 17:22:11 +04:00
|
|
|
self.conn.set_session(
|
2011-06-01 12:07:02 +04:00
|
|
|
isolation_level=psycopg2.extensions.ISOLATION_LEVEL_READ_UNCOMMITTED)
|
|
|
|
cur.execute("SHOW default_transaction_isolation;")
|
2011-12-15 16:53:48 +04:00
|
|
|
if self.conn.server_version > 80000:
|
|
|
|
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")
|
2011-06-01 12:07:02 +04:00
|
|
|
cur.execute("SHOW default_transaction_isolation;")
|
|
|
|
self.assertEqual(cur.fetchone()[0], 'serializable')
|
|
|
|
self.conn.rollback()
|
|
|
|
|
2011-06-08 17:22:11 +04:00
|
|
|
self.conn.set_session("repeatable read")
|
2011-06-01 12:07:02 +04:00
|
|
|
cur.execute("SHOW default_transaction_isolation;")
|
|
|
|
if self.conn.server_version > 80000:
|
|
|
|
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")
|
2011-06-01 12:07:02 +04:00
|
|
|
cur.execute("SHOW default_transaction_isolation;")
|
|
|
|
self.assertEqual(cur.fetchone()[0], 'read committed')
|
|
|
|
self.conn.rollback()
|
|
|
|
|
2011-06-08 17:22:11 +04:00
|
|
|
self.conn.set_session("read uncommitted")
|
2011-06-01 12:07:02 +04:00
|
|
|
cur.execute("SHOW default_transaction_isolation;")
|
|
|
|
if self.conn.server_version > 80000:
|
|
|
|
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):
|
|
|
|
cur = self.conn.cursor()
|
2011-06-08 17:22:11 +04:00
|
|
|
self.conn.set_session(readonly=True)
|
2011-06-01 12:07:02 +04:00
|
|
|
cur.execute("SHOW default_transaction_read_only;")
|
|
|
|
self.assertEqual(cur.fetchone()[0], 'on')
|
|
|
|
self.conn.rollback()
|
|
|
|
cur.execute("SHOW default_transaction_read_only;")
|
|
|
|
self.assertEqual(cur.fetchone()[0], 'on')
|
|
|
|
self.conn.rollback()
|
|
|
|
|
|
|
|
cur = self.conn.cursor()
|
2011-06-08 17:22:11 +04:00
|
|
|
self.conn.set_session(readonly=None)
|
2011-06-01 12:07:02 +04:00
|
|
|
cur.execute("SHOW default_transaction_read_only;")
|
|
|
|
self.assertEqual(cur.fetchone()[0], 'on')
|
|
|
|
self.conn.rollback()
|
|
|
|
|
2011-06-08 17:22:11 +04:00
|
|
|
self.conn.set_session(readonly=False)
|
2011-06-01 12:07:02 +04:00
|
|
|
cur.execute("SHOW default_transaction_read_only;")
|
|
|
|
self.assertEqual(cur.fetchone()[0], 'off')
|
|
|
|
self.conn.rollback()
|
|
|
|
|
|
|
|
def test_set_default(self):
|
|
|
|
cur = self.conn.cursor()
|
|
|
|
cur.execute("SHOW default_transaction_isolation;")
|
|
|
|
default_isolevel = cur.fetchone()[0]
|
|
|
|
cur.execute("SHOW default_transaction_read_only;")
|
|
|
|
default_readonly = cur.fetchone()[0]
|
|
|
|
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
|
|
|
|
|
|
|
cur.execute("SHOW default_transaction_isolation;")
|
|
|
|
self.assertEqual(cur.fetchone()[0], default_isolevel)
|
|
|
|
cur.execute("SHOW default_transaction_read_only;")
|
|
|
|
self.assertEqual(cur.fetchone()[0], default_readonly)
|
|
|
|
|
|
|
|
@skip_before_postgres(9, 1)
|
|
|
|
def test_set_deferrable(self):
|
|
|
|
cur = self.conn.cursor()
|
2011-06-08 17:22:11 +04:00
|
|
|
self.conn.set_session(readonly=True, deferrable=True)
|
2011-06-01 12:07:02 +04:00
|
|
|
cur.execute("SHOW default_transaction_read_only;")
|
|
|
|
self.assertEqual(cur.fetchone()[0], 'on')
|
|
|
|
cur.execute("SHOW default_transaction_deferrable;")
|
|
|
|
self.assertEqual(cur.fetchone()[0], 'on')
|
|
|
|
self.conn.rollback()
|
|
|
|
cur.execute("SHOW default_transaction_deferrable;")
|
|
|
|
self.assertEqual(cur.fetchone()[0], 'on')
|
|
|
|
self.conn.rollback()
|
|
|
|
|
2011-06-08 17:22:11 +04:00
|
|
|
self.conn.set_session(deferrable=False)
|
2011-06-01 12:07:02 +04:00
|
|
|
cur.execute("SHOW default_transaction_read_only;")
|
|
|
|
self.assertEqual(cur.fetchone()[0], 'on')
|
|
|
|
cur.execute("SHOW default_transaction_deferrable;")
|
|
|
|
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)
|
2011-06-01 12:07:02 +04:00
|
|
|
|
|
|
|
|
2011-06-02 04:16:22 +04:00
|
|
|
class AutocommitTests(unittest.TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
self.conn = psycopg2.connect(dsn)
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
if not self.conn.closed:
|
|
|
|
self.conn.close()
|
|
|
|
|
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)
|
|
|
|
self.assertEqual(self.conn.status, psycopg2.extensions.STATUS_READY)
|
|
|
|
self.assertEqual(self.conn.get_transaction_status(),
|
|
|
|
psycopg2.extensions.TRANSACTION_STATUS_IDLE)
|
|
|
|
|
|
|
|
cur = self.conn.cursor()
|
|
|
|
cur.execute('select 1;')
|
|
|
|
self.assertEqual(self.conn.status, psycopg2.extensions.STATUS_BEGIN)
|
|
|
|
self.assertEqual(self.conn.get_transaction_status(),
|
|
|
|
psycopg2.extensions.TRANSACTION_STATUS_INTRANS)
|
|
|
|
|
|
|
|
self.conn.rollback()
|
|
|
|
self.assertEqual(self.conn.status, psycopg2.extensions.STATUS_READY)
|
|
|
|
self.assertEqual(self.conn.get_transaction_status(),
|
|
|
|
psycopg2.extensions.TRANSACTION_STATUS_IDLE)
|
|
|
|
|
|
|
|
def test_set_autocommit(self):
|
|
|
|
self.conn.autocommit = True
|
|
|
|
self.assert_(self.conn.autocommit)
|
|
|
|
self.assertEqual(self.conn.status, psycopg2.extensions.STATUS_READY)
|
|
|
|
self.assertEqual(self.conn.get_transaction_status(),
|
|
|
|
psycopg2.extensions.TRANSACTION_STATUS_IDLE)
|
|
|
|
|
|
|
|
cur = self.conn.cursor()
|
|
|
|
cur.execute('select 1;')
|
|
|
|
self.assertEqual(self.conn.status, psycopg2.extensions.STATUS_READY)
|
|
|
|
self.assertEqual(self.conn.get_transaction_status(),
|
|
|
|
psycopg2.extensions.TRANSACTION_STATUS_IDLE)
|
|
|
|
|
|
|
|
self.conn.autocommit = False
|
|
|
|
self.assert_(not self.conn.autocommit)
|
|
|
|
self.assertEqual(self.conn.status, psycopg2.extensions.STATUS_READY)
|
|
|
|
self.assertEqual(self.conn.get_transaction_status(),
|
|
|
|
psycopg2.extensions.TRANSACTION_STATUS_IDLE)
|
|
|
|
|
|
|
|
cur.execute('select 1;')
|
|
|
|
self.assertEqual(self.conn.status, psycopg2.extensions.STATUS_BEGIN)
|
|
|
|
self.assertEqual(self.conn.get_transaction_status(),
|
|
|
|
psycopg2.extensions.TRANSACTION_STATUS_INTRANS)
|
|
|
|
|
|
|
|
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)
|
|
|
|
self.assertEqual(self.conn.status, psycopg2.extensions.STATUS_READY)
|
|
|
|
self.assertEqual(self.conn.get_transaction_status(),
|
|
|
|
psycopg2.extensions.TRANSACTION_STATUS_IDLE)
|
|
|
|
|
|
|
|
cur = self.conn.cursor()
|
|
|
|
cur.execute('select 1;')
|
|
|
|
self.assertEqual(self.conn.status, psycopg2.extensions.STATUS_READY)
|
|
|
|
self.assertEqual(self.conn.get_transaction_status(),
|
|
|
|
psycopg2.extensions.TRANSACTION_STATUS_IDLE)
|
|
|
|
|
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)
|
|
|
|
self.assertEqual(self.conn.status, psycopg2.extensions.STATUS_READY)
|
|
|
|
self.assertEqual(self.conn.get_transaction_status(),
|
|
|
|
psycopg2.extensions.TRANSACTION_STATUS_IDLE)
|
|
|
|
|
|
|
|
cur.execute('select 1;')
|
|
|
|
self.assertEqual(self.conn.status, psycopg2.extensions.STATUS_BEGIN)
|
|
|
|
self.assertEqual(self.conn.get_transaction_status(),
|
|
|
|
psycopg2.extensions.TRANSACTION_STATUS_INTRANS)
|
|
|
|
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;')
|
|
|
|
self.assertEqual(self.conn.status, psycopg2.extensions.STATUS_READY)
|
|
|
|
self.assertEqual(self.conn.get_transaction_status(),
|
|
|
|
psycopg2.extensions.TRANSACTION_STATUS_IDLE)
|
|
|
|
cur.execute("SHOW default_transaction_isolation;")
|
|
|
|
self.assertEqual(cur.fetchone()[0], 'serializable')
|
|
|
|
cur.execute("SHOW default_transaction_read_only;")
|
|
|
|
self.assertEqual(cur.fetchone()[0], 'on')
|
|
|
|
|
|
|
|
|
2008-01-19 06:32:42 +03:00
|
|
|
def test_suite():
|
|
|
|
return unittest.TestLoader().loadTestsFromName(__name__)
|
|
|
|
|
2010-02-13 01:34:53 +03:00
|
|
|
if __name__ == "__main__":
|
2010-04-21 16:22:09 +04:00
|
|
|
unittest.main()
|