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