blob: 28202cb260b64d96544a38fccbd53506b9489999 [file] [log] [blame]
Anthony Baxterc51ee692006-04-01 00:57:31 +00001#-*- coding: ISO-8859-1 -*-
2# pysqlite2/test/transactions.py: tests transactions
3#
4# Copyright (C) 2005 Gerhard Häring <gh@ghaering.de>
5#
6# This file is part of pysqlite.
7#
8# This software is provided 'as-is', without any express or implied
9# warranty. In no event will the authors be held liable for any damages
10# arising from the use of this software.
11#
12# Permission is granted to anyone to use this software for any purpose,
13# including commercial applications, and to alter it and redistribute it
14# freely, subject to the following restrictions:
15#
16# 1. The origin of this software must not be misrepresented; you must not
17# claim that you wrote the original software. If you use this software
18# in a product, an acknowledgment in the product documentation would be
19# appreciated but is not required.
20# 2. Altered source versions must be plainly marked as such, and must not be
21# misrepresented as being the original software.
22# 3. This notice may not be removed or altered from any source distribution.
23
24import os, unittest
25import sqlite3 as sqlite
26
27def get_db_path():
28 return "testdb"
29
30class TransactionTests(unittest.TestCase):
31 def setUp(self):
32 try:
33 os.remove(get_db_path())
34 except:
35 pass
36
37 self.con1 = sqlite.connect(get_db_path(), timeout=0.1)
38 self.cur1 = self.con1.cursor()
39
40 self.con2 = sqlite.connect(get_db_path(), timeout=0.1)
41 self.cur2 = self.con2.cursor()
42
43 def tearDown(self):
44 self.cur1.close()
45 self.con1.close()
46
47 self.cur2.close()
48 self.con2.close()
49
50 def CheckDMLdoesAutoCommitBefore(self):
51 self.cur1.execute("create table test(i)")
52 self.cur1.execute("insert into test(i) values (5)")
53 self.cur1.execute("create table test2(j)")
54 self.cur2.execute("select i from test")
55 res = self.cur2.fetchall()
56 self.failUnlessEqual(len(res), 1)
57
58 def CheckInsertStartsTransaction(self):
59 self.cur1.execute("create table test(i)")
60 self.cur1.execute("insert into test(i) values (5)")
61 self.cur2.execute("select i from test")
62 res = self.cur2.fetchall()
63 self.failUnlessEqual(len(res), 0)
64
65 def CheckUpdateStartsTransaction(self):
66 self.cur1.execute("create table test(i)")
67 self.cur1.execute("insert into test(i) values (5)")
68 self.con1.commit()
69 self.cur1.execute("update test set i=6")
70 self.cur2.execute("select i from test")
71 res = self.cur2.fetchone()[0]
72 self.failUnlessEqual(res, 5)
73
74 def CheckDeleteStartsTransaction(self):
75 self.cur1.execute("create table test(i)")
76 self.cur1.execute("insert into test(i) values (5)")
77 self.con1.commit()
78 self.cur1.execute("delete from test")
79 self.cur2.execute("select i from test")
80 res = self.cur2.fetchall()
81 self.failUnlessEqual(len(res), 1)
82
83 def CheckReplaceStartsTransaction(self):
84 self.cur1.execute("create table test(i)")
85 self.cur1.execute("insert into test(i) values (5)")
86 self.con1.commit()
87 self.cur1.execute("replace into test(i) values (6)")
88 self.cur2.execute("select i from test")
89 res = self.cur2.fetchall()
90 self.failUnlessEqual(len(res), 1)
91 self.failUnlessEqual(res[0][0], 5)
92
93 def CheckToggleAutoCommit(self):
94 self.cur1.execute("create table test(i)")
95 self.cur1.execute("insert into test(i) values (5)")
96 self.con1.isolation_level = None
97 self.failUnlessEqual(self.con1.isolation_level, None)
98 self.cur2.execute("select i from test")
99 res = self.cur2.fetchall()
100 self.failUnlessEqual(len(res), 1)
101
102 self.con1.isolation_level = "DEFERRED"
103 self.failUnlessEqual(self.con1.isolation_level , "DEFERRED")
104 self.cur1.execute("insert into test(i) values (5)")
105 self.cur2.execute("select i from test")
106 res = self.cur2.fetchall()
107 self.failUnlessEqual(len(res), 1)
108
109 def CheckRaiseTimeout(self):
110 self.cur1.execute("create table test(i)")
111 self.cur1.execute("insert into test(i) values (5)")
112 try:
113 self.cur2.execute("insert into test(i) values (5)")
114 self.fail("should have raised an OperationalError")
115 except sqlite.OperationalError:
116 pass
117 except:
118 self.fail("should have raised an OperationalError")
119
120class SpecialCommandTests(unittest.TestCase):
121 def setUp(self):
122 self.con = sqlite.connect(":memory:")
123 self.cur = self.con.cursor()
124
125 def CheckVacuum(self):
126 self.cur.execute("create table test(i)")
127 self.cur.execute("insert into test(i) values (5)")
128 self.cur.execute("vacuum")
129
130 def CheckDropTable(self):
131 self.cur.execute("create table test(i)")
132 self.cur.execute("insert into test(i) values (5)")
133 self.cur.execute("drop table test")
134
135 def CheckPragma(self):
136 self.cur.execute("create table test(i)")
137 self.cur.execute("insert into test(i) values (5)")
138 self.cur.execute("pragma count_changes=1")
139
140 def tearDown(self):
141 self.cur.close()
142 self.con.close()
143
144def suite():
145 default_suite = unittest.makeSuite(TransactionTests, "Check")
146 special_command_suite = unittest.makeSuite(SpecialCommandTests, "Check")
147 return unittest.TestSuite((default_suite, special_command_suite))
148
149def test():
150 runner = unittest.TextTestRunner()
151 runner.run(suite())
152
153if __name__ == "__main__":
154 test()