Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1 | # pysqlite2/test/dbapi.py: tests for DB-API compliance |
| 2 | # |
Erlend Egeberg Aasland | deab1e5 | 2021-01-07 01:36:35 +0100 | [diff] [blame] | 3 | # Copyright (C) 2004-2010 Gerhard Häring <gh@ghaering.de> |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4 | # |
| 5 | # This file is part of pysqlite. |
| 6 | # |
| 7 | # This software is provided 'as-is', without any express or implied |
| 8 | # warranty. In no event will the authors be held liable for any damages |
| 9 | # arising from the use of this software. |
| 10 | # |
| 11 | # Permission is granted to anyone to use this software for any purpose, |
| 12 | # including commercial applications, and to alter it and redistribute it |
| 13 | # freely, subject to the following restrictions: |
| 14 | # |
| 15 | # 1. The origin of this software must not be misrepresented; you must not |
| 16 | # claim that you wrote the original software. If you use this software |
| 17 | # in a product, an acknowledgment in the product documentation would be |
| 18 | # appreciated but is not required. |
| 19 | # 2. Altered source versions must be plainly marked as such, and must not be |
| 20 | # misrepresented as being the original software. |
| 21 | # 3. This notice may not be removed or altered from any source distribution. |
| 22 | |
Erlend Egeberg Aasland | 2a80893 | 2021-08-28 20:26:00 +0200 | [diff] [blame^] | 23 | import subprocess |
Antoine Pitrou | a6a4dc8 | 2017-09-07 18:56:24 +0200 | [diff] [blame] | 24 | import threading |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 25 | import unittest |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 26 | import sqlite3 as sqlite |
Erlend Egeberg Aasland | d16f617 | 2021-01-09 12:25:55 +0100 | [diff] [blame] | 27 | import sys |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 28 | |
Erlend Egeberg Aasland | 2a80893 | 2021-08-28 20:26:00 +0200 | [diff] [blame^] | 29 | from test.support import check_disallow_instantiation, SHORT_TIMEOUT |
Hai Shi | fcce8c6 | 2020-08-08 05:55:35 +0800 | [diff] [blame] | 30 | from test.support.os_helper import TESTFN, unlink |
Antoine Pitrou | 902fc8b | 2013-02-10 00:02:44 +0100 | [diff] [blame] | 31 | |
| 32 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 33 | class ModuleTests(unittest.TestCase): |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 34 | def test_api_level(self): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 35 | self.assertEqual(sqlite.apilevel, "2.0", |
| 36 | "apilevel is %s, should be 2.0" % sqlite.apilevel) |
| 37 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 38 | def test_thread_safety(self): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 39 | self.assertEqual(sqlite.threadsafety, 1, |
| 40 | "threadsafety is %d, should be 1" % sqlite.threadsafety) |
| 41 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 42 | def test_param_style(self): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 43 | self.assertEqual(sqlite.paramstyle, "qmark", |
| 44 | "paramstyle is '%s', should be 'qmark'" % |
| 45 | sqlite.paramstyle) |
| 46 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 47 | def test_warning(self): |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 48 | self.assertTrue(issubclass(sqlite.Warning, Exception), |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 49 | "Warning is not a subclass of Exception") |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 50 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 51 | def test_error(self): |
Gregory P. Smith | 04cecaf | 2009-07-04 08:32:15 +0000 | [diff] [blame] | 52 | self.assertTrue(issubclass(sqlite.Error, Exception), |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 53 | "Error is not a subclass of Exception") |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 54 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 55 | def test_interface_error(self): |
Gregory P. Smith | 04cecaf | 2009-07-04 08:32:15 +0000 | [diff] [blame] | 56 | self.assertTrue(issubclass(sqlite.InterfaceError, sqlite.Error), |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 57 | "InterfaceError is not a subclass of Error") |
| 58 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 59 | def test_database_error(self): |
Gregory P. Smith | 04cecaf | 2009-07-04 08:32:15 +0000 | [diff] [blame] | 60 | self.assertTrue(issubclass(sqlite.DatabaseError, sqlite.Error), |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 61 | "DatabaseError is not a subclass of Error") |
| 62 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 63 | def test_data_error(self): |
Gregory P. Smith | 04cecaf | 2009-07-04 08:32:15 +0000 | [diff] [blame] | 64 | self.assertTrue(issubclass(sqlite.DataError, sqlite.DatabaseError), |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 65 | "DataError is not a subclass of DatabaseError") |
| 66 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 67 | def test_operational_error(self): |
Gregory P. Smith | 04cecaf | 2009-07-04 08:32:15 +0000 | [diff] [blame] | 68 | self.assertTrue(issubclass(sqlite.OperationalError, sqlite.DatabaseError), |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 69 | "OperationalError is not a subclass of DatabaseError") |
| 70 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 71 | def test_integrity_error(self): |
Gregory P. Smith | 04cecaf | 2009-07-04 08:32:15 +0000 | [diff] [blame] | 72 | self.assertTrue(issubclass(sqlite.IntegrityError, sqlite.DatabaseError), |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 73 | "IntegrityError is not a subclass of DatabaseError") |
| 74 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 75 | def test_internal_error(self): |
Gregory P. Smith | 04cecaf | 2009-07-04 08:32:15 +0000 | [diff] [blame] | 76 | self.assertTrue(issubclass(sqlite.InternalError, sqlite.DatabaseError), |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 77 | "InternalError is not a subclass of DatabaseError") |
| 78 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 79 | def test_programming_error(self): |
Gregory P. Smith | 04cecaf | 2009-07-04 08:32:15 +0000 | [diff] [blame] | 80 | self.assertTrue(issubclass(sqlite.ProgrammingError, sqlite.DatabaseError), |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 81 | "ProgrammingError is not a subclass of DatabaseError") |
| 82 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 83 | def test_not_supported_error(self): |
Gregory P. Smith | 04cecaf | 2009-07-04 08:32:15 +0000 | [diff] [blame] | 84 | self.assertTrue(issubclass(sqlite.NotSupportedError, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 85 | sqlite.DatabaseError), |
| 86 | "NotSupportedError is not a subclass of DatabaseError") |
| 87 | |
Erlend Egeberg Aasland | d16f617 | 2021-01-09 12:25:55 +0100 | [diff] [blame] | 88 | # sqlite3_enable_shared_cache() is deprecated on macOS and calling it may raise |
| 89 | # OperationalError on some buildbots. |
| 90 | @unittest.skipIf(sys.platform == "darwin", "shared cache is deprecated on macOS") |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 91 | def test_shared_cache_deprecated(self): |
Erlend Egeberg Aasland | ddb5e11 | 2021-01-06 01:36:04 +0100 | [diff] [blame] | 92 | for enable in (True, False): |
| 93 | with self.assertWarns(DeprecationWarning) as cm: |
| 94 | sqlite.enable_shared_cache(enable) |
| 95 | self.assertIn("dbapi.py", cm.filename) |
| 96 | |
Erlend Egeberg Aasland | ccc95c7 | 2021-06-20 23:07:31 +0200 | [diff] [blame] | 97 | def test_disallow_instantiation(self): |
| 98 | cx = sqlite.connect(":memory:") |
Erlend Egeberg Aasland | 0a3452e | 2021-06-24 01:46:25 +0200 | [diff] [blame] | 99 | check_disallow_instantiation(self, type(cx("select 1"))) |
Erlend Egeberg Aasland | ccc95c7 | 2021-06-20 23:07:31 +0200 | [diff] [blame] | 100 | |
Erlend Egeberg Aasland | ddb5e11 | 2021-01-06 01:36:04 +0100 | [diff] [blame] | 101 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 102 | class ConnectionTests(unittest.TestCase): |
R. David Murray | d35251d | 2010-06-01 01:32:12 +0000 | [diff] [blame] | 103 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 104 | def setUp(self): |
| 105 | self.cx = sqlite.connect(":memory:") |
| 106 | cu = self.cx.cursor() |
| 107 | cu.execute("create table test(id integer primary key, name text)") |
| 108 | cu.execute("insert into test(name) values (?)", ("foo",)) |
| 109 | |
| 110 | def tearDown(self): |
| 111 | self.cx.close() |
| 112 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 113 | def test_commit(self): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 114 | self.cx.commit() |
| 115 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 116 | def test_commit_after_no_changes(self): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 117 | """ |
| 118 | A commit should also work when no changes were made to the database. |
| 119 | """ |
| 120 | self.cx.commit() |
| 121 | self.cx.commit() |
| 122 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 123 | def test_rollback(self): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 124 | self.cx.rollback() |
| 125 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 126 | def test_rollback_after_no_changes(self): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 127 | """ |
| 128 | A rollback should also work when no changes were made to the database. |
| 129 | """ |
| 130 | self.cx.rollback() |
| 131 | self.cx.rollback() |
| 132 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 133 | def test_cursor(self): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 134 | cu = self.cx.cursor() |
| 135 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 136 | def test_failed_open(self): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 137 | YOU_CANNOT_OPEN_THIS = "/foo/bar/bla/23534/mydb.db" |
Berker Peksag | 1003b34 | 2016-06-12 22:34:49 +0300 | [diff] [blame] | 138 | with self.assertRaises(sqlite.OperationalError): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 139 | con = sqlite.connect(YOU_CANNOT_OPEN_THIS) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 140 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 141 | def test_close(self): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 142 | self.cx.close() |
| 143 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 144 | def test_exceptions(self): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 145 | # Optional DB-API extension. |
Gregory P. Smith | 04cecaf | 2009-07-04 08:32:15 +0000 | [diff] [blame] | 146 | self.assertEqual(self.cx.Warning, sqlite.Warning) |
| 147 | self.assertEqual(self.cx.Error, sqlite.Error) |
| 148 | self.assertEqual(self.cx.InterfaceError, sqlite.InterfaceError) |
| 149 | self.assertEqual(self.cx.DatabaseError, sqlite.DatabaseError) |
| 150 | self.assertEqual(self.cx.DataError, sqlite.DataError) |
| 151 | self.assertEqual(self.cx.OperationalError, sqlite.OperationalError) |
| 152 | self.assertEqual(self.cx.IntegrityError, sqlite.IntegrityError) |
| 153 | self.assertEqual(self.cx.InternalError, sqlite.InternalError) |
| 154 | self.assertEqual(self.cx.ProgrammingError, sqlite.ProgrammingError) |
| 155 | self.assertEqual(self.cx.NotSupportedError, sqlite.NotSupportedError) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 156 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 157 | def test_in_transaction(self): |
R. David Murray | d35251d | 2010-06-01 01:32:12 +0000 | [diff] [blame] | 158 | # Can't use db from setUp because we want to test initial state. |
| 159 | cx = sqlite.connect(":memory:") |
| 160 | cu = cx.cursor() |
| 161 | self.assertEqual(cx.in_transaction, False) |
| 162 | cu.execute("create table transactiontest(id integer primary key, name text)") |
| 163 | self.assertEqual(cx.in_transaction, False) |
| 164 | cu.execute("insert into transactiontest(name) values (?)", ("foo",)) |
| 165 | self.assertEqual(cx.in_transaction, True) |
| 166 | cu.execute("select name from transactiontest where name=?", ["foo"]) |
| 167 | row = cu.fetchone() |
| 168 | self.assertEqual(cx.in_transaction, True) |
| 169 | cx.commit() |
| 170 | self.assertEqual(cx.in_transaction, False) |
| 171 | cu.execute("select name from transactiontest where name=?", ["foo"]) |
| 172 | row = cu.fetchone() |
| 173 | self.assertEqual(cx.in_transaction, False) |
| 174 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 175 | def test_in_transaction_ro(self): |
R. David Murray | d35251d | 2010-06-01 01:32:12 +0000 | [diff] [blame] | 176 | with self.assertRaises(AttributeError): |
| 177 | self.cx.in_transaction = True |
| 178 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 179 | def test_open_with_path_like_object(self): |
Ville Skyttä | 61f82e0 | 2018-04-20 23:08:45 +0300 | [diff] [blame] | 180 | """ Checks that we can successfully connect to a database using an object that |
Anders Lorentsen | a22a127 | 2017-11-07 01:47:43 +0100 | [diff] [blame] | 181 | is PathLike, i.e. has __fspath__(). """ |
| 182 | self.addCleanup(unlink, TESTFN) |
| 183 | class Path: |
| 184 | def __fspath__(self): |
| 185 | return TESTFN |
| 186 | path = Path() |
| 187 | with sqlite.connect(path) as cx: |
| 188 | cx.execute('create table test(id integer)') |
| 189 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 190 | def test_open_uri(self): |
Antoine Pitrou | 902fc8b | 2013-02-10 00:02:44 +0100 | [diff] [blame] | 191 | self.addCleanup(unlink, TESTFN) |
| 192 | with sqlite.connect(TESTFN) as cx: |
| 193 | cx.execute('create table test(id integer)') |
| 194 | with sqlite.connect('file:' + TESTFN, uri=True) as cx: |
| 195 | cx.execute('insert into test(id) values(0)') |
| 196 | with sqlite.connect('file:' + TESTFN + '?mode=ro', uri=True) as cx: |
| 197 | with self.assertRaises(sqlite.OperationalError): |
| 198 | cx.execute('insert into test(id) values(1)') |
| 199 | |
| 200 | |
Erlend Egeberg Aasland | 0cb470e | 2021-07-30 14:01:22 +0200 | [diff] [blame] | 201 | class UninitialisedConnectionTests(unittest.TestCase): |
| 202 | def setUp(self): |
| 203 | self.cx = sqlite.Connection.__new__(sqlite.Connection) |
| 204 | |
| 205 | def test_uninit_operations(self): |
| 206 | funcs = ( |
| 207 | lambda: self.cx.isolation_level, |
| 208 | lambda: self.cx.total_changes, |
| 209 | lambda: self.cx.in_transaction, |
| 210 | lambda: self.cx.iterdump(), |
| 211 | lambda: self.cx.cursor(), |
| 212 | lambda: self.cx.close(), |
| 213 | ) |
| 214 | for func in funcs: |
| 215 | with self.subTest(func=func): |
| 216 | self.assertRaisesRegex(sqlite.ProgrammingError, |
| 217 | "Base Connection.__init__ not called", |
| 218 | func) |
| 219 | |
| 220 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 221 | class CursorTests(unittest.TestCase): |
| 222 | def setUp(self): |
| 223 | self.cx = sqlite.connect(":memory:") |
| 224 | self.cu = self.cx.cursor() |
Berker Peksag | e0b70cd | 2016-06-14 15:25:36 +0300 | [diff] [blame] | 225 | self.cu.execute( |
| 226 | "create table test(id integer primary key, name text, " |
| 227 | "income number, unique_test text unique)" |
| 228 | ) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 229 | self.cu.execute("insert into test(name) values (?)", ("foo",)) |
| 230 | |
| 231 | def tearDown(self): |
| 232 | self.cu.close() |
| 233 | self.cx.close() |
| 234 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 235 | def test_execute_no_args(self): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 236 | self.cu.execute("delete from test") |
| 237 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 238 | def test_execute_illegal_sql(self): |
Berker Peksag | 1003b34 | 2016-06-12 22:34:49 +0300 | [diff] [blame] | 239 | with self.assertRaises(sqlite.OperationalError): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 240 | self.cu.execute("select asdf") |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 241 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 242 | def test_execute_too_much_sql(self): |
Berker Peksag | 1003b34 | 2016-06-12 22:34:49 +0300 | [diff] [blame] | 243 | with self.assertRaises(sqlite.Warning): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 244 | self.cu.execute("select 5+4; select 4+5") |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 245 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 246 | def test_execute_too_much_sql2(self): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 247 | self.cu.execute("select 5+4; -- foo bar") |
| 248 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 249 | def test_execute_too_much_sql3(self): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 250 | self.cu.execute(""" |
| 251 | select 5+4; |
| 252 | |
| 253 | /* |
| 254 | foo |
| 255 | */ |
| 256 | """) |
| 257 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 258 | def test_execute_wrong_sql_arg(self): |
Victor Stinner | c6a2320 | 2019-06-26 03:16:24 +0200 | [diff] [blame] | 259 | with self.assertRaises(TypeError): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 260 | self.cu.execute(42) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 261 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 262 | def test_execute_arg_int(self): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 263 | self.cu.execute("insert into test(id) values (?)", (42,)) |
| 264 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 265 | def test_execute_arg_float(self): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 266 | self.cu.execute("insert into test(income) values (?)", (2500.32,)) |
| 267 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 268 | def test_execute_arg_string(self): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 269 | self.cu.execute("insert into test(name) values (?)", ("Hugo",)) |
| 270 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 271 | def test_execute_arg_string_with_zero_byte(self): |
Petri Lehtinen | 023fe33 | 2012-02-01 22:18:19 +0200 | [diff] [blame] | 272 | self.cu.execute("insert into test(name) values (?)", ("Hu\x00go",)) |
| 273 | |
| 274 | self.cu.execute("select name from test where id=?", (self.cu.lastrowid,)) |
| 275 | row = self.cu.fetchone() |
| 276 | self.assertEqual(row[0], "Hu\x00go") |
| 277 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 278 | def test_execute_non_iterable(self): |
Berker Peksag | c415440 | 2016-06-12 13:41:47 +0300 | [diff] [blame] | 279 | with self.assertRaises(ValueError) as cm: |
| 280 | self.cu.execute("insert into test(id) values (?)", 42) |
| 281 | self.assertEqual(str(cm.exception), 'parameters are of unsupported type') |
| 282 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 283 | def test_execute_wrong_no_of_args1(self): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 284 | # too many parameters |
Berker Peksag | 1003b34 | 2016-06-12 22:34:49 +0300 | [diff] [blame] | 285 | with self.assertRaises(sqlite.ProgrammingError): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 286 | self.cu.execute("insert into test(id) values (?)", (17, "Egon")) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 287 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 288 | def test_execute_wrong_no_of_args2(self): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 289 | # too little parameters |
Berker Peksag | 1003b34 | 2016-06-12 22:34:49 +0300 | [diff] [blame] | 290 | with self.assertRaises(sqlite.ProgrammingError): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 291 | self.cu.execute("insert into test(id) values (?)") |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 292 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 293 | def test_execute_wrong_no_of_args3(self): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 294 | # no parameters, parameters are needed |
Berker Peksag | 1003b34 | 2016-06-12 22:34:49 +0300 | [diff] [blame] | 295 | with self.assertRaises(sqlite.ProgrammingError): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 296 | self.cu.execute("insert into test(id) values (?)") |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 297 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 298 | def test_execute_param_list(self): |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 299 | self.cu.execute("insert into test(name) values ('foo')") |
| 300 | self.cu.execute("select name from test where name=?", ["foo"]) |
| 301 | row = self.cu.fetchone() |
Gregory P. Smith | 04cecaf | 2009-07-04 08:32:15 +0000 | [diff] [blame] | 302 | self.assertEqual(row[0], "foo") |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 303 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 304 | def test_execute_param_sequence(self): |
Serhiy Storchaka | 0b419b7 | 2020-09-17 10:35:44 +0300 | [diff] [blame] | 305 | class L: |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 306 | def __len__(self): |
| 307 | return 1 |
| 308 | def __getitem__(self, x): |
| 309 | assert x == 0 |
| 310 | return "foo" |
| 311 | |
| 312 | self.cu.execute("insert into test(name) values ('foo')") |
| 313 | self.cu.execute("select name from test where name=?", L()) |
| 314 | row = self.cu.fetchone() |
Gregory P. Smith | 04cecaf | 2009-07-04 08:32:15 +0000 | [diff] [blame] | 315 | self.assertEqual(row[0], "foo") |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 316 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 317 | def test_execute_param_sequence_bad_len(self): |
Serhiy Storchaka | 0b419b7 | 2020-09-17 10:35:44 +0300 | [diff] [blame] | 318 | # Issue41662: Error in __len__() was overridden with ProgrammingError. |
| 319 | class L: |
| 320 | def __len__(self): |
| 321 | 1/0 |
| 322 | def __getitem__(slf, x): |
| 323 | raise AssertionError |
| 324 | |
| 325 | self.cu.execute("insert into test(name) values ('foo')") |
| 326 | with self.assertRaises(ZeroDivisionError): |
| 327 | self.cu.execute("select name from test where name=?", L()) |
| 328 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 329 | def test_execute_dict_mapping(self): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 330 | self.cu.execute("insert into test(name) values ('foo')") |
| 331 | self.cu.execute("select name from test where name=:name", {"name": "foo"}) |
| 332 | row = self.cu.fetchone() |
Gregory P. Smith | 04cecaf | 2009-07-04 08:32:15 +0000 | [diff] [blame] | 333 | self.assertEqual(row[0], "foo") |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 334 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 335 | def test_execute_dict_mapping_mapping(self): |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 336 | class D(dict): |
| 337 | def __missing__(self, key): |
| 338 | return "foo" |
| 339 | |
| 340 | self.cu.execute("insert into test(name) values ('foo')") |
| 341 | self.cu.execute("select name from test where name=:name", D()) |
| 342 | row = self.cu.fetchone() |
Gregory P. Smith | 04cecaf | 2009-07-04 08:32:15 +0000 | [diff] [blame] | 343 | self.assertEqual(row[0], "foo") |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 344 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 345 | def test_execute_dict_mapping_too_little_args(self): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 346 | self.cu.execute("insert into test(name) values ('foo')") |
Berker Peksag | 1003b34 | 2016-06-12 22:34:49 +0300 | [diff] [blame] | 347 | with self.assertRaises(sqlite.ProgrammingError): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 348 | self.cu.execute("select name from test where name=:name and id=:id", {"name": "foo"}) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 349 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 350 | def test_execute_dict_mapping_no_args(self): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 351 | self.cu.execute("insert into test(name) values ('foo')") |
Berker Peksag | 1003b34 | 2016-06-12 22:34:49 +0300 | [diff] [blame] | 352 | with self.assertRaises(sqlite.ProgrammingError): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 353 | self.cu.execute("select name from test where name=:name") |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 354 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 355 | def test_execute_dict_mapping_unnamed(self): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 356 | self.cu.execute("insert into test(name) values ('foo')") |
Berker Peksag | 1003b34 | 2016-06-12 22:34:49 +0300 | [diff] [blame] | 357 | with self.assertRaises(sqlite.ProgrammingError): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 358 | self.cu.execute("select name from test where name=?", {"name": "foo"}) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 359 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 360 | def test_close(self): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 361 | self.cu.close() |
| 362 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 363 | def test_rowcount_execute(self): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 364 | self.cu.execute("delete from test") |
| 365 | self.cu.execute("insert into test(name) values ('foo')") |
| 366 | self.cu.execute("insert into test(name) values ('foo')") |
| 367 | self.cu.execute("update test set name='bar'") |
Gregory P. Smith | 04cecaf | 2009-07-04 08:32:15 +0000 | [diff] [blame] | 368 | self.assertEqual(self.cu.rowcount, 2) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 369 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 370 | def test_rowcount_select(self): |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 371 | """ |
| 372 | pysqlite does not know the rowcount of SELECT statements, because we |
| 373 | don't fetch all rows after executing the select statement. The rowcount |
| 374 | has thus to be -1. |
| 375 | """ |
| 376 | self.cu.execute("select 5 union select 6") |
Gregory P. Smith | 04cecaf | 2009-07-04 08:32:15 +0000 | [diff] [blame] | 377 | self.assertEqual(self.cu.rowcount, -1) |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 378 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 379 | def test_rowcount_executemany(self): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 380 | self.cu.execute("delete from test") |
| 381 | self.cu.executemany("insert into test(name) values (?)", [(1,), (2,), (3,)]) |
Gregory P. Smith | 04cecaf | 2009-07-04 08:32:15 +0000 | [diff] [blame] | 382 | self.assertEqual(self.cu.rowcount, 3) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 383 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 384 | def test_total_changes(self): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 385 | self.cu.execute("insert into test(name) values ('foo')") |
| 386 | self.cu.execute("insert into test(name) values ('foo')") |
Berker Peksag | 48b5c98 | 2016-06-14 00:42:50 +0300 | [diff] [blame] | 387 | self.assertLess(2, self.cx.total_changes, msg='total changes reported wrong value') |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 388 | |
| 389 | # Checks for executemany: |
| 390 | # Sequences are required by the DB-API, iterators |
| 391 | # enhancements in pysqlite. |
| 392 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 393 | def test_execute_many_sequence(self): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 394 | self.cu.executemany("insert into test(income) values (?)", [(x,) for x in range(100, 110)]) |
| 395 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 396 | def test_execute_many_iterator(self): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 397 | class MyIter: |
| 398 | def __init__(self): |
| 399 | self.value = 5 |
| 400 | |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 401 | def __next__(self): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 402 | if self.value == 10: |
| 403 | raise StopIteration |
| 404 | else: |
| 405 | self.value += 1 |
| 406 | return (self.value,) |
| 407 | |
| 408 | self.cu.executemany("insert into test(income) values (?)", MyIter()) |
| 409 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 410 | def test_execute_many_generator(self): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 411 | def mygen(): |
| 412 | for i in range(5): |
| 413 | yield (i,) |
| 414 | |
| 415 | self.cu.executemany("insert into test(income) values (?)", mygen()) |
| 416 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 417 | def test_execute_many_wrong_sql_arg(self): |
Victor Stinner | c6a2320 | 2019-06-26 03:16:24 +0200 | [diff] [blame] | 418 | with self.assertRaises(TypeError): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 419 | self.cu.executemany(42, [(3,)]) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 420 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 421 | def test_execute_many_select(self): |
Berker Peksag | 1003b34 | 2016-06-12 22:34:49 +0300 | [diff] [blame] | 422 | with self.assertRaises(sqlite.ProgrammingError): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 423 | self.cu.executemany("select ?", [(3,)]) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 424 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 425 | def test_execute_many_not_iterable(self): |
Berker Peksag | 1003b34 | 2016-06-12 22:34:49 +0300 | [diff] [blame] | 426 | with self.assertRaises(TypeError): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 427 | self.cu.executemany("insert into test(income) values (?)", 42) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 428 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 429 | def test_fetch_iter(self): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 430 | # Optional DB-API extension. |
| 431 | self.cu.execute("delete from test") |
| 432 | self.cu.execute("insert into test(id) values (?)", (5,)) |
| 433 | self.cu.execute("insert into test(id) values (?)", (6,)) |
| 434 | self.cu.execute("select id from test order by id") |
| 435 | lst = [] |
| 436 | for row in self.cu: |
| 437 | lst.append(row[0]) |
Gregory P. Smith | 04cecaf | 2009-07-04 08:32:15 +0000 | [diff] [blame] | 438 | self.assertEqual(lst[0], 5) |
| 439 | self.assertEqual(lst[1], 6) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 440 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 441 | def test_fetchone(self): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 442 | self.cu.execute("select name from test") |
| 443 | row = self.cu.fetchone() |
Gregory P. Smith | 04cecaf | 2009-07-04 08:32:15 +0000 | [diff] [blame] | 444 | self.assertEqual(row[0], "foo") |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 445 | row = self.cu.fetchone() |
Gregory P. Smith | 04cecaf | 2009-07-04 08:32:15 +0000 | [diff] [blame] | 446 | self.assertEqual(row, None) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 447 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 448 | def test_fetchone_no_statement(self): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 449 | cur = self.cx.cursor() |
| 450 | row = cur.fetchone() |
Gregory P. Smith | 04cecaf | 2009-07-04 08:32:15 +0000 | [diff] [blame] | 451 | self.assertEqual(row, None) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 452 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 453 | def test_array_size(self): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 454 | # must default ot 1 |
Gregory P. Smith | 04cecaf | 2009-07-04 08:32:15 +0000 | [diff] [blame] | 455 | self.assertEqual(self.cu.arraysize, 1) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 456 | |
| 457 | # now set to 2 |
| 458 | self.cu.arraysize = 2 |
| 459 | |
| 460 | # now make the query return 3 rows |
| 461 | self.cu.execute("delete from test") |
| 462 | self.cu.execute("insert into test(name) values ('A')") |
| 463 | self.cu.execute("insert into test(name) values ('B')") |
| 464 | self.cu.execute("insert into test(name) values ('C')") |
| 465 | self.cu.execute("select name from test") |
| 466 | res = self.cu.fetchmany() |
| 467 | |
Gregory P. Smith | 04cecaf | 2009-07-04 08:32:15 +0000 | [diff] [blame] | 468 | self.assertEqual(len(res), 2) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 469 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 470 | def test_fetchmany(self): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 471 | self.cu.execute("select name from test") |
| 472 | res = self.cu.fetchmany(100) |
Gregory P. Smith | 04cecaf | 2009-07-04 08:32:15 +0000 | [diff] [blame] | 473 | self.assertEqual(len(res), 1) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 474 | res = self.cu.fetchmany(100) |
Gregory P. Smith | 04cecaf | 2009-07-04 08:32:15 +0000 | [diff] [blame] | 475 | self.assertEqual(res, []) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 476 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 477 | def test_fetchmany_kw_arg(self): |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 478 | """Checks if fetchmany works with keyword arguments""" |
| 479 | self.cu.execute("select name from test") |
| 480 | res = self.cu.fetchmany(size=100) |
Gregory P. Smith | 04cecaf | 2009-07-04 08:32:15 +0000 | [diff] [blame] | 481 | self.assertEqual(len(res), 1) |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 482 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 483 | def test_fetchall(self): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 484 | self.cu.execute("select name from test") |
| 485 | res = self.cu.fetchall() |
Gregory P. Smith | 04cecaf | 2009-07-04 08:32:15 +0000 | [diff] [blame] | 486 | self.assertEqual(len(res), 1) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 487 | res = self.cu.fetchall() |
Gregory P. Smith | 04cecaf | 2009-07-04 08:32:15 +0000 | [diff] [blame] | 488 | self.assertEqual(res, []) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 489 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 490 | def test_setinputsizes(self): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 491 | self.cu.setinputsizes([3, 4, 5]) |
| 492 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 493 | def test_setoutputsize(self): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 494 | self.cu.setoutputsize(5, 0) |
| 495 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 496 | def test_setoutputsize_no_column(self): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 497 | self.cu.setoutputsize(42) |
| 498 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 499 | def test_cursor_connection(self): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 500 | # Optional DB-API extension. |
Gregory P. Smith | 04cecaf | 2009-07-04 08:32:15 +0000 | [diff] [blame] | 501 | self.assertEqual(self.cu.connection, self.cx) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 502 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 503 | def test_wrong_cursor_callable(self): |
Berker Peksag | 1003b34 | 2016-06-12 22:34:49 +0300 | [diff] [blame] | 504 | with self.assertRaises(TypeError): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 505 | def f(): pass |
| 506 | cur = self.cx.cursor(f) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 507 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 508 | def test_cursor_wrong_class(self): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 509 | class Foo: pass |
| 510 | foo = Foo() |
Berker Peksag | 1003b34 | 2016-06-12 22:34:49 +0300 | [diff] [blame] | 511 | with self.assertRaises(TypeError): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 512 | cur = sqlite.Cursor(foo) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 513 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 514 | def test_last_row_id_on_replace(self): |
Berker Peksag | e0b70cd | 2016-06-14 15:25:36 +0300 | [diff] [blame] | 515 | """ |
| 516 | INSERT OR REPLACE and REPLACE INTO should produce the same behavior. |
| 517 | """ |
| 518 | sql = '{} INTO test(id, unique_test) VALUES (?, ?)' |
| 519 | for statement in ('INSERT OR REPLACE', 'REPLACE'): |
| 520 | with self.subTest(statement=statement): |
| 521 | self.cu.execute(sql.format(statement), (1, 'foo')) |
| 522 | self.assertEqual(self.cu.lastrowid, 1) |
| 523 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 524 | def test_last_row_id_on_ignore(self): |
Berker Peksag | e0b70cd | 2016-06-14 15:25:36 +0300 | [diff] [blame] | 525 | self.cu.execute( |
| 526 | "insert or ignore into test(unique_test) values (?)", |
| 527 | ('test',)) |
| 528 | self.assertEqual(self.cu.lastrowid, 2) |
| 529 | self.cu.execute( |
| 530 | "insert or ignore into test(unique_test) values (?)", |
| 531 | ('test',)) |
| 532 | self.assertEqual(self.cu.lastrowid, 2) |
| 533 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 534 | def test_last_row_id_insert_o_r(self): |
Berker Peksag | e0b70cd | 2016-06-14 15:25:36 +0300 | [diff] [blame] | 535 | results = [] |
| 536 | for statement in ('FAIL', 'ABORT', 'ROLLBACK'): |
| 537 | sql = 'INSERT OR {} INTO test(unique_test) VALUES (?)' |
| 538 | with self.subTest(statement='INSERT OR {}'.format(statement)): |
| 539 | self.cu.execute(sql.format(statement), (statement,)) |
| 540 | results.append((statement, self.cu.lastrowid)) |
| 541 | with self.assertRaises(sqlite.IntegrityError): |
| 542 | self.cu.execute(sql.format(statement), (statement,)) |
| 543 | results.append((statement, self.cu.lastrowid)) |
| 544 | expected = [ |
| 545 | ('FAIL', 2), ('FAIL', 2), |
| 546 | ('ABORT', 3), ('ABORT', 3), |
| 547 | ('ROLLBACK', 4), ('ROLLBACK', 4), |
| 548 | ] |
| 549 | self.assertEqual(results, expected) |
| 550 | |
| 551 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 552 | class ThreadTests(unittest.TestCase): |
| 553 | def setUp(self): |
| 554 | self.con = sqlite.connect(":memory:") |
| 555 | self.cur = self.con.cursor() |
| 556 | self.cur.execute("create table test(id integer primary key, name text, bin binary, ratio number, ts timestamp)") |
| 557 | |
| 558 | def tearDown(self): |
| 559 | self.cur.close() |
| 560 | self.con.close() |
| 561 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 562 | def test_con_cursor(self): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 563 | def run(con, errors): |
| 564 | try: |
| 565 | cur = con.cursor() |
| 566 | errors.append("did not raise ProgrammingError") |
| 567 | return |
| 568 | except sqlite.ProgrammingError: |
| 569 | return |
| 570 | except: |
| 571 | errors.append("raised wrong exception") |
| 572 | |
| 573 | errors = [] |
| 574 | t = threading.Thread(target=run, kwargs={"con": self.con, "errors": errors}) |
| 575 | t.start() |
| 576 | t.join() |
| 577 | if len(errors) > 0: |
| 578 | self.fail("\n".join(errors)) |
| 579 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 580 | def test_con_commit(self): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 581 | def run(con, errors): |
| 582 | try: |
| 583 | con.commit() |
| 584 | errors.append("did not raise ProgrammingError") |
| 585 | return |
| 586 | except sqlite.ProgrammingError: |
| 587 | return |
| 588 | except: |
| 589 | errors.append("raised wrong exception") |
| 590 | |
| 591 | errors = [] |
| 592 | t = threading.Thread(target=run, kwargs={"con": self.con, "errors": errors}) |
| 593 | t.start() |
| 594 | t.join() |
| 595 | if len(errors) > 0: |
| 596 | self.fail("\n".join(errors)) |
| 597 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 598 | def test_con_rollback(self): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 599 | def run(con, errors): |
| 600 | try: |
| 601 | con.rollback() |
| 602 | errors.append("did not raise ProgrammingError") |
| 603 | return |
| 604 | except sqlite.ProgrammingError: |
| 605 | return |
| 606 | except: |
| 607 | errors.append("raised wrong exception") |
| 608 | |
| 609 | errors = [] |
| 610 | t = threading.Thread(target=run, kwargs={"con": self.con, "errors": errors}) |
| 611 | t.start() |
| 612 | t.join() |
| 613 | if len(errors) > 0: |
| 614 | self.fail("\n".join(errors)) |
| 615 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 616 | def test_con_close(self): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 617 | def run(con, errors): |
| 618 | try: |
| 619 | con.close() |
| 620 | errors.append("did not raise ProgrammingError") |
| 621 | return |
| 622 | except sqlite.ProgrammingError: |
| 623 | return |
| 624 | except: |
| 625 | errors.append("raised wrong exception") |
| 626 | |
| 627 | errors = [] |
| 628 | t = threading.Thread(target=run, kwargs={"con": self.con, "errors": errors}) |
| 629 | t.start() |
| 630 | t.join() |
| 631 | if len(errors) > 0: |
| 632 | self.fail("\n".join(errors)) |
| 633 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 634 | def test_cur_implicit_begin(self): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 635 | def run(cur, errors): |
| 636 | try: |
| 637 | cur.execute("insert into test(name) values ('a')") |
| 638 | errors.append("did not raise ProgrammingError") |
| 639 | return |
| 640 | except sqlite.ProgrammingError: |
| 641 | return |
| 642 | except: |
| 643 | errors.append("raised wrong exception") |
| 644 | |
| 645 | errors = [] |
| 646 | t = threading.Thread(target=run, kwargs={"cur": self.cur, "errors": errors}) |
| 647 | t.start() |
| 648 | t.join() |
| 649 | if len(errors) > 0: |
| 650 | self.fail("\n".join(errors)) |
| 651 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 652 | def test_cur_close(self): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 653 | def run(cur, errors): |
| 654 | try: |
| 655 | cur.close() |
| 656 | errors.append("did not raise ProgrammingError") |
| 657 | return |
| 658 | except sqlite.ProgrammingError: |
| 659 | return |
| 660 | except: |
| 661 | errors.append("raised wrong exception") |
| 662 | |
| 663 | errors = [] |
| 664 | t = threading.Thread(target=run, kwargs={"cur": self.cur, "errors": errors}) |
| 665 | t.start() |
| 666 | t.join() |
| 667 | if len(errors) > 0: |
| 668 | self.fail("\n".join(errors)) |
| 669 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 670 | def test_cur_execute(self): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 671 | def run(cur, errors): |
| 672 | try: |
| 673 | cur.execute("select name from test") |
| 674 | errors.append("did not raise ProgrammingError") |
| 675 | return |
| 676 | except sqlite.ProgrammingError: |
| 677 | return |
| 678 | except: |
| 679 | errors.append("raised wrong exception") |
| 680 | |
| 681 | errors = [] |
| 682 | self.cur.execute("insert into test(name) values ('a')") |
| 683 | t = threading.Thread(target=run, kwargs={"cur": self.cur, "errors": errors}) |
| 684 | t.start() |
| 685 | t.join() |
| 686 | if len(errors) > 0: |
| 687 | self.fail("\n".join(errors)) |
| 688 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 689 | def test_cur_iter_next(self): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 690 | def run(cur, errors): |
| 691 | try: |
| 692 | row = cur.fetchone() |
| 693 | errors.append("did not raise ProgrammingError") |
| 694 | return |
| 695 | except sqlite.ProgrammingError: |
| 696 | return |
| 697 | except: |
| 698 | errors.append("raised wrong exception") |
| 699 | |
| 700 | errors = [] |
| 701 | self.cur.execute("insert into test(name) values ('a')") |
| 702 | self.cur.execute("select name from test") |
| 703 | t = threading.Thread(target=run, kwargs={"cur": self.cur, "errors": errors}) |
| 704 | t.start() |
| 705 | t.join() |
| 706 | if len(errors) > 0: |
| 707 | self.fail("\n".join(errors)) |
| 708 | |
| 709 | class ConstructorTests(unittest.TestCase): |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 710 | def test_date(self): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 711 | d = sqlite.Date(2004, 10, 28) |
| 712 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 713 | def test_time(self): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 714 | t = sqlite.Time(12, 39, 35) |
| 715 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 716 | def test_timestamp(self): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 717 | ts = sqlite.Timestamp(2004, 10, 28, 12, 39, 35) |
| 718 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 719 | def test_date_from_ticks(self): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 720 | d = sqlite.DateFromTicks(42) |
| 721 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 722 | def test_time_from_ticks(self): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 723 | t = sqlite.TimeFromTicks(42) |
| 724 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 725 | def test_timestamp_from_ticks(self): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 726 | ts = sqlite.TimestampFromTicks(42) |
| 727 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 728 | def test_binary(self): |
Guido van Rossum | bae07c9 | 2007-10-08 02:46:15 +0000 | [diff] [blame] | 729 | b = sqlite.Binary(b"\0'") |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 730 | |
| 731 | class ExtensionTests(unittest.TestCase): |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 732 | def test_script_string_sql(self): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 733 | con = sqlite.connect(":memory:") |
| 734 | cur = con.cursor() |
| 735 | cur.executescript(""" |
| 736 | -- bla bla |
| 737 | /* a stupid comment */ |
| 738 | create table a(i); |
| 739 | insert into a(i) values (5); |
| 740 | """) |
| 741 | cur.execute("select i from a") |
| 742 | res = cur.fetchone()[0] |
Gregory P. Smith | 04cecaf | 2009-07-04 08:32:15 +0000 | [diff] [blame] | 743 | self.assertEqual(res, 5) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 744 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 745 | def test_script_syntax_error(self): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 746 | con = sqlite.connect(":memory:") |
| 747 | cur = con.cursor() |
Berker Peksag | 1003b34 | 2016-06-12 22:34:49 +0300 | [diff] [blame] | 748 | with self.assertRaises(sqlite.OperationalError): |
Gerhard Häring | f9cee22 | 2010-03-05 15:20:03 +0000 | [diff] [blame] | 749 | cur.executescript("create table test(x); asdf; create table test2(x)") |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 750 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 751 | def test_script_error_normal(self): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 752 | con = sqlite.connect(":memory:") |
| 753 | cur = con.cursor() |
Berker Peksag | 1003b34 | 2016-06-12 22:34:49 +0300 | [diff] [blame] | 754 | with self.assertRaises(sqlite.OperationalError): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 755 | cur.executescript("create table test(sadfsadfdsa); select foo from hurz;") |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 756 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 757 | def test_cursor_executescript_as_bytes(self): |
Berker Peksag | c415440 | 2016-06-12 13:41:47 +0300 | [diff] [blame] | 758 | con = sqlite.connect(":memory:") |
| 759 | cur = con.cursor() |
| 760 | with self.assertRaises(ValueError) as cm: |
| 761 | cur.executescript(b"create table test(foo); insert into test(foo) values (5);") |
| 762 | self.assertEqual(str(cm.exception), 'script argument must be unicode.') |
| 763 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 764 | def test_connection_execute(self): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 765 | con = sqlite.connect(":memory:") |
| 766 | result = con.execute("select 5").fetchone()[0] |
Gregory P. Smith | 04cecaf | 2009-07-04 08:32:15 +0000 | [diff] [blame] | 767 | self.assertEqual(result, 5, "Basic test of Connection.execute") |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 768 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 769 | def test_connection_executemany(self): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 770 | con = sqlite.connect(":memory:") |
| 771 | con.execute("create table test(foo)") |
| 772 | con.executemany("insert into test(foo) values (?)", [(3,), (4,)]) |
| 773 | result = con.execute("select foo from test order by foo").fetchall() |
Gregory P. Smith | 04cecaf | 2009-07-04 08:32:15 +0000 | [diff] [blame] | 774 | self.assertEqual(result[0][0], 3, "Basic test of Connection.executemany") |
| 775 | self.assertEqual(result[1][0], 4, "Basic test of Connection.executemany") |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 776 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 777 | def test_connection_executescript(self): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 778 | con = sqlite.connect(":memory:") |
| 779 | con.executescript("create table test(foo); insert into test(foo) values (5);") |
| 780 | result = con.execute("select foo from test").fetchone()[0] |
Gregory P. Smith | 04cecaf | 2009-07-04 08:32:15 +0000 | [diff] [blame] | 781 | self.assertEqual(result, 5, "Basic test of Connection.executescript") |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 782 | |
Gerhard Häring | f9cee22 | 2010-03-05 15:20:03 +0000 | [diff] [blame] | 783 | class ClosedConTests(unittest.TestCase): |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 784 | def test_closed_con_cursor(self): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 785 | con = sqlite.connect(":memory:") |
| 786 | con.close() |
Berker Peksag | 1003b34 | 2016-06-12 22:34:49 +0300 | [diff] [blame] | 787 | with self.assertRaises(sqlite.ProgrammingError): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 788 | cur = con.cursor() |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 789 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 790 | def test_closed_con_commit(self): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 791 | con = sqlite.connect(":memory:") |
| 792 | con.close() |
Berker Peksag | 1003b34 | 2016-06-12 22:34:49 +0300 | [diff] [blame] | 793 | with self.assertRaises(sqlite.ProgrammingError): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 794 | con.commit() |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 795 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 796 | def test_closed_con_rollback(self): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 797 | con = sqlite.connect(":memory:") |
| 798 | con.close() |
Berker Peksag | 1003b34 | 2016-06-12 22:34:49 +0300 | [diff] [blame] | 799 | with self.assertRaises(sqlite.ProgrammingError): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 800 | con.rollback() |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 801 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 802 | def test_closed_cur_execute(self): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 803 | con = sqlite.connect(":memory:") |
| 804 | cur = con.cursor() |
| 805 | con.close() |
Berker Peksag | 1003b34 | 2016-06-12 22:34:49 +0300 | [diff] [blame] | 806 | with self.assertRaises(sqlite.ProgrammingError): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 807 | cur.execute("select 4") |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 808 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 809 | def test_closed_create_function(self): |
Gerhard Häring | f9cee22 | 2010-03-05 15:20:03 +0000 | [diff] [blame] | 810 | con = sqlite.connect(":memory:") |
| 811 | con.close() |
| 812 | def f(x): return 17 |
Berker Peksag | 1003b34 | 2016-06-12 22:34:49 +0300 | [diff] [blame] | 813 | with self.assertRaises(sqlite.ProgrammingError): |
Gerhard Häring | f9cee22 | 2010-03-05 15:20:03 +0000 | [diff] [blame] | 814 | con.create_function("foo", 1, f) |
Gerhard Häring | f9cee22 | 2010-03-05 15:20:03 +0000 | [diff] [blame] | 815 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 816 | def test_closed_create_aggregate(self): |
Gerhard Häring | f9cee22 | 2010-03-05 15:20:03 +0000 | [diff] [blame] | 817 | con = sqlite.connect(":memory:") |
| 818 | con.close() |
| 819 | class Agg: |
| 820 | def __init__(self): |
| 821 | pass |
| 822 | def step(self, x): |
| 823 | pass |
| 824 | def finalize(self): |
| 825 | return 17 |
Berker Peksag | 1003b34 | 2016-06-12 22:34:49 +0300 | [diff] [blame] | 826 | with self.assertRaises(sqlite.ProgrammingError): |
Gerhard Häring | f9cee22 | 2010-03-05 15:20:03 +0000 | [diff] [blame] | 827 | con.create_aggregate("foo", 1, Agg) |
Gerhard Häring | f9cee22 | 2010-03-05 15:20:03 +0000 | [diff] [blame] | 828 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 829 | def test_closed_set_authorizer(self): |
Gerhard Häring | f9cee22 | 2010-03-05 15:20:03 +0000 | [diff] [blame] | 830 | con = sqlite.connect(":memory:") |
| 831 | con.close() |
| 832 | def authorizer(*args): |
| 833 | return sqlite.DENY |
Berker Peksag | 1003b34 | 2016-06-12 22:34:49 +0300 | [diff] [blame] | 834 | with self.assertRaises(sqlite.ProgrammingError): |
Gerhard Häring | f9cee22 | 2010-03-05 15:20:03 +0000 | [diff] [blame] | 835 | con.set_authorizer(authorizer) |
Gerhard Häring | f9cee22 | 2010-03-05 15:20:03 +0000 | [diff] [blame] | 836 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 837 | def test_closed_set_progress_callback(self): |
Gerhard Häring | f9cee22 | 2010-03-05 15:20:03 +0000 | [diff] [blame] | 838 | con = sqlite.connect(":memory:") |
| 839 | con.close() |
| 840 | def progress(): pass |
Berker Peksag | 1003b34 | 2016-06-12 22:34:49 +0300 | [diff] [blame] | 841 | with self.assertRaises(sqlite.ProgrammingError): |
Gerhard Häring | f9cee22 | 2010-03-05 15:20:03 +0000 | [diff] [blame] | 842 | con.set_progress_handler(progress, 100) |
Gerhard Häring | f9cee22 | 2010-03-05 15:20:03 +0000 | [diff] [blame] | 843 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 844 | def test_closed_call(self): |
Gerhard Häring | f9cee22 | 2010-03-05 15:20:03 +0000 | [diff] [blame] | 845 | con = sqlite.connect(":memory:") |
| 846 | con.close() |
Berker Peksag | 1003b34 | 2016-06-12 22:34:49 +0300 | [diff] [blame] | 847 | with self.assertRaises(sqlite.ProgrammingError): |
Gerhard Häring | f9cee22 | 2010-03-05 15:20:03 +0000 | [diff] [blame] | 848 | con() |
Gerhard Häring | f9cee22 | 2010-03-05 15:20:03 +0000 | [diff] [blame] | 849 | |
| 850 | class ClosedCurTests(unittest.TestCase): |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 851 | def test_closed(self): |
Gerhard Häring | f9cee22 | 2010-03-05 15:20:03 +0000 | [diff] [blame] | 852 | con = sqlite.connect(":memory:") |
| 853 | cur = con.cursor() |
| 854 | cur.close() |
| 855 | |
| 856 | for method_name in ("execute", "executemany", "executescript", "fetchall", "fetchmany", "fetchone"): |
| 857 | if method_name in ("execute", "executescript"): |
| 858 | params = ("select 4 union select 5",) |
| 859 | elif method_name == "executemany": |
| 860 | params = ("insert into foo(bar) values (?)", [(3,), (4,)]) |
| 861 | else: |
| 862 | params = [] |
| 863 | |
Berker Peksag | 1003b34 | 2016-06-12 22:34:49 +0300 | [diff] [blame] | 864 | with self.assertRaises(sqlite.ProgrammingError): |
Gerhard Häring | f9cee22 | 2010-03-05 15:20:03 +0000 | [diff] [blame] | 865 | method = getattr(cur, method_name) |
Gerhard Häring | f9cee22 | 2010-03-05 15:20:03 +0000 | [diff] [blame] | 866 | method(*params) |
Gerhard Häring | f9cee22 | 2010-03-05 15:20:03 +0000 | [diff] [blame] | 867 | |
Berker Peksag | 4bf580d | 2016-09-07 02:04:34 +0300 | [diff] [blame] | 868 | |
| 869 | class SqliteOnConflictTests(unittest.TestCase): |
| 870 | """ |
| 871 | Tests for SQLite's "insert on conflict" feature. |
| 872 | |
| 873 | See https://www.sqlite.org/lang_conflict.html for details. |
| 874 | """ |
| 875 | |
| 876 | def setUp(self): |
| 877 | self.cx = sqlite.connect(":memory:") |
| 878 | self.cu = self.cx.cursor() |
| 879 | self.cu.execute(""" |
| 880 | CREATE TABLE test( |
| 881 | id INTEGER PRIMARY KEY, name TEXT, unique_name TEXT UNIQUE |
| 882 | ); |
| 883 | """) |
| 884 | |
| 885 | def tearDown(self): |
| 886 | self.cu.close() |
| 887 | self.cx.close() |
| 888 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 889 | def test_on_conflict_rollback_with_explicit_transaction(self): |
Berker Peksag | 4bf580d | 2016-09-07 02:04:34 +0300 | [diff] [blame] | 890 | self.cx.isolation_level = None # autocommit mode |
| 891 | self.cu = self.cx.cursor() |
| 892 | # Start an explicit transaction. |
| 893 | self.cu.execute("BEGIN") |
| 894 | self.cu.execute("INSERT INTO test(name) VALUES ('abort_test')") |
| 895 | self.cu.execute("INSERT OR ROLLBACK INTO test(unique_name) VALUES ('foo')") |
| 896 | with self.assertRaises(sqlite.IntegrityError): |
| 897 | self.cu.execute("INSERT OR ROLLBACK INTO test(unique_name) VALUES ('foo')") |
| 898 | # Use connection to commit. |
| 899 | self.cx.commit() |
| 900 | self.cu.execute("SELECT name, unique_name from test") |
| 901 | # Transaction should have rolled back and nothing should be in table. |
| 902 | self.assertEqual(self.cu.fetchall(), []) |
| 903 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 904 | def test_on_conflict_abort_raises_with_explicit_transactions(self): |
Berker Peksag | 4bf580d | 2016-09-07 02:04:34 +0300 | [diff] [blame] | 905 | # Abort cancels the current sql statement but doesn't change anything |
| 906 | # about the current transaction. |
| 907 | self.cx.isolation_level = None # autocommit mode |
| 908 | self.cu = self.cx.cursor() |
| 909 | # Start an explicit transaction. |
| 910 | self.cu.execute("BEGIN") |
| 911 | self.cu.execute("INSERT INTO test(name) VALUES ('abort_test')") |
| 912 | self.cu.execute("INSERT OR ABORT INTO test(unique_name) VALUES ('foo')") |
| 913 | with self.assertRaises(sqlite.IntegrityError): |
| 914 | self.cu.execute("INSERT OR ABORT INTO test(unique_name) VALUES ('foo')") |
| 915 | self.cx.commit() |
| 916 | self.cu.execute("SELECT name, unique_name FROM test") |
| 917 | # Expect the first two inserts to work, third to do nothing. |
| 918 | self.assertEqual(self.cu.fetchall(), [('abort_test', None), (None, 'foo',)]) |
| 919 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 920 | def test_on_conflict_rollback_without_transaction(self): |
Berker Peksag | 4bf580d | 2016-09-07 02:04:34 +0300 | [diff] [blame] | 921 | # Start of implicit transaction |
| 922 | self.cu.execute("INSERT INTO test(name) VALUES ('abort_test')") |
| 923 | self.cu.execute("INSERT OR ROLLBACK INTO test(unique_name) VALUES ('foo')") |
| 924 | with self.assertRaises(sqlite.IntegrityError): |
| 925 | self.cu.execute("INSERT OR ROLLBACK INTO test(unique_name) VALUES ('foo')") |
| 926 | self.cu.execute("SELECT name, unique_name FROM test") |
| 927 | # Implicit transaction is rolled back on error. |
| 928 | self.assertEqual(self.cu.fetchall(), []) |
| 929 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 930 | def test_on_conflict_abort_raises_without_transactions(self): |
Berker Peksag | 4bf580d | 2016-09-07 02:04:34 +0300 | [diff] [blame] | 931 | # Abort cancels the current sql statement but doesn't change anything |
| 932 | # about the current transaction. |
| 933 | self.cu.execute("INSERT INTO test(name) VALUES ('abort_test')") |
| 934 | self.cu.execute("INSERT OR ABORT INTO test(unique_name) VALUES ('foo')") |
| 935 | with self.assertRaises(sqlite.IntegrityError): |
| 936 | self.cu.execute("INSERT OR ABORT INTO test(unique_name) VALUES ('foo')") |
| 937 | # Make sure all other values were inserted. |
| 938 | self.cu.execute("SELECT name, unique_name FROM test") |
| 939 | self.assertEqual(self.cu.fetchall(), [('abort_test', None), (None, 'foo',)]) |
| 940 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 941 | def test_on_conflict_fail(self): |
Berker Peksag | 4bf580d | 2016-09-07 02:04:34 +0300 | [diff] [blame] | 942 | self.cu.execute("INSERT OR FAIL INTO test(unique_name) VALUES ('foo')") |
| 943 | with self.assertRaises(sqlite.IntegrityError): |
| 944 | self.cu.execute("INSERT OR FAIL INTO test(unique_name) VALUES ('foo')") |
| 945 | self.assertEqual(self.cu.fetchall(), []) |
| 946 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 947 | def test_on_conflict_ignore(self): |
Berker Peksag | 4bf580d | 2016-09-07 02:04:34 +0300 | [diff] [blame] | 948 | self.cu.execute("INSERT OR IGNORE INTO test(unique_name) VALUES ('foo')") |
| 949 | # Nothing should happen. |
| 950 | self.cu.execute("INSERT OR IGNORE INTO test(unique_name) VALUES ('foo')") |
| 951 | self.cu.execute("SELECT unique_name FROM test") |
| 952 | self.assertEqual(self.cu.fetchall(), [('foo',)]) |
| 953 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 954 | def test_on_conflict_replace(self): |
Berker Peksag | 4bf580d | 2016-09-07 02:04:34 +0300 | [diff] [blame] | 955 | self.cu.execute("INSERT OR REPLACE INTO test(name, unique_name) VALUES ('Data!', 'foo')") |
| 956 | # There shouldn't be an IntegrityError exception. |
| 957 | self.cu.execute("INSERT OR REPLACE INTO test(name, unique_name) VALUES ('Very different data!', 'foo')") |
| 958 | self.cu.execute("SELECT name, unique_name FROM test") |
| 959 | self.assertEqual(self.cu.fetchall(), [('Very different data!', 'foo')]) |
| 960 | |
| 961 | |
Erlend Egeberg Aasland | 2a80893 | 2021-08-28 20:26:00 +0200 | [diff] [blame^] | 962 | class MultiprocessTests(unittest.TestCase): |
| 963 | CONNECTION_TIMEOUT = SHORT_TIMEOUT / 1000. # Defaults to 30 ms |
| 964 | |
| 965 | def tearDown(self): |
| 966 | unlink(TESTFN) |
| 967 | |
| 968 | def test_ctx_mgr_rollback_if_commit_failed(self): |
| 969 | # bpo-27334: ctx manager does not rollback if commit fails |
| 970 | SCRIPT = f"""if 1: |
| 971 | import sqlite3 |
| 972 | def wait(): |
| 973 | print("started") |
| 974 | assert "database is locked" in input() |
| 975 | |
| 976 | cx = sqlite3.connect("{TESTFN}", timeout={self.CONNECTION_TIMEOUT}) |
| 977 | cx.create_function("wait", 0, wait) |
| 978 | with cx: |
| 979 | cx.execute("create table t(t)") |
| 980 | try: |
| 981 | # execute two transactions; both will try to lock the db |
| 982 | cx.executescript(''' |
| 983 | -- start a transaction and wait for parent |
| 984 | begin transaction; |
| 985 | select * from t; |
| 986 | select wait(); |
| 987 | rollback; |
| 988 | |
| 989 | -- start a new transaction; would fail if parent holds lock |
| 990 | begin transaction; |
| 991 | select * from t; |
| 992 | rollback; |
| 993 | ''') |
| 994 | finally: |
| 995 | cx.close() |
| 996 | """ |
| 997 | |
| 998 | # spawn child process |
| 999 | proc = subprocess.Popen( |
| 1000 | [sys.executable, "-c", SCRIPT], |
| 1001 | encoding="utf-8", |
| 1002 | bufsize=0, |
| 1003 | stdin=subprocess.PIPE, |
| 1004 | stdout=subprocess.PIPE, |
| 1005 | ) |
| 1006 | self.addCleanup(proc.communicate) |
| 1007 | |
| 1008 | # wait for child process to start |
| 1009 | self.assertEqual("started", proc.stdout.readline().strip()) |
| 1010 | |
| 1011 | cx = sqlite.connect(TESTFN, timeout=self.CONNECTION_TIMEOUT) |
| 1012 | try: # context manager should correctly release the db lock |
| 1013 | with cx: |
| 1014 | cx.execute("insert into t values('test')") |
| 1015 | except sqlite.OperationalError as exc: |
| 1016 | proc.stdin.write(str(exc)) |
| 1017 | else: |
| 1018 | proc.stdin.write("no error") |
| 1019 | finally: |
| 1020 | cx.close() |
| 1021 | |
| 1022 | # terminate child process |
| 1023 | self.assertIsNone(proc.returncode) |
| 1024 | try: |
| 1025 | proc.communicate(input="end", timeout=SHORT_TIMEOUT) |
| 1026 | except subprocess.TimeoutExpired: |
| 1027 | proc.kill() |
| 1028 | proc.communicate() |
| 1029 | raise |
| 1030 | self.assertEqual(proc.returncode, 0) |
| 1031 | |
| 1032 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1033 | def suite(): |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 1034 | tests = [ |
| 1035 | ClosedConTests, |
| 1036 | ClosedCurTests, |
| 1037 | ConnectionTests, |
| 1038 | ConstructorTests, |
| 1039 | CursorTests, |
| 1040 | ExtensionTests, |
| 1041 | ModuleTests, |
Erlend Egeberg Aasland | 2a80893 | 2021-08-28 20:26:00 +0200 | [diff] [blame^] | 1042 | MultiprocessTests, |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 1043 | SqliteOnConflictTests, |
| 1044 | ThreadTests, |
Erlend Egeberg Aasland | 0cb470e | 2021-07-30 14:01:22 +0200 | [diff] [blame] | 1045 | UninitialisedConnectionTests, |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 1046 | ] |
| 1047 | return unittest.TestSuite( |
| 1048 | [unittest.TestLoader().loadTestsFromTestCase(t) for t in tests] |
| 1049 | ) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1050 | |
| 1051 | def test(): |
| 1052 | runner = unittest.TextTestRunner() |
| 1053 | runner.run(suite()) |
| 1054 | |
| 1055 | if __name__ == "__main__": |
| 1056 | test() |