-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathtest_transactions.py
More file actions
122 lines (103 loc) · 3.71 KB
/
Copy pathtest_transactions.py
File metadata and controls
122 lines (103 loc) · 3.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import pytest
from sqlobject import SQLObject, SQLObjectNotFound, StringCol
from sqlobject.tests.dbtest import raises, setupClass, supports
from sqlobject import events
from sqlobject.main import sqlmeta
########################################
# Transaction test
########################################
try:
support_transactions = supports('transactions')
except NameError:
# The module was imported during documentation building
pass
else:
if not support_transactions:
pytestmark = pytest.mark.skip("These tests require transactions")
class SOTestSOTrans(SQLObject):
class sqlmeta:
defaultOrder = 'name'
name = StringCol(length=10, alternateID=True, dbName='name_col')
def make_watcher():
log = []
def watch(*args):
log.append(args)
watch.log = log
return watch
def make_listen(signal):
watcher = make_watcher()
events.listen(watcher, sqlmeta, signal)
return watcher
def test_transaction():
commit_watcher = make_listen(events.CommitSignal)
rollback_watcher = make_listen(events.RollbackSignal)
setupClass(SOTestSOTrans)
SOTestSOTrans(name='bob')
SOTestSOTrans(name='tim')
trans = SOTestSOTrans._connection.transaction()
try:
SOTestSOTrans._connection.autoCommit = 'exception'
SOTestSOTrans(name='joe', connection=trans)
trans.rollback()
trans.begin()
assert ([n.name for n in SOTestSOTrans.select(connection=trans)]
== ['bob', 'tim'])
b = SOTestSOTrans.byName('bob', connection=trans)
b.name = 'robert'
trans.commit()
assert b.name == 'robert'
b.name = 'bob'
trans.rollback()
trans.begin()
assert b.name == 'robert'
assert len(commit_watcher.log) == 1
assert commit_watcher.log[0][0][0][0] == 'SOTestSOTrans'
assert commit_watcher.log[0][0][0][1] == [1, 2]
assert len(rollback_watcher.log) == 2
assert rollback_watcher.log[0][0][0][0] == 'SOTestSOTrans'
assert rollback_watcher.log[0][0][0][1] == [3]
assert rollback_watcher.log[1][0][0][0] == 'SOTestSOTrans'
assert rollback_watcher.log[1][0][0][1] == [1, 2]
finally:
SOTestSOTrans._connection.autoCommit = True
def test_transaction_commit_sync():
setupClass(SOTestSOTrans)
connection = SOTestSOTrans._connection
trans = connection.transaction()
try:
SOTestSOTrans(name='bob')
bOut = SOTestSOTrans.byName('bob')
bIn = SOTestSOTrans.byName('bob', connection=trans)
bIn.name = 'robert'
assert bOut.name == 'bob'
trans.commit()
assert bOut.name == 'robert'
finally:
trans.rollback()
connection.autoCommit = True
connection.close()
def test_transaction_delete(close=False):
setupClass(SOTestSOTrans)
connection = SOTestSOTrans._connection
if (connection.dbName == 'sqlite') and connection._memory:
pytest.skip("The test doesn't work with sqlite memory connection")
trans = connection.transaction()
try:
SOTestSOTrans(name='bob')
bIn = SOTestSOTrans.byName('bob', connection=trans)
bIn.destroySelf()
bOut = SOTestSOTrans.select(SOTestSOTrans.q.name == 'bob')
assert bOut.count() == 1
bOutInst = bOut[0]
bOutID = bOutInst.id # noqa: bOutID is used in the string code below
trans.commit(close=close)
assert bOut.count() == 0
raises(SQLObjectNotFound, SOTestSOTrans.get, bOutID)
with raises(SQLObjectNotFound):
bOutInst.name
finally:
trans.rollback()
connection.autoCommit = True
connection.close()
def test_transaction_delete_with_close():
test_transaction_delete(close=True)