Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1 | # pysqlite2/test/regression.py: pysqlite regression tests |
| 2 | # |
Erlend Egeberg Aasland | deab1e5 | 2021-01-07 01:36:35 +0100 | [diff] [blame] | 3 | # Copyright (C) 2006-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 | |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 23 | import datetime |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 24 | import unittest |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 25 | import sqlite3 as sqlite |
Oren Milman | e56ab74 | 2017-11-07 02:01:47 +0200 | [diff] [blame] | 26 | import weakref |
gescheit | b9a0376 | 2019-07-13 06:15:49 +0300 | [diff] [blame] | 27 | import functools |
Oren Milman | e56ab74 | 2017-11-07 02:01:47 +0200 | [diff] [blame] | 28 | from test import support |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 29 | |
| 30 | class RegressionTests(unittest.TestCase): |
| 31 | def setUp(self): |
| 32 | self.con = sqlite.connect(":memory:") |
| 33 | |
| 34 | def tearDown(self): |
| 35 | self.con.close() |
| 36 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 37 | def test_pragma_user_version(self): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 38 | # This used to crash pysqlite because this pragma command returns NULL for the column name |
| 39 | cur = self.con.cursor() |
| 40 | cur.execute("pragma user_version") |
| 41 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 42 | def test_pragma_schema_version(self): |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 43 | # This still crashed pysqlite <= 2.2.1 |
| 44 | con = sqlite.connect(":memory:", detect_types=sqlite.PARSE_COLNAMES) |
| 45 | try: |
| 46 | cur = self.con.cursor() |
| 47 | cur.execute("pragma schema_version") |
| 48 | finally: |
| 49 | cur.close() |
| 50 | con.close() |
| 51 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 52 | def test_statement_reset(self): |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 53 | # pysqlite 2.1.0 to 2.2.0 have the problem that not all statements are |
| 54 | # reset before a rollback, but only those that are still in the |
| 55 | # statement cache. The others are not accessible from the connection object. |
| 56 | con = sqlite.connect(":memory:", cached_statements=5) |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 57 | cursors = [con.cursor() for x in range(5)] |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 58 | cursors[0].execute("create table test(x)") |
| 59 | for i in range(10): |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 60 | cursors[0].executemany("insert into test(x) values (?)", [(x,) for x in range(10)]) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 61 | |
| 62 | for i in range(5): |
| 63 | cursors[i].execute(" " * i + "select x from test") |
| 64 | |
| 65 | con.rollback() |
| 66 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 67 | def test_column_name_with_spaces(self): |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 68 | cur = self.con.cursor() |
| 69 | cur.execute('select 1 as "foo bar [datetime]"') |
Serhiy Storchaka | b146568 | 2020-03-21 15:53:28 +0200 | [diff] [blame] | 70 | self.assertEqual(cur.description[0][0], "foo bar [datetime]") |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 71 | |
| 72 | cur.execute('select 1 as "foo baz"') |
Gregory P. Smith | 04cecaf | 2009-07-04 08:32:15 +0000 | [diff] [blame] | 73 | self.assertEqual(cur.description[0][0], "foo baz") |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 74 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 75 | def test_statement_finalization_on_close_db(self): |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 76 | # pysqlite versions <= 2.3.3 only finalized statements in the statement |
| 77 | # cache when closing the database. statements that were still |
Serhiy Storchaka | 6a7b3a7 | 2016-04-17 08:32:47 +0300 | [diff] [blame] | 78 | # referenced in cursors weren't closed and could provoke " |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 79 | # "OperationalError: Unable to close due to unfinalised statements". |
| 80 | con = sqlite.connect(":memory:") |
| 81 | cursors = [] |
| 82 | # default statement cache size is 100 |
| 83 | for i in range(105): |
| 84 | cur = con.cursor() |
| 85 | cursors.append(cur) |
| 86 | cur.execute("select 1 x union select " + str(i)) |
| 87 | con.close() |
| 88 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 89 | def test_on_conflict_rollback(self): |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 90 | con = sqlite.connect(":memory:") |
| 91 | con.execute("create table foo(x, unique(x) on conflict rollback)") |
| 92 | con.execute("insert into foo(x) values (1)") |
| 93 | try: |
| 94 | con.execute("insert into foo(x) values (1)") |
| 95 | except sqlite.DatabaseError: |
| 96 | pass |
| 97 | con.execute("insert into foo(x) values (2)") |
| 98 | try: |
| 99 | con.commit() |
| 100 | except sqlite.OperationalError: |
| 101 | self.fail("pysqlite knew nothing about the implicit ROLLBACK") |
| 102 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 103 | def test_workaround_for_buggy_sqlite_transfer_bindings(self): |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 104 | """ |
| 105 | pysqlite would crash with older SQLite versions unless |
| 106 | a workaround is implemented. |
| 107 | """ |
| 108 | self.con.execute("create table foo(bar)") |
| 109 | self.con.execute("drop table foo") |
| 110 | self.con.execute("create table foo(bar)") |
| 111 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 112 | def test_empty_statement(self): |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 113 | """ |
| 114 | pysqlite used to segfault with SQLite versions 3.5.x. These return NULL |
| 115 | for "no-operation" statements |
| 116 | """ |
| 117 | self.con.execute("") |
| 118 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 119 | def test_type_map_usage(self): |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 120 | """ |
| 121 | pysqlite until 2.4.1 did not rebuild the row_cast_map when recompiling |
| 122 | a statement. This test exhibits the problem. |
| 123 | """ |
| 124 | SELECT = "select * from foo" |
| 125 | con = sqlite.connect(":memory:",detect_types=sqlite.PARSE_DECLTYPES) |
| 126 | con.execute("create table foo(bar timestamp)") |
| 127 | con.execute("insert into foo(bar) values (?)", (datetime.datetime.now(),)) |
Miss Islington (bot) | a49398b | 2021-08-29 03:43:48 -0700 | [diff] [blame] | 128 | con.execute(SELECT).close() |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 129 | con.execute("drop table foo") |
| 130 | con.execute("create table foo(bar integer)") |
| 131 | con.execute("insert into foo(bar) values (5)") |
Miss Islington (bot) | a49398b | 2021-08-29 03:43:48 -0700 | [diff] [blame] | 132 | con.execute(SELECT).close() |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 133 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 134 | def test_bind_mutating_list(self): |
Serhiy Storchaka | 0b419b7 | 2020-09-17 10:35:44 +0300 | [diff] [blame] | 135 | # Issue41662: Crash when mutate a list of parameters during iteration. |
| 136 | class X: |
| 137 | def __conform__(self, protocol): |
| 138 | parameters.clear() |
| 139 | return "..." |
| 140 | parameters = [X(), 0] |
| 141 | con = sqlite.connect(":memory:",detect_types=sqlite.PARSE_DECLTYPES) |
| 142 | con.execute("create table foo(bar X, baz integer)") |
| 143 | # Should not crash |
| 144 | with self.assertRaises(IndexError): |
| 145 | con.execute("insert into foo(bar, baz) values (?, ?)", parameters) |
| 146 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 147 | def test_error_msg_decode_error(self): |
Gerhard Häring | 873d9ff | 2008-02-29 22:22:09 +0000 | [diff] [blame] | 148 | # When porting the module to Python 3.0, the error message about |
| 149 | # decoding errors disappeared. This verifies they're back again. |
Berker Peksag | 48b5c98 | 2016-06-14 00:42:50 +0300 | [diff] [blame] | 150 | with self.assertRaises(sqlite.OperationalError) as cm: |
Georg Brandl | 3dbca81 | 2008-07-23 16:10:53 +0000 | [diff] [blame] | 151 | self.con.execute("select 'xxx' || ? || 'yyy' colname", |
| 152 | (bytes(bytearray([250])),)).fetchone() |
Berker Peksag | 48b5c98 | 2016-06-14 00:42:50 +0300 | [diff] [blame] | 153 | msg = "Could not decode to UTF-8 column 'colname' with text 'xxx" |
| 154 | self.assertIn(msg, str(cm.exception)) |
Gerhard Häring | 873d9ff | 2008-02-29 22:22:09 +0000 | [diff] [blame] | 155 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 156 | def test_register_adapter(self): |
Georg Brandl | 3dbca81 | 2008-07-23 16:10:53 +0000 | [diff] [blame] | 157 | """ |
| 158 | See issue 3312. |
| 159 | """ |
| 160 | self.assertRaises(TypeError, sqlite.register_adapter, {}, None) |
| 161 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 162 | def test_set_isolation_level(self): |
Serhiy Storchaka | 2891492 | 2016-09-01 22:18:03 +0300 | [diff] [blame] | 163 | # See issue 27881. |
| 164 | class CustomStr(str): |
| 165 | def upper(self): |
| 166 | return None |
| 167 | def __del__(self): |
| 168 | con.isolation_level = "" |
| 169 | |
Georg Brandl | 3dbca81 | 2008-07-23 16:10:53 +0000 | [diff] [blame] | 170 | con = sqlite.connect(":memory:") |
Serhiy Storchaka | 2891492 | 2016-09-01 22:18:03 +0300 | [diff] [blame] | 171 | con.isolation_level = None |
| 172 | for level in "", "DEFERRED", "IMMEDIATE", "EXCLUSIVE": |
| 173 | with self.subTest(level=level): |
| 174 | con.isolation_level = level |
| 175 | con.isolation_level = level.lower() |
| 176 | con.isolation_level = level.capitalize() |
| 177 | con.isolation_level = CustomStr(level) |
| 178 | |
| 179 | # setting isolation_level failure should not alter previous state |
| 180 | con.isolation_level = None |
| 181 | con.isolation_level = "DEFERRED" |
| 182 | pairs = [ |
| 183 | (1, TypeError), (b'', TypeError), ("abc", ValueError), |
| 184 | ("IMMEDIATE\0EXCLUSIVE", ValueError), ("\xe9", ValueError), |
| 185 | ] |
| 186 | for value, exc in pairs: |
| 187 | with self.subTest(level=value): |
| 188 | with self.assertRaises(exc): |
| 189 | con.isolation_level = value |
| 190 | self.assertEqual(con.isolation_level, "DEFERRED") |
Georg Brandl | 3dbca81 | 2008-07-23 16:10:53 +0000 | [diff] [blame] | 191 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 192 | def test_cursor_constructor_call_check(self): |
Gerhard Häring | f9cee22 | 2010-03-05 15:20:03 +0000 | [diff] [blame] | 193 | """ |
Ezio Melotti | b5bc353 | 2013-08-17 16:11:40 +0300 | [diff] [blame] | 194 | Verifies that cursor methods check whether base class __init__ was |
| 195 | called. |
Gerhard Häring | f9cee22 | 2010-03-05 15:20:03 +0000 | [diff] [blame] | 196 | """ |
| 197 | class Cursor(sqlite.Cursor): |
| 198 | def __init__(self, con): |
| 199 | pass |
| 200 | |
| 201 | con = sqlite.connect(":memory:") |
| 202 | cur = Cursor(con) |
Berker Peksag | 1003b34 | 2016-06-12 22:34:49 +0300 | [diff] [blame] | 203 | with self.assertRaises(sqlite.ProgrammingError): |
Gerhard Häring | f9cee22 | 2010-03-05 15:20:03 +0000 | [diff] [blame] | 204 | cur.execute("select 4+5").fetchall() |
Oren Milman | edb13ae | 2017-11-07 02:09:49 +0200 | [diff] [blame] | 205 | with self.assertRaisesRegex(sqlite.ProgrammingError, |
| 206 | r'^Base Cursor\.__init__ not called\.$'): |
| 207 | cur.close() |
Gerhard Häring | f9cee22 | 2010-03-05 15:20:03 +0000 | [diff] [blame] | 208 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 209 | def test_str_subclass(self): |
Gerhard Häring | 6117f42 | 2008-09-22 06:04:51 +0000 | [diff] [blame] | 210 | """ |
| 211 | The Python 3.0 port of the module didn't cope with values of subclasses of str. |
| 212 | """ |
| 213 | class MyStr(str): pass |
| 214 | self.con.execute("select ?", (MyStr("abc"),)) |
Georg Brandl | 3dbca81 | 2008-07-23 16:10:53 +0000 | [diff] [blame] | 215 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 216 | def test_connection_constructor_call_check(self): |
Gerhard Häring | f9cee22 | 2010-03-05 15:20:03 +0000 | [diff] [blame] | 217 | """ |
Ezio Melotti | b5bc353 | 2013-08-17 16:11:40 +0300 | [diff] [blame] | 218 | Verifies that connection methods check whether base class __init__ was |
| 219 | called. |
Gerhard Häring | f9cee22 | 2010-03-05 15:20:03 +0000 | [diff] [blame] | 220 | """ |
| 221 | class Connection(sqlite.Connection): |
| 222 | def __init__(self, name): |
| 223 | pass |
| 224 | |
| 225 | con = Connection(":memory:") |
Berker Peksag | 1003b34 | 2016-06-12 22:34:49 +0300 | [diff] [blame] | 226 | with self.assertRaises(sqlite.ProgrammingError): |
Gerhard Häring | f9cee22 | 2010-03-05 15:20:03 +0000 | [diff] [blame] | 227 | cur = con.cursor() |
Gerhard Häring | f9cee22 | 2010-03-05 15:20:03 +0000 | [diff] [blame] | 228 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 229 | def test_cursor_registration(self): |
Gerhard Häring | f9cee22 | 2010-03-05 15:20:03 +0000 | [diff] [blame] | 230 | """ |
| 231 | Verifies that subclassed cursor classes are correctly registered with |
| 232 | the connection object, too. (fetch-across-rollback problem) |
| 233 | """ |
| 234 | class Connection(sqlite.Connection): |
| 235 | def cursor(self): |
| 236 | return Cursor(self) |
| 237 | |
| 238 | class Cursor(sqlite.Cursor): |
| 239 | def __init__(self, con): |
| 240 | sqlite.Cursor.__init__(self, con) |
| 241 | |
| 242 | con = Connection(":memory:") |
| 243 | cur = con.cursor() |
| 244 | cur.execute("create table foo(x)") |
| 245 | cur.executemany("insert into foo(x) values (?)", [(3,), (4,), (5,)]) |
| 246 | cur.execute("select x from foo") |
| 247 | con.rollback() |
Berker Peksag | 1003b34 | 2016-06-12 22:34:49 +0300 | [diff] [blame] | 248 | with self.assertRaises(sqlite.InterfaceError): |
Gerhard Häring | f9cee22 | 2010-03-05 15:20:03 +0000 | [diff] [blame] | 249 | cur.fetchall() |
Gerhard Häring | f9cee22 | 2010-03-05 15:20:03 +0000 | [diff] [blame] | 250 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 251 | def test_auto_commit(self): |
Gerhard Häring | f9cee22 | 2010-03-05 15:20:03 +0000 | [diff] [blame] | 252 | """ |
| 253 | Verifies that creating a connection in autocommit mode works. |
| 254 | 2.5.3 introduced a regression so that these could no longer |
| 255 | be created. |
| 256 | """ |
| 257 | con = sqlite.connect(":memory:", isolation_level=None) |
| 258 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 259 | def test_pragma_autocommit(self): |
Gerhard Häring | f9cee22 | 2010-03-05 15:20:03 +0000 | [diff] [blame] | 260 | """ |
| 261 | Verifies that running a PRAGMA statement that does an autocommit does |
| 262 | work. This did not work in 2.5.3/2.5.4. |
| 263 | """ |
Victor Stinner | 0201f44 | 2010-03-13 03:28:34 +0000 | [diff] [blame] | 264 | cur = self.con.cursor() |
Gerhard Häring | f9cee22 | 2010-03-05 15:20:03 +0000 | [diff] [blame] | 265 | cur.execute("create table foo(bar)") |
| 266 | cur.execute("insert into foo(bar) values (5)") |
| 267 | |
| 268 | cur.execute("pragma page_size") |
| 269 | row = cur.fetchone() |
| 270 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 271 | def test_connection_call(self): |
Victor Stinner | 0201f44 | 2010-03-13 03:28:34 +0000 | [diff] [blame] | 272 | """ |
| 273 | Call a connection with a non-string SQL request: check error handling |
| 274 | of the statement constructor. |
| 275 | """ |
Victor Stinner | c6a2320 | 2019-06-26 03:16:24 +0200 | [diff] [blame] | 276 | self.assertRaises(TypeError, self.con, 1) |
Gerhard Häring | f9cee22 | 2010-03-05 15:20:03 +0000 | [diff] [blame] | 277 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 278 | def test_collation(self): |
Victor Stinner | 35466c5 | 2010-04-22 11:23:23 +0000 | [diff] [blame] | 279 | def collation_cb(a, b): |
| 280 | return 1 |
| 281 | self.assertRaises(sqlite.ProgrammingError, self.con.create_collation, |
| 282 | # Lone surrogate cannot be encoded to the default encoding (utf8) |
| 283 | "\uDC80", collation_cb) |
| 284 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 285 | def test_recursive_cursor_use(self): |
Petri Lehtinen | 4a84f58 | 2011-05-09 12:24:09 +0200 | [diff] [blame] | 286 | """ |
| 287 | http://bugs.python.org/issue10811 |
| 288 | |
| 289 | Recursively using a cursor, such as when reusing it from a generator led to segfaults. |
| 290 | Now we catch recursive cursor usage and raise a ProgrammingError. |
| 291 | """ |
| 292 | con = sqlite.connect(":memory:") |
| 293 | |
| 294 | cur = con.cursor() |
| 295 | cur.execute("create table a (bar)") |
| 296 | cur.execute("create table b (baz)") |
| 297 | |
| 298 | def foo(): |
| 299 | cur.execute("insert into a (bar) values (?)", (1,)) |
| 300 | yield 1 |
| 301 | |
| 302 | with self.assertRaises(sqlite.ProgrammingError): |
| 303 | cur.executemany("insert into b (baz) values (?)", |
| 304 | ((i,) for i in foo())) |
| 305 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 306 | def test_convert_timestamp_microsecond_padding(self): |
Petri Lehtinen | 8b94514 | 2013-02-23 19:05:09 +0100 | [diff] [blame] | 307 | """ |
| 308 | http://bugs.python.org/issue14720 |
| 309 | |
| 310 | The microsecond parsing of convert_timestamp() should pad with zeros, |
| 311 | since the microsecond string "456" actually represents "456000". |
| 312 | """ |
| 313 | |
| 314 | con = sqlite.connect(":memory:", detect_types=sqlite.PARSE_DECLTYPES) |
| 315 | cur = con.cursor() |
| 316 | cur.execute("CREATE TABLE t (x TIMESTAMP)") |
Petri Lehtinen | 8b94514 | 2013-02-23 19:05:09 +0100 | [diff] [blame] | 317 | |
Petri Lehtinen | 5f79409 | 2013-02-26 21:32:02 +0200 | [diff] [blame] | 318 | # Microseconds should be 456000 |
| 319 | cur.execute("INSERT INTO t (x) VALUES ('2012-04-04 15:06:00.456')") |
| 320 | |
| 321 | # Microseconds should be truncated to 123456 |
| 322 | cur.execute("INSERT INTO t (x) VALUES ('2012-04-04 15:06:00.123456789')") |
| 323 | |
| 324 | cur.execute("SELECT * FROM t") |
| 325 | values = [x[0] for x in cur.fetchall()] |
| 326 | |
| 327 | self.assertEqual(values, [ |
| 328 | datetime.datetime(2012, 4, 4, 15, 6, 0, 456000), |
| 329 | datetime.datetime(2012, 4, 4, 15, 6, 0, 123456), |
| 330 | ]) |
Petri Lehtinen | 8b94514 | 2013-02-23 19:05:09 +0100 | [diff] [blame] | 331 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 332 | def test_invalid_isolation_level_type(self): |
Victor Stinner | cb1f74e | 2013-12-19 16:38:03 +0100 | [diff] [blame] | 333 | # isolation level is a string, not an integer |
| 334 | self.assertRaises(TypeError, |
| 335 | sqlite.connect, ":memory:", isolation_level=123) |
| 336 | |
Petri Lehtinen | 4a84f58 | 2011-05-09 12:24:09 +0200 | [diff] [blame] | 337 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 338 | def test_null_character(self): |
Serhiy Storchaka | 42d67af | 2014-09-11 13:29:05 +0300 | [diff] [blame] | 339 | # Issue #21147 |
| 340 | con = sqlite.connect(":memory:") |
| 341 | self.assertRaises(ValueError, con, "\0select 1") |
| 342 | self.assertRaises(ValueError, con, "select 1\0") |
| 343 | cur = con.cursor() |
| 344 | self.assertRaises(ValueError, cur.execute, " \0select 2") |
| 345 | self.assertRaises(ValueError, cur.execute, "select 2\0") |
| 346 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 347 | def test_commit_cursor_reset(self): |
Berker Peksag | cc9afa9 | 2016-08-26 22:07:51 +0300 | [diff] [blame] | 348 | """ |
| 349 | Connection.commit() did reset cursors, which made sqlite3 |
| 350 | to return rows multiple times when fetched from cursors |
| 351 | after commit. See issues 10513 and 23129 for details. |
| 352 | """ |
| 353 | con = sqlite.connect(":memory:") |
| 354 | con.executescript(""" |
| 355 | create table t(c); |
| 356 | create table t2(c); |
| 357 | insert into t values(0); |
| 358 | insert into t values(1); |
| 359 | insert into t values(2); |
| 360 | """) |
| 361 | |
| 362 | self.assertEqual(con.isolation_level, "") |
| 363 | |
| 364 | counter = 0 |
| 365 | for i, row in enumerate(con.execute("select c from t")): |
| 366 | with self.subTest(i=i, row=row): |
| 367 | con.execute("insert into t2(c) values (?)", (i,)) |
| 368 | con.commit() |
| 369 | if counter == 0: |
| 370 | self.assertEqual(row[0], 0) |
| 371 | elif counter == 1: |
| 372 | self.assertEqual(row[0], 1) |
| 373 | elif counter == 2: |
| 374 | self.assertEqual(row[0], 2) |
| 375 | counter += 1 |
| 376 | self.assertEqual(counter, 3, "should have returned exactly three rows") |
| 377 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 378 | def test_bpo31770(self): |
Oren Milman | e56ab74 | 2017-11-07 02:01:47 +0200 | [diff] [blame] | 379 | """ |
| 380 | The interpreter shouldn't crash in case Cursor.__init__() is called |
| 381 | more than once. |
| 382 | """ |
| 383 | def callback(*args): |
| 384 | pass |
| 385 | con = sqlite.connect(":memory:") |
| 386 | cur = sqlite.Cursor(con) |
| 387 | ref = weakref.ref(cur, callback) |
| 388 | cur.__init__(con) |
| 389 | del cur |
| 390 | # The interpreter shouldn't crash when ref is collected. |
| 391 | del ref |
| 392 | support.gc_collect() |
| 393 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 394 | def test_del_isolation_level_segfault(self): |
Zackery Spytz | 842acaa | 2018-12-17 07:52:45 -0700 | [diff] [blame] | 395 | with self.assertRaises(AttributeError): |
| 396 | del self.con.isolation_level |
| 397 | |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 398 | def test_bpo37347(self): |
gescheit | b9a0376 | 2019-07-13 06:15:49 +0300 | [diff] [blame] | 399 | class Printer: |
| 400 | def log(self, *args): |
| 401 | return sqlite.SQLITE_OK |
Serhiy Storchaka | 42d67af | 2014-09-11 13:29:05 +0300 | [diff] [blame] | 402 | |
gescheit | b9a0376 | 2019-07-13 06:15:49 +0300 | [diff] [blame] | 403 | for method in [self.con.set_trace_callback, |
| 404 | functools.partial(self.con.set_progress_handler, n=1), |
| 405 | self.con.set_authorizer]: |
| 406 | printer_instance = Printer() |
| 407 | method(printer_instance.log) |
| 408 | method(printer_instance.log) |
| 409 | self.con.execute("select 1") # trigger seg fault |
| 410 | method(None) |
Sergey Fedoseev | 5b25f1d | 2018-12-05 22:50:26 +0500 | [diff] [blame] | 411 | |
Mariusz Felisiak | 3b4b2cf | 2021-03-03 15:16:24 +0100 | [diff] [blame] | 412 | def test_return_empty_bytestring(self): |
| 413 | cur = self.con.execute("select X''") |
| 414 | val = cur.fetchone()[0] |
| 415 | self.assertEqual(val, b'') |
Sergey Fedoseev | 5b25f1d | 2018-12-05 22:50:26 +0500 | [diff] [blame] | 416 | |
| 417 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 418 | def suite(): |
Erlend Egeberg Aasland | 849e339 | 2021-01-07 01:05:07 +0100 | [diff] [blame] | 419 | tests = [ |
| 420 | RegressionTests |
| 421 | ] |
| 422 | return unittest.TestSuite( |
| 423 | [unittest.TestLoader().loadTestsFromTestCase(t) for t in tests] |
| 424 | ) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 425 | |
| 426 | def test(): |
| 427 | runner = unittest.TextTestRunner() |
| 428 | runner.run(suite()) |
| 429 | |
| 430 | if __name__ == "__main__": |
| 431 | test() |