blob: 417a53109c87c72fbc43b956614e42d3486479c9 [file] [log] [blame]
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001# pysqlite2/test/regression.py: pysqlite regression tests
2#
Erlend Egeberg Aaslanddeab1e52021-01-07 01:36:35 +01003# Copyright (C) 2006-2010 Gerhard Häring <gh@ghaering.de>
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004#
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äringe7ea7452008-03-29 00:45:29 +000023import datetime
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000024import unittest
Thomas Wouters477c8d52006-05-27 19:21:47 +000025import sqlite3 as sqlite
Oren Milmane56ab742017-11-07 02:01:47 +020026import weakref
gescheitb9a03762019-07-13 06:15:49 +030027import functools
Oren Milmane56ab742017-11-07 02:01:47 +020028from test import support
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000029
30class 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 Aasland849e3392021-01-07 01:05:07 +010037 def test_pragma_user_version(self):
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000038 # 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 Aasland849e3392021-01-07 01:05:07 +010042 def test_pragma_schema_version(self):
Thomas Wouters477c8d52006-05-27 19:21:47 +000043 # 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 Aasland849e3392021-01-07 01:05:07 +010052 def test_statement_reset(self):
Thomas Wouters477c8d52006-05-27 19:21:47 +000053 # 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 Rossum805365e2007-05-07 22:24:25 +000057 cursors = [con.cursor() for x in range(5)]
Thomas Wouters477c8d52006-05-27 19:21:47 +000058 cursors[0].execute("create table test(x)")
59 for i in range(10):
Guido van Rossum805365e2007-05-07 22:24:25 +000060 cursors[0].executemany("insert into test(x) values (?)", [(x,) for x in range(10)])
Thomas Wouters477c8d52006-05-27 19:21:47 +000061
62 for i in range(5):
63 cursors[i].execute(" " * i + "select x from test")
64
65 con.rollback()
66
Erlend Egeberg Aasland849e3392021-01-07 01:05:07 +010067 def test_column_name_with_spaces(self):
Thomas Wouters0e3f5912006-08-11 14:57:12 +000068 cur = self.con.cursor()
69 cur.execute('select 1 as "foo bar [datetime]"')
Serhiy Storchakab1465682020-03-21 15:53:28 +020070 self.assertEqual(cur.description[0][0], "foo bar [datetime]")
Thomas Wouters0e3f5912006-08-11 14:57:12 +000071
72 cur.execute('select 1 as "foo baz"')
Gregory P. Smith04cecaf2009-07-04 08:32:15 +000073 self.assertEqual(cur.description[0][0], "foo baz")
Thomas Wouters0e3f5912006-08-11 14:57:12 +000074
Erlend Egeberg Aasland849e3392021-01-07 01:05:07 +010075 def test_statement_finalization_on_close_db(self):
Gerhard Häringe7ea7452008-03-29 00:45:29 +000076 # pysqlite versions <= 2.3.3 only finalized statements in the statement
77 # cache when closing the database. statements that were still
Serhiy Storchaka6a7b3a72016-04-17 08:32:47 +030078 # referenced in cursors weren't closed and could provoke "
Gerhard Häringe7ea7452008-03-29 00:45:29 +000079 # "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 Aasland849e3392021-01-07 01:05:07 +010089 def test_on_conflict_rollback(self):
Gerhard Häringe7ea7452008-03-29 00:45:29 +000090 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 Aasland849e3392021-01-07 01:05:07 +0100103 def test_workaround_for_buggy_sqlite_transfer_bindings(self):
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000104 """
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 Aasland849e3392021-01-07 01:05:07 +0100112 def test_empty_statement(self):
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000113 """
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 Aasland849e3392021-01-07 01:05:07 +0100119 def test_type_map_usage(self):
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000120 """
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(),))
128 con.execute(SELECT)
129 con.execute("drop table foo")
130 con.execute("create table foo(bar integer)")
131 con.execute("insert into foo(bar) values (5)")
132 con.execute(SELECT)
133
Erlend Egeberg Aasland849e3392021-01-07 01:05:07 +0100134 def test_bind_mutating_list(self):
Serhiy Storchaka0b419b72020-09-17 10:35:44 +0300135 # 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 Aasland849e3392021-01-07 01:05:07 +0100147 def test_error_msg_decode_error(self):
Gerhard Häring873d9ff2008-02-29 22:22:09 +0000148 # When porting the module to Python 3.0, the error message about
149 # decoding errors disappeared. This verifies they're back again.
Berker Peksag48b5c982016-06-14 00:42:50 +0300150 with self.assertRaises(sqlite.OperationalError) as cm:
Georg Brandl3dbca812008-07-23 16:10:53 +0000151 self.con.execute("select 'xxx' || ? || 'yyy' colname",
152 (bytes(bytearray([250])),)).fetchone()
Berker Peksag48b5c982016-06-14 00:42:50 +0300153 msg = "Could not decode to UTF-8 column 'colname' with text 'xxx"
154 self.assertIn(msg, str(cm.exception))
Gerhard Häring873d9ff2008-02-29 22:22:09 +0000155
Erlend Egeberg Aasland849e3392021-01-07 01:05:07 +0100156 def test_register_adapter(self):
Georg Brandl3dbca812008-07-23 16:10:53 +0000157 """
158 See issue 3312.
159 """
160 self.assertRaises(TypeError, sqlite.register_adapter, {}, None)
161
Erlend Egeberg Aasland849e3392021-01-07 01:05:07 +0100162 def test_set_isolation_level(self):
Serhiy Storchaka28914922016-09-01 22:18:03 +0300163 # See issue 27881.
164 class CustomStr(str):
165 def upper(self):
166 return None
167 def __del__(self):
168 con.isolation_level = ""
169
Georg Brandl3dbca812008-07-23 16:10:53 +0000170 con = sqlite.connect(":memory:")
Serhiy Storchaka28914922016-09-01 22:18:03 +0300171 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 Brandl3dbca812008-07-23 16:10:53 +0000191
Erlend Egeberg Aasland849e3392021-01-07 01:05:07 +0100192 def test_cursor_constructor_call_check(self):
Gerhard Häringf9cee222010-03-05 15:20:03 +0000193 """
Ezio Melottib5bc3532013-08-17 16:11:40 +0300194 Verifies that cursor methods check whether base class __init__ was
195 called.
Gerhard Häringf9cee222010-03-05 15:20:03 +0000196 """
197 class Cursor(sqlite.Cursor):
198 def __init__(self, con):
199 pass
200
201 con = sqlite.connect(":memory:")
202 cur = Cursor(con)
Berker Peksag1003b342016-06-12 22:34:49 +0300203 with self.assertRaises(sqlite.ProgrammingError):
Gerhard Häringf9cee222010-03-05 15:20:03 +0000204 cur.execute("select 4+5").fetchall()
Oren Milmanedb13ae2017-11-07 02:09:49 +0200205 with self.assertRaisesRegex(sqlite.ProgrammingError,
206 r'^Base Cursor\.__init__ not called\.$'):
207 cur.close()
Gerhard Häringf9cee222010-03-05 15:20:03 +0000208
Erlend Egeberg Aasland849e3392021-01-07 01:05:07 +0100209 def test_str_subclass(self):
Gerhard Häring6117f422008-09-22 06:04:51 +0000210 """
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 Brandl3dbca812008-07-23 16:10:53 +0000215
Erlend Egeberg Aasland849e3392021-01-07 01:05:07 +0100216 def test_connection_constructor_call_check(self):
Gerhard Häringf9cee222010-03-05 15:20:03 +0000217 """
Ezio Melottib5bc3532013-08-17 16:11:40 +0300218 Verifies that connection methods check whether base class __init__ was
219 called.
Gerhard Häringf9cee222010-03-05 15:20:03 +0000220 """
221 class Connection(sqlite.Connection):
222 def __init__(self, name):
223 pass
224
225 con = Connection(":memory:")
Berker Peksag1003b342016-06-12 22:34:49 +0300226 with self.assertRaises(sqlite.ProgrammingError):
Gerhard Häringf9cee222010-03-05 15:20:03 +0000227 cur = con.cursor()
Gerhard Häringf9cee222010-03-05 15:20:03 +0000228
Erlend Egeberg Aasland849e3392021-01-07 01:05:07 +0100229 def test_cursor_registration(self):
Gerhard Häringf9cee222010-03-05 15:20:03 +0000230 """
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 Peksag1003b342016-06-12 22:34:49 +0300248 with self.assertRaises(sqlite.InterfaceError):
Gerhard Häringf9cee222010-03-05 15:20:03 +0000249 cur.fetchall()
Gerhard Häringf9cee222010-03-05 15:20:03 +0000250
Erlend Egeberg Aasland849e3392021-01-07 01:05:07 +0100251 def test_auto_commit(self):
Gerhard Häringf9cee222010-03-05 15:20:03 +0000252 """
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 Aasland849e3392021-01-07 01:05:07 +0100259 def test_pragma_autocommit(self):
Gerhard Häringf9cee222010-03-05 15:20:03 +0000260 """
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 Stinner0201f442010-03-13 03:28:34 +0000264 cur = self.con.cursor()
Gerhard Häringf9cee222010-03-05 15:20:03 +0000265 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 Aasland849e3392021-01-07 01:05:07 +0100271 def test_connection_call(self):
Victor Stinner0201f442010-03-13 03:28:34 +0000272 """
273 Call a connection with a non-string SQL request: check error handling
274 of the statement constructor.
275 """
Victor Stinnerc6a23202019-06-26 03:16:24 +0200276 self.assertRaises(TypeError, self.con, 1)
Gerhard Häringf9cee222010-03-05 15:20:03 +0000277
Erlend Egeberg Aasland849e3392021-01-07 01:05:07 +0100278 def test_collation(self):
Victor Stinner35466c52010-04-22 11:23:23 +0000279 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 Aasland849e3392021-01-07 01:05:07 +0100285 def test_recursive_cursor_use(self):
Petri Lehtinen4a84f582011-05-09 12:24:09 +0200286 """
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 Aasland849e3392021-01-07 01:05:07 +0100306 def test_convert_timestamp_microsecond_padding(self):
Petri Lehtinen8b945142013-02-23 19:05:09 +0100307 """
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 Lehtinen8b945142013-02-23 19:05:09 +0100317
Petri Lehtinen5f794092013-02-26 21:32:02 +0200318 # 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 Lehtinen8b945142013-02-23 19:05:09 +0100331
Erlend Egeberg Aasland849e3392021-01-07 01:05:07 +0100332 def test_invalid_isolation_level_type(self):
Victor Stinnercb1f74e2013-12-19 16:38:03 +0100333 # isolation level is a string, not an integer
334 self.assertRaises(TypeError,
335 sqlite.connect, ":memory:", isolation_level=123)
336
Petri Lehtinen4a84f582011-05-09 12:24:09 +0200337
Erlend Egeberg Aasland849e3392021-01-07 01:05:07 +0100338 def test_null_character(self):
Serhiy Storchaka42d67af2014-09-11 13:29:05 +0300339 # 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 Aasland849e3392021-01-07 01:05:07 +0100347 def test_commit_cursor_reset(self):
Berker Peksagcc9afa92016-08-26 22:07:51 +0300348 """
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 Aasland849e3392021-01-07 01:05:07 +0100378 def test_bpo31770(self):
Oren Milmane56ab742017-11-07 02:01:47 +0200379 """
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 Aasland849e3392021-01-07 01:05:07 +0100394 def test_del_isolation_level_segfault(self):
Zackery Spytz842acaa2018-12-17 07:52:45 -0700395 with self.assertRaises(AttributeError):
396 del self.con.isolation_level
397
Erlend Egeberg Aasland849e3392021-01-07 01:05:07 +0100398 def test_bpo37347(self):
gescheitb9a03762019-07-13 06:15:49 +0300399 class Printer:
400 def log(self, *args):
401 return sqlite.SQLITE_OK
Serhiy Storchaka42d67af2014-09-11 13:29:05 +0300402
gescheitb9a03762019-07-13 06:15:49 +0300403 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 Fedoseev5b25f1d2018-12-05 22:50:26 +0500411
Mariusz Felisiak3b4b2cf2021-03-03 15:16:24 +0100412 def test_return_empty_bytestring(self):
413 cur = self.con.execute("select X''")
414 val = cur.fetchone()[0]
415 self.assertEqual(val, b'')
Sergey Fedoseev5b25f1d2018-12-05 22:50:26 +0500416
417
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000418def suite():
Erlend Egeberg Aasland849e3392021-01-07 01:05:07 +0100419 tests = [
420 RegressionTests
421 ]
422 return unittest.TestSuite(
423 [unittest.TestLoader().loadTestsFromTestCase(t) for t in tests]
424 )
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000425
426def test():
427 runner = unittest.TextTestRunner()
428 runner.run(suite())
429
430if __name__ == "__main__":
431 test()