Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1 | #-*- coding: ISO-8859-1 -*- |
| 2 | # pysqlite2/test/regression.py: pysqlite regression tests |
| 3 | # |
| 4 | # Copyright (C) 2006 Gerhard Häring <gh@ghaering.de> |
| 5 | # |
| 6 | # This file is part of pysqlite. |
| 7 | # |
| 8 | # This software is provided 'as-is', without any express or implied |
| 9 | # warranty. In no event will the authors be held liable for any damages |
| 10 | # arising from the use of this software. |
| 11 | # |
| 12 | # Permission is granted to anyone to use this software for any purpose, |
| 13 | # including commercial applications, and to alter it and redistribute it |
| 14 | # freely, subject to the following restrictions: |
| 15 | # |
| 16 | # 1. The origin of this software must not be misrepresented; you must not |
| 17 | # claim that you wrote the original software. If you use this software |
| 18 | # in a product, an acknowledgment in the product documentation would be |
| 19 | # appreciated but is not required. |
| 20 | # 2. Altered source versions must be plainly marked as such, and must not be |
| 21 | # misrepresented as being the original software. |
| 22 | # 3. This notice may not be removed or altered from any source distribution. |
| 23 | |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 24 | import datetime |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 25 | import unittest |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 26 | import sqlite3 as sqlite |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 27 | |
| 28 | class RegressionTests(unittest.TestCase): |
| 29 | def setUp(self): |
| 30 | self.con = sqlite.connect(":memory:") |
| 31 | |
| 32 | def tearDown(self): |
| 33 | self.con.close() |
| 34 | |
| 35 | def CheckPragmaUserVersion(self): |
| 36 | # This used to crash pysqlite because this pragma command returns NULL for the column name |
| 37 | cur = self.con.cursor() |
| 38 | cur.execute("pragma user_version") |
| 39 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 40 | def CheckPragmaSchemaVersion(self): |
| 41 | # This still crashed pysqlite <= 2.2.1 |
| 42 | con = sqlite.connect(":memory:", detect_types=sqlite.PARSE_COLNAMES) |
| 43 | try: |
| 44 | cur = self.con.cursor() |
| 45 | cur.execute("pragma schema_version") |
| 46 | finally: |
| 47 | cur.close() |
| 48 | con.close() |
| 49 | |
| 50 | def CheckStatementReset(self): |
| 51 | # pysqlite 2.1.0 to 2.2.0 have the problem that not all statements are |
| 52 | # reset before a rollback, but only those that are still in the |
| 53 | # statement cache. The others are not accessible from the connection object. |
| 54 | con = sqlite.connect(":memory:", cached_statements=5) |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 55 | cursors = [con.cursor() for x in range(5)] |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 56 | cursors[0].execute("create table test(x)") |
| 57 | for i in range(10): |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 58 | 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] | 59 | |
| 60 | for i in range(5): |
| 61 | cursors[i].execute(" " * i + "select x from test") |
| 62 | |
| 63 | con.rollback() |
| 64 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 65 | def CheckColumnNameWithSpaces(self): |
| 66 | cur = self.con.cursor() |
| 67 | cur.execute('select 1 as "foo bar [datetime]"') |
| 68 | self.failUnlessEqual(cur.description[0][0], "foo bar") |
| 69 | |
| 70 | cur.execute('select 1 as "foo baz"') |
| 71 | self.failUnlessEqual(cur.description[0][0], "foo baz") |
| 72 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 73 | def CheckStatementAvailable(self): |
| 74 | # pysqlite up to 2.3.2 crashed on this, because the active statement handle was not checked |
| 75 | # before trying to fetch data from it. close() destroys the active statement ... |
| 76 | con = sqlite.connect(":memory:", detect_types=sqlite.PARSE_DECLTYPES) |
| 77 | cur = con.cursor() |
| 78 | cur.execute("select 4 union select 5") |
| 79 | cur.close() |
| 80 | cur.fetchone() |
| 81 | cur.fetchone() |
| 82 | |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 83 | def CheckStatementFinalizationOnCloseDb(self): |
| 84 | # pysqlite versions <= 2.3.3 only finalized statements in the statement |
| 85 | # cache when closing the database. statements that were still |
| 86 | # referenced in cursors weren't closed an could provoke " |
| 87 | # "OperationalError: Unable to close due to unfinalised statements". |
| 88 | con = sqlite.connect(":memory:") |
| 89 | cursors = [] |
| 90 | # default statement cache size is 100 |
| 91 | for i in range(105): |
| 92 | cur = con.cursor() |
| 93 | cursors.append(cur) |
| 94 | cur.execute("select 1 x union select " + str(i)) |
| 95 | con.close() |
| 96 | |
| 97 | def CheckOnConflictRollback(self): |
| 98 | if sqlite.sqlite_version_info < (3, 2, 2): |
| 99 | return |
| 100 | con = sqlite.connect(":memory:") |
| 101 | con.execute("create table foo(x, unique(x) on conflict rollback)") |
| 102 | con.execute("insert into foo(x) values (1)") |
| 103 | try: |
| 104 | con.execute("insert into foo(x) values (1)") |
| 105 | except sqlite.DatabaseError: |
| 106 | pass |
| 107 | con.execute("insert into foo(x) values (2)") |
| 108 | try: |
| 109 | con.commit() |
| 110 | except sqlite.OperationalError: |
| 111 | self.fail("pysqlite knew nothing about the implicit ROLLBACK") |
| 112 | |
| 113 | def CheckWorkaroundForBuggySqliteTransferBindings(self): |
| 114 | """ |
| 115 | pysqlite would crash with older SQLite versions unless |
| 116 | a workaround is implemented. |
| 117 | """ |
| 118 | self.con.execute("create table foo(bar)") |
| 119 | self.con.execute("drop table foo") |
| 120 | self.con.execute("create table foo(bar)") |
| 121 | |
| 122 | def CheckEmptyStatement(self): |
| 123 | """ |
| 124 | pysqlite used to segfault with SQLite versions 3.5.x. These return NULL |
| 125 | for "no-operation" statements |
| 126 | """ |
| 127 | self.con.execute("") |
| 128 | |
| 129 | def CheckTypeMapUsage(self): |
| 130 | """ |
| 131 | pysqlite until 2.4.1 did not rebuild the row_cast_map when recompiling |
| 132 | a statement. This test exhibits the problem. |
| 133 | """ |
| 134 | SELECT = "select * from foo" |
| 135 | con = sqlite.connect(":memory:",detect_types=sqlite.PARSE_DECLTYPES) |
| 136 | con.execute("create table foo(bar timestamp)") |
| 137 | con.execute("insert into foo(bar) values (?)", (datetime.datetime.now(),)) |
| 138 | con.execute(SELECT) |
| 139 | con.execute("drop table foo") |
| 140 | con.execute("create table foo(bar integer)") |
| 141 | con.execute("insert into foo(bar) values (5)") |
| 142 | con.execute(SELECT) |
| 143 | |
Gerhard Häring | 873d9ff | 2008-02-29 22:22:09 +0000 | [diff] [blame] | 144 | def CheckErrorMsgDecodeError(self): |
| 145 | # When porting the module to Python 3.0, the error message about |
| 146 | # decoding errors disappeared. This verifies they're back again. |
| 147 | failure = None |
| 148 | try: |
| 149 | self.con.execute("select 'xxx' || ? || 'yyy' colname", (bytes(bytearray([250])),)).fetchone() |
| 150 | failure = "should have raised an OperationalError with detailed description" |
| 151 | except sqlite.OperationalError as e: |
| 152 | msg = e.args[0] |
| 153 | if not msg.startswith("Could not decode to UTF-8 column 'colname' with text 'xxx"): |
| 154 | failure = "OperationalError did not have expected description text" |
| 155 | if failure: |
| 156 | self.fail(failure) |
| 157 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 158 | def suite(): |
| 159 | regression_suite = unittest.makeSuite(RegressionTests, "Check") |
| 160 | return unittest.TestSuite((regression_suite,)) |
| 161 | |
| 162 | def test(): |
| 163 | runner = unittest.TextTestRunner() |
| 164 | runner.run(suite()) |
| 165 | |
| 166 | if __name__ == "__main__": |
| 167 | test() |