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