| 1 | n/a | #-*- coding: iso-8859-1 -*- |
|---|
| 2 | n/a | # pysqlite2/test/transactions.py: tests transactions |
|---|
| 3 | n/a | # |
|---|
| 4 | n/a | # Copyright (C) 2005-2007 Gerhard Häring <gh@ghaering.de> |
|---|
| 5 | n/a | # |
|---|
| 6 | n/a | # This file is part of pysqlite. |
|---|
| 7 | n/a | # |
|---|
| 8 | n/a | # This software is provided 'as-is', without any express or implied |
|---|
| 9 | n/a | # warranty. In no event will the authors be held liable for any damages |
|---|
| 10 | n/a | # arising from the use of this software. |
|---|
| 11 | n/a | # |
|---|
| 12 | n/a | # Permission is granted to anyone to use this software for any purpose, |
|---|
| 13 | n/a | # including commercial applications, and to alter it and redistribute it |
|---|
| 14 | n/a | # freely, subject to the following restrictions: |
|---|
| 15 | n/a | # |
|---|
| 16 | n/a | # 1. The origin of this software must not be misrepresented; you must not |
|---|
| 17 | n/a | # claim that you wrote the original software. If you use this software |
|---|
| 18 | n/a | # in a product, an acknowledgment in the product documentation would be |
|---|
| 19 | n/a | # appreciated but is not required. |
|---|
| 20 | n/a | # 2. Altered source versions must be plainly marked as such, and must not be |
|---|
| 21 | n/a | # misrepresented as being the original software. |
|---|
| 22 | n/a | # 3. This notice may not be removed or altered from any source distribution. |
|---|
| 23 | n/a | |
|---|
| 24 | n/a | import os, unittest |
|---|
| 25 | n/a | import sqlite3 as sqlite |
|---|
| 26 | n/a | |
|---|
| 27 | n/a | def get_db_path(): |
|---|
| 28 | n/a | return "sqlite_testdb" |
|---|
| 29 | n/a | |
|---|
| 30 | n/a | class TransactionTests(unittest.TestCase): |
|---|
| 31 | n/a | def setUp(self): |
|---|
| 32 | n/a | try: |
|---|
| 33 | n/a | os.remove(get_db_path()) |
|---|
| 34 | n/a | except OSError: |
|---|
| 35 | n/a | pass |
|---|
| 36 | n/a | |
|---|
| 37 | n/a | self.con1 = sqlite.connect(get_db_path(), timeout=0.1) |
|---|
| 38 | n/a | self.cur1 = self.con1.cursor() |
|---|
| 39 | n/a | |
|---|
| 40 | n/a | self.con2 = sqlite.connect(get_db_path(), timeout=0.1) |
|---|
| 41 | n/a | self.cur2 = self.con2.cursor() |
|---|
| 42 | n/a | |
|---|
| 43 | n/a | def tearDown(self): |
|---|
| 44 | n/a | self.cur1.close() |
|---|
| 45 | n/a | self.con1.close() |
|---|
| 46 | n/a | |
|---|
| 47 | n/a | self.cur2.close() |
|---|
| 48 | n/a | self.con2.close() |
|---|
| 49 | n/a | |
|---|
| 50 | n/a | try: |
|---|
| 51 | n/a | os.unlink(get_db_path()) |
|---|
| 52 | n/a | except OSError: |
|---|
| 53 | n/a | pass |
|---|
| 54 | n/a | |
|---|
| 55 | n/a | def CheckDMLDoesNotAutoCommitBefore(self): |
|---|
| 56 | n/a | self.cur1.execute("create table test(i)") |
|---|
| 57 | n/a | self.cur1.execute("insert into test(i) values (5)") |
|---|
| 58 | n/a | self.cur1.execute("create table test2(j)") |
|---|
| 59 | n/a | self.cur2.execute("select i from test") |
|---|
| 60 | n/a | res = self.cur2.fetchall() |
|---|
| 61 | n/a | self.assertEqual(len(res), 0) |
|---|
| 62 | n/a | |
|---|
| 63 | n/a | def CheckInsertStartsTransaction(self): |
|---|
| 64 | n/a | self.cur1.execute("create table test(i)") |
|---|
| 65 | n/a | self.cur1.execute("insert into test(i) values (5)") |
|---|
| 66 | n/a | self.cur2.execute("select i from test") |
|---|
| 67 | n/a | res = self.cur2.fetchall() |
|---|
| 68 | n/a | self.assertEqual(len(res), 0) |
|---|
| 69 | n/a | |
|---|
| 70 | n/a | def CheckUpdateStartsTransaction(self): |
|---|
| 71 | n/a | self.cur1.execute("create table test(i)") |
|---|
| 72 | n/a | self.cur1.execute("insert into test(i) values (5)") |
|---|
| 73 | n/a | self.con1.commit() |
|---|
| 74 | n/a | self.cur1.execute("update test set i=6") |
|---|
| 75 | n/a | self.cur2.execute("select i from test") |
|---|
| 76 | n/a | res = self.cur2.fetchone()[0] |
|---|
| 77 | n/a | self.assertEqual(res, 5) |
|---|
| 78 | n/a | |
|---|
| 79 | n/a | def CheckDeleteStartsTransaction(self): |
|---|
| 80 | n/a | self.cur1.execute("create table test(i)") |
|---|
| 81 | n/a | self.cur1.execute("insert into test(i) values (5)") |
|---|
| 82 | n/a | self.con1.commit() |
|---|
| 83 | n/a | self.cur1.execute("delete from test") |
|---|
| 84 | n/a | self.cur2.execute("select i from test") |
|---|
| 85 | n/a | res = self.cur2.fetchall() |
|---|
| 86 | n/a | self.assertEqual(len(res), 1) |
|---|
| 87 | n/a | |
|---|
| 88 | n/a | def CheckReplaceStartsTransaction(self): |
|---|
| 89 | n/a | self.cur1.execute("create table test(i)") |
|---|
| 90 | n/a | self.cur1.execute("insert into test(i) values (5)") |
|---|
| 91 | n/a | self.con1.commit() |
|---|
| 92 | n/a | self.cur1.execute("replace into test(i) values (6)") |
|---|
| 93 | n/a | self.cur2.execute("select i from test") |
|---|
| 94 | n/a | res = self.cur2.fetchall() |
|---|
| 95 | n/a | self.assertEqual(len(res), 1) |
|---|
| 96 | n/a | self.assertEqual(res[0][0], 5) |
|---|
| 97 | n/a | |
|---|
| 98 | n/a | def CheckToggleAutoCommit(self): |
|---|
| 99 | n/a | self.cur1.execute("create table test(i)") |
|---|
| 100 | n/a | self.cur1.execute("insert into test(i) values (5)") |
|---|
| 101 | n/a | self.con1.isolation_level = None |
|---|
| 102 | n/a | self.assertEqual(self.con1.isolation_level, None) |
|---|
| 103 | n/a | self.cur2.execute("select i from test") |
|---|
| 104 | n/a | res = self.cur2.fetchall() |
|---|
| 105 | n/a | self.assertEqual(len(res), 1) |
|---|
| 106 | n/a | |
|---|
| 107 | n/a | self.con1.isolation_level = "DEFERRED" |
|---|
| 108 | n/a | self.assertEqual(self.con1.isolation_level , "DEFERRED") |
|---|
| 109 | n/a | self.cur1.execute("insert into test(i) values (5)") |
|---|
| 110 | n/a | self.cur2.execute("select i from test") |
|---|
| 111 | n/a | res = self.cur2.fetchall() |
|---|
| 112 | n/a | self.assertEqual(len(res), 1) |
|---|
| 113 | n/a | |
|---|
| 114 | n/a | @unittest.skipIf(sqlite.sqlite_version_info < (3, 2, 2), |
|---|
| 115 | n/a | 'test hangs on sqlite versions older than 3.2.2') |
|---|
| 116 | n/a | def CheckRaiseTimeout(self): |
|---|
| 117 | n/a | self.cur1.execute("create table test(i)") |
|---|
| 118 | n/a | self.cur1.execute("insert into test(i) values (5)") |
|---|
| 119 | n/a | with self.assertRaises(sqlite.OperationalError): |
|---|
| 120 | n/a | self.cur2.execute("insert into test(i) values (5)") |
|---|
| 121 | n/a | |
|---|
| 122 | n/a | @unittest.skipIf(sqlite.sqlite_version_info < (3, 2, 2), |
|---|
| 123 | n/a | 'test hangs on sqlite versions older than 3.2.2') |
|---|
| 124 | n/a | def CheckLocking(self): |
|---|
| 125 | n/a | """ |
|---|
| 126 | n/a | This tests the improved concurrency with pysqlite 2.3.4. You needed |
|---|
| 127 | n/a | to roll back con2 before you could commit con1. |
|---|
| 128 | n/a | """ |
|---|
| 129 | n/a | self.cur1.execute("create table test(i)") |
|---|
| 130 | n/a | self.cur1.execute("insert into test(i) values (5)") |
|---|
| 131 | n/a | with self.assertRaises(sqlite.OperationalError): |
|---|
| 132 | n/a | self.cur2.execute("insert into test(i) values (5)") |
|---|
| 133 | n/a | # NO self.con2.rollback() HERE!!! |
|---|
| 134 | n/a | self.con1.commit() |
|---|
| 135 | n/a | |
|---|
| 136 | n/a | def CheckRollbackCursorConsistency(self): |
|---|
| 137 | n/a | """ |
|---|
| 138 | n/a | Checks if cursors on the connection are set into a "reset" state |
|---|
| 139 | n/a | when a rollback is done on the connection. |
|---|
| 140 | n/a | """ |
|---|
| 141 | n/a | con = sqlite.connect(":memory:") |
|---|
| 142 | n/a | cur = con.cursor() |
|---|
| 143 | n/a | cur.execute("create table test(x)") |
|---|
| 144 | n/a | cur.execute("insert into test(x) values (5)") |
|---|
| 145 | n/a | cur.execute("select 1 union select 2 union select 3") |
|---|
| 146 | n/a | |
|---|
| 147 | n/a | con.rollback() |
|---|
| 148 | n/a | with self.assertRaises(sqlite.InterfaceError): |
|---|
| 149 | n/a | cur.fetchall() |
|---|
| 150 | n/a | |
|---|
| 151 | n/a | class SpecialCommandTests(unittest.TestCase): |
|---|
| 152 | n/a | def setUp(self): |
|---|
| 153 | n/a | self.con = sqlite.connect(":memory:") |
|---|
| 154 | n/a | self.cur = self.con.cursor() |
|---|
| 155 | n/a | |
|---|
| 156 | n/a | def CheckDropTable(self): |
|---|
| 157 | n/a | self.cur.execute("create table test(i)") |
|---|
| 158 | n/a | self.cur.execute("insert into test(i) values (5)") |
|---|
| 159 | n/a | self.cur.execute("drop table test") |
|---|
| 160 | n/a | |
|---|
| 161 | n/a | def CheckPragma(self): |
|---|
| 162 | n/a | self.cur.execute("create table test(i)") |
|---|
| 163 | n/a | self.cur.execute("insert into test(i) values (5)") |
|---|
| 164 | n/a | self.cur.execute("pragma count_changes=1") |
|---|
| 165 | n/a | |
|---|
| 166 | n/a | def tearDown(self): |
|---|
| 167 | n/a | self.cur.close() |
|---|
| 168 | n/a | self.con.close() |
|---|
| 169 | n/a | |
|---|
| 170 | n/a | class TransactionalDDL(unittest.TestCase): |
|---|
| 171 | n/a | def setUp(self): |
|---|
| 172 | n/a | self.con = sqlite.connect(":memory:") |
|---|
| 173 | n/a | |
|---|
| 174 | n/a | def CheckDdlDoesNotAutostartTransaction(self): |
|---|
| 175 | n/a | # For backwards compatibility reasons, DDL statements should not |
|---|
| 176 | n/a | # implicitly start a transaction. |
|---|
| 177 | n/a | self.con.execute("create table test(i)") |
|---|
| 178 | n/a | self.con.rollback() |
|---|
| 179 | n/a | result = self.con.execute("select * from test").fetchall() |
|---|
| 180 | n/a | self.assertEqual(result, []) |
|---|
| 181 | n/a | |
|---|
| 182 | n/a | def CheckTransactionalDDL(self): |
|---|
| 183 | n/a | # You can achieve transactional DDL by issuing a BEGIN |
|---|
| 184 | n/a | # statement manually. |
|---|
| 185 | n/a | self.con.execute("begin") |
|---|
| 186 | n/a | self.con.execute("create table test(i)") |
|---|
| 187 | n/a | self.con.rollback() |
|---|
| 188 | n/a | with self.assertRaises(sqlite.OperationalError): |
|---|
| 189 | n/a | self.con.execute("select * from test") |
|---|
| 190 | n/a | |
|---|
| 191 | n/a | def tearDown(self): |
|---|
| 192 | n/a | self.con.close() |
|---|
| 193 | n/a | |
|---|
| 194 | n/a | def suite(): |
|---|
| 195 | n/a | default_suite = unittest.makeSuite(TransactionTests, "Check") |
|---|
| 196 | n/a | special_command_suite = unittest.makeSuite(SpecialCommandTests, "Check") |
|---|
| 197 | n/a | ddl_suite = unittest.makeSuite(TransactionalDDL, "Check") |
|---|
| 198 | n/a | return unittest.TestSuite((default_suite, special_command_suite, ddl_suite)) |
|---|
| 199 | n/a | |
|---|
| 200 | n/a | def test(): |
|---|
| 201 | n/a | runner = unittest.TextTestRunner() |
|---|
| 202 | n/a | runner.run(suite()) |
|---|
| 203 | n/a | |
|---|
| 204 | n/a | if __name__ == "__main__": |
|---|
| 205 | n/a | test() |
|---|