12
12
from textwrap import dedent
15
17
from canonical.database.sqlbase import (
16
18
cursor, ISOLATION_LEVEL_AUTOCOMMIT, ISOLATION_LEVEL_DEFAULT,
17
19
ISOLATION_LEVEL_READ_COMMITTED, ISOLATION_LEVEL_SERIALIZABLE,
20
connect, ZopelessTransactionManager)
19
21
from canonical.testing.layers import LaunchpadZopelessLayer
24
def set_isolation_level(isolation):
25
user = ZopelessTransactionManager._dbuser
26
ZopelessTransactionManager.uninstall()
27
ZopelessTransactionManager.initZopeless(dbuser=user, isolation=isolation)
22
30
class TestIsolation(unittest.TestCase):
23
31
layer = LaunchpadZopelessLayer
26
self.txn = LaunchpadZopelessLayer.txn
28
33
def getCurrentIsolation(self, con=None):
38
43
self.failUnlessEqual(self.getCurrentIsolation(), 'read committed')
40
45
def test_default2(self):
41
self.txn.set_isolation_level(ISOLATION_LEVEL_DEFAULT)
46
set_isolation_level(ISOLATION_LEVEL_DEFAULT)
42
47
self.failUnlessEqual(self.getCurrentIsolation(), 'read committed')
44
49
def test_autocommit(self):
45
self.txn.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT)
50
set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT)
46
51
# There is no actual 'autocommit' mode in PostgreSQL. psycopg
47
52
# implements this feature by using read committed isolation and
48
53
# issuing commit() statements after every query.
51
56
# So we need to confirm we are actually in autocommit mode
52
57
# by seeing if we an roll back
56
60
"SELECT COUNT(*) FROM Person WHERE homepage_content IS NULL")
57
61
self.failIfEqual(cur.fetchone()[0], 0)
58
62
cur.execute("UPDATE Person SET homepage_content=NULL")
62
66
"SELECT COUNT(*) FROM Person WHERE homepage_content IS NOT NULL")
63
67
self.failUnlessEqual(cur.fetchone()[0], 0)
65
69
def test_readCommitted(self):
66
self.txn.set_isolation_level(ISOLATION_LEVEL_READ_COMMITTED)
70
set_isolation_level(ISOLATION_LEVEL_READ_COMMITTED)
67
71
self.failUnlessEqual(self.getCurrentIsolation(), 'read committed')
69
73
def test_serializable(self):
70
self.txn.set_isolation_level(ISOLATION_LEVEL_SERIALIZABLE)
74
set_isolation_level(ISOLATION_LEVEL_SERIALIZABLE)
71
75
self.failUnlessEqual(self.getCurrentIsolation(), 'serializable')
73
77
def test_commit(self):
74
78
# Change the isolation level
75
79
self.failUnlessEqual(self.getCurrentIsolation(), 'read committed')
76
self.txn.set_isolation_level(ISOLATION_LEVEL_SERIALIZABLE)
80
set_isolation_level(ISOLATION_LEVEL_SERIALIZABLE)
77
81
self.failUnlessEqual(self.getCurrentIsolation(), 'serializable')
81
84
cur.execute("UPDATE Person SET homepage_content=NULL")
83
86
cur.execute("UPDATE Person SET homepage_content='foo'")
84
87
self.failUnlessEqual(self.getCurrentIsolation(), 'serializable')
86
89
def test_rollback(self):
87
90
# Change the isolation level
88
91
self.failUnlessEqual(self.getCurrentIsolation(), 'read committed')
89
self.txn.set_isolation_level(ISOLATION_LEVEL_SERIALIZABLE)
92
set_isolation_level(ISOLATION_LEVEL_SERIALIZABLE)
90
93
self.failUnlessEqual(self.getCurrentIsolation(), 'serializable')
94
96
cur.execute("UPDATE Person SET homepage_content=NULL")
96
98
self.failUnlessEqual(self.getCurrentIsolation(), 'serializable')
98
100
def test_script(self):