| 1 | n/a | #-*- coding: iso-8859-1 -*- | 
|---|
| 2 | n/a | # pysqlite2/test/hooks.py: tests for various SQLite-specific hooks | 
|---|
| 3 | n/a | # | 
|---|
| 4 | n/a | # Copyright (C) 2006-2007 Gerhard Häring <gh@ghaering.de> | 
|---|
| 5 | n/a | # | 
|---|
| 6 | n/a | # This file is part of pysqlite. | 
|---|
| 7 | n/a | # | 
|---|
| 8 | n/a | # This software is provided 'as-is', without any express or implied | 
|---|
| 9 | n/a | # warranty.  In no event will the authors be held liable for any damages | 
|---|
| 10 | n/a | # arising from the use of this software. | 
|---|
| 11 | n/a | # | 
|---|
| 12 | n/a | # Permission is granted to anyone to use this software for any purpose, | 
|---|
| 13 | n/a | # including commercial applications, and to alter it and redistribute it | 
|---|
| 14 | n/a | # freely, subject to the following restrictions: | 
|---|
| 15 | n/a | # | 
|---|
| 16 | n/a | # 1. The origin of this software must not be misrepresented; you must not | 
|---|
| 17 | n/a | #    claim that you wrote the original software. If you use this software | 
|---|
| 18 | n/a | #    in a product, an acknowledgment in the product documentation would be | 
|---|
| 19 | n/a | #    appreciated but is not required. | 
|---|
| 20 | n/a | # 2. Altered source versions must be plainly marked as such, and must not be | 
|---|
| 21 | n/a | #    misrepresented as being the original software. | 
|---|
| 22 | n/a | # 3. This notice may not be removed or altered from any source distribution. | 
|---|
| 23 | n/a |  | 
|---|
| 24 | n/a | import unittest | 
|---|
| 25 | n/a | import sqlite3 as sqlite | 
|---|
| 26 | n/a |  | 
|---|
| 27 | n/a | class CollationTests(unittest.TestCase): | 
|---|
| 28 | n/a | def CheckCreateCollationNotString(self): | 
|---|
| 29 | n/a | con = sqlite.connect(":memory:") | 
|---|
| 30 | n/a | with self.assertRaises(TypeError): | 
|---|
| 31 | n/a | con.create_collation(None, lambda x, y: (x > y) - (x < y)) | 
|---|
| 32 | n/a |  | 
|---|
| 33 | n/a | def CheckCreateCollationNotCallable(self): | 
|---|
| 34 | n/a | con = sqlite.connect(":memory:") | 
|---|
| 35 | n/a | with self.assertRaises(TypeError) as cm: | 
|---|
| 36 | n/a | con.create_collation("X", 42) | 
|---|
| 37 | n/a | self.assertEqual(str(cm.exception), 'parameter must be callable') | 
|---|
| 38 | n/a |  | 
|---|
| 39 | n/a | def CheckCreateCollationNotAscii(self): | 
|---|
| 40 | n/a | con = sqlite.connect(":memory:") | 
|---|
| 41 | n/a | with self.assertRaises(sqlite.ProgrammingError): | 
|---|
| 42 | n/a | con.create_collation("collä", lambda x, y: (x > y) - (x < y)) | 
|---|
| 43 | n/a |  | 
|---|
| 44 | n/a | def CheckCreateCollationBadUpper(self): | 
|---|
| 45 | n/a | class BadUpperStr(str): | 
|---|
| 46 | n/a | def upper(self): | 
|---|
| 47 | n/a | return None | 
|---|
| 48 | n/a | con = sqlite.connect(":memory:") | 
|---|
| 49 | n/a | mycoll = lambda x, y: -((x > y) - (x < y)) | 
|---|
| 50 | n/a | con.create_collation(BadUpperStr("mycoll"), mycoll) | 
|---|
| 51 | n/a | result = con.execute(""" | 
|---|
| 52 | n/a | select x from ( | 
|---|
| 53 | n/a | select 'a' as x | 
|---|
| 54 | n/a | union | 
|---|
| 55 | n/a | select 'b' as x | 
|---|
| 56 | n/a | ) order by x collate mycoll | 
|---|
| 57 | n/a | """).fetchall() | 
|---|
| 58 | n/a | self.assertEqual(result[0][0], 'b') | 
|---|
| 59 | n/a | self.assertEqual(result[1][0], 'a') | 
|---|
| 60 | n/a |  | 
|---|
| 61 | n/a | @unittest.skipIf(sqlite.sqlite_version_info < (3, 2, 1), | 
|---|
| 62 | n/a | 'old SQLite versions crash on this test') | 
|---|
| 63 | n/a | def CheckCollationIsUsed(self): | 
|---|
| 64 | n/a | def mycoll(x, y): | 
|---|
| 65 | n/a | # reverse order | 
|---|
| 66 | n/a | return -((x > y) - (x < y)) | 
|---|
| 67 | n/a |  | 
|---|
| 68 | n/a | con = sqlite.connect(":memory:") | 
|---|
| 69 | n/a | con.create_collation("mycoll", mycoll) | 
|---|
| 70 | n/a | sql = """ | 
|---|
| 71 | n/a | select x from ( | 
|---|
| 72 | n/a | select 'a' as x | 
|---|
| 73 | n/a | union | 
|---|
| 74 | n/a | select 'b' as x | 
|---|
| 75 | n/a | union | 
|---|
| 76 | n/a | select 'c' as x | 
|---|
| 77 | n/a | ) order by x collate mycoll | 
|---|
| 78 | n/a | """ | 
|---|
| 79 | n/a | result = con.execute(sql).fetchall() | 
|---|
| 80 | n/a | self.assertEqual(result, [('c',), ('b',), ('a',)], | 
|---|
| 81 | n/a | msg='the expected order was not returned') | 
|---|
| 82 | n/a |  | 
|---|
| 83 | n/a | con.create_collation("mycoll", None) | 
|---|
| 84 | n/a | with self.assertRaises(sqlite.OperationalError) as cm: | 
|---|
| 85 | n/a | result = con.execute(sql).fetchall() | 
|---|
| 86 | n/a | self.assertEqual(str(cm.exception), 'no such collation sequence: mycoll') | 
|---|
| 87 | n/a |  | 
|---|
| 88 | n/a | def CheckCollationReturnsLargeInteger(self): | 
|---|
| 89 | n/a | def mycoll(x, y): | 
|---|
| 90 | n/a | # reverse order | 
|---|
| 91 | n/a | return -((x > y) - (x < y)) * 2**32 | 
|---|
| 92 | n/a | con = sqlite.connect(":memory:") | 
|---|
| 93 | n/a | con.create_collation("mycoll", mycoll) | 
|---|
| 94 | n/a | sql = """ | 
|---|
| 95 | n/a | select x from ( | 
|---|
| 96 | n/a | select 'a' as x | 
|---|
| 97 | n/a | union | 
|---|
| 98 | n/a | select 'b' as x | 
|---|
| 99 | n/a | union | 
|---|
| 100 | n/a | select 'c' as x | 
|---|
| 101 | n/a | ) order by x collate mycoll | 
|---|
| 102 | n/a | """ | 
|---|
| 103 | n/a | result = con.execute(sql).fetchall() | 
|---|
| 104 | n/a | self.assertEqual(result, [('c',), ('b',), ('a',)], | 
|---|
| 105 | n/a | msg="the expected order was not returned") | 
|---|
| 106 | n/a |  | 
|---|
| 107 | n/a | def CheckCollationRegisterTwice(self): | 
|---|
| 108 | n/a | """ | 
|---|
| 109 | n/a | Register two different collation functions under the same name. | 
|---|
| 110 | n/a | Verify that the last one is actually used. | 
|---|
| 111 | n/a | """ | 
|---|
| 112 | n/a | con = sqlite.connect(":memory:") | 
|---|
| 113 | n/a | con.create_collation("mycoll", lambda x, y: (x > y) - (x < y)) | 
|---|
| 114 | n/a | con.create_collation("mycoll", lambda x, y: -((x > y) - (x < y))) | 
|---|
| 115 | n/a | result = con.execute(""" | 
|---|
| 116 | n/a | select x from (select 'a' as x union select 'b' as x) order by x collate mycoll | 
|---|
| 117 | n/a | """).fetchall() | 
|---|
| 118 | n/a | self.assertEqual(result[0][0], 'b') | 
|---|
| 119 | n/a | self.assertEqual(result[1][0], 'a') | 
|---|
| 120 | n/a |  | 
|---|
| 121 | n/a | def CheckDeregisterCollation(self): | 
|---|
| 122 | n/a | """ | 
|---|
| 123 | n/a | Register a collation, then deregister it. Make sure an error is raised if we try | 
|---|
| 124 | n/a | to use it. | 
|---|
| 125 | n/a | """ | 
|---|
| 126 | n/a | con = sqlite.connect(":memory:") | 
|---|
| 127 | n/a | con.create_collation("mycoll", lambda x, y: (x > y) - (x < y)) | 
|---|
| 128 | n/a | con.create_collation("mycoll", None) | 
|---|
| 129 | n/a | with self.assertRaises(sqlite.OperationalError) as cm: | 
|---|
| 130 | n/a | con.execute("select 'a' as x union select 'b' as x order by x collate mycoll") | 
|---|
| 131 | n/a | self.assertEqual(str(cm.exception), 'no such collation sequence: mycoll') | 
|---|
| 132 | n/a |  | 
|---|
| 133 | n/a | class ProgressTests(unittest.TestCase): | 
|---|
| 134 | n/a | def CheckProgressHandlerUsed(self): | 
|---|
| 135 | n/a | """ | 
|---|
| 136 | n/a | Test that the progress handler is invoked once it is set. | 
|---|
| 137 | n/a | """ | 
|---|
| 138 | n/a | con = sqlite.connect(":memory:") | 
|---|
| 139 | n/a | progress_calls = [] | 
|---|
| 140 | n/a | def progress(): | 
|---|
| 141 | n/a | progress_calls.append(None) | 
|---|
| 142 | n/a | return 0 | 
|---|
| 143 | n/a | con.set_progress_handler(progress, 1) | 
|---|
| 144 | n/a | con.execute(""" | 
|---|
| 145 | n/a | create table foo(a, b) | 
|---|
| 146 | n/a | """) | 
|---|
| 147 | n/a | self.assertTrue(progress_calls) | 
|---|
| 148 | n/a |  | 
|---|
| 149 | n/a |  | 
|---|
| 150 | n/a | def CheckOpcodeCount(self): | 
|---|
| 151 | n/a | """ | 
|---|
| 152 | n/a | Test that the opcode argument is respected. | 
|---|
| 153 | n/a | """ | 
|---|
| 154 | n/a | con = sqlite.connect(":memory:") | 
|---|
| 155 | n/a | progress_calls = [] | 
|---|
| 156 | n/a | def progress(): | 
|---|
| 157 | n/a | progress_calls.append(None) | 
|---|
| 158 | n/a | return 0 | 
|---|
| 159 | n/a | con.set_progress_handler(progress, 1) | 
|---|
| 160 | n/a | curs = con.cursor() | 
|---|
| 161 | n/a | curs.execute(""" | 
|---|
| 162 | n/a | create table foo (a, b) | 
|---|
| 163 | n/a | """) | 
|---|
| 164 | n/a | first_count = len(progress_calls) | 
|---|
| 165 | n/a | progress_calls = [] | 
|---|
| 166 | n/a | con.set_progress_handler(progress, 2) | 
|---|
| 167 | n/a | curs.execute(""" | 
|---|
| 168 | n/a | create table bar (a, b) | 
|---|
| 169 | n/a | """) | 
|---|
| 170 | n/a | second_count = len(progress_calls) | 
|---|
| 171 | n/a | self.assertGreaterEqual(first_count, second_count) | 
|---|
| 172 | n/a |  | 
|---|
| 173 | n/a | def CheckCancelOperation(self): | 
|---|
| 174 | n/a | """ | 
|---|
| 175 | n/a | Test that returning a non-zero value stops the operation in progress. | 
|---|
| 176 | n/a | """ | 
|---|
| 177 | n/a | con = sqlite.connect(":memory:") | 
|---|
| 178 | n/a | progress_calls = [] | 
|---|
| 179 | n/a | def progress(): | 
|---|
| 180 | n/a | progress_calls.append(None) | 
|---|
| 181 | n/a | return 1 | 
|---|
| 182 | n/a | con.set_progress_handler(progress, 1) | 
|---|
| 183 | n/a | curs = con.cursor() | 
|---|
| 184 | n/a | self.assertRaises( | 
|---|
| 185 | n/a | sqlite.OperationalError, | 
|---|
| 186 | n/a | curs.execute, | 
|---|
| 187 | n/a | "create table bar (a, b)") | 
|---|
| 188 | n/a |  | 
|---|
| 189 | n/a | def CheckClearHandler(self): | 
|---|
| 190 | n/a | """ | 
|---|
| 191 | n/a | Test that setting the progress handler to None clears the previously set handler. | 
|---|
| 192 | n/a | """ | 
|---|
| 193 | n/a | con = sqlite.connect(":memory:") | 
|---|
| 194 | n/a | action = 0 | 
|---|
| 195 | n/a | def progress(): | 
|---|
| 196 | n/a | nonlocal action | 
|---|
| 197 | n/a | action = 1 | 
|---|
| 198 | n/a | return 0 | 
|---|
| 199 | n/a | con.set_progress_handler(progress, 1) | 
|---|
| 200 | n/a | con.set_progress_handler(None, 1) | 
|---|
| 201 | n/a | con.execute("select 1 union select 2 union select 3").fetchall() | 
|---|
| 202 | n/a | self.assertEqual(action, 0, "progress handler was not cleared") | 
|---|
| 203 | n/a |  | 
|---|
| 204 | n/a | class TraceCallbackTests(unittest.TestCase): | 
|---|
| 205 | n/a | def CheckTraceCallbackUsed(self): | 
|---|
| 206 | n/a | """ | 
|---|
| 207 | n/a | Test that the trace callback is invoked once it is set. | 
|---|
| 208 | n/a | """ | 
|---|
| 209 | n/a | con = sqlite.connect(":memory:") | 
|---|
| 210 | n/a | traced_statements = [] | 
|---|
| 211 | n/a | def trace(statement): | 
|---|
| 212 | n/a | traced_statements.append(statement) | 
|---|
| 213 | n/a | con.set_trace_callback(trace) | 
|---|
| 214 | n/a | con.execute("create table foo(a, b)") | 
|---|
| 215 | n/a | self.assertTrue(traced_statements) | 
|---|
| 216 | n/a | self.assertTrue(any("create table foo" in stmt for stmt in traced_statements)) | 
|---|
| 217 | n/a |  | 
|---|
| 218 | n/a | def CheckClearTraceCallback(self): | 
|---|
| 219 | n/a | """ | 
|---|
| 220 | n/a | Test that setting the trace callback to None clears the previously set callback. | 
|---|
| 221 | n/a | """ | 
|---|
| 222 | n/a | con = sqlite.connect(":memory:") | 
|---|
| 223 | n/a | traced_statements = [] | 
|---|
| 224 | n/a | def trace(statement): | 
|---|
| 225 | n/a | traced_statements.append(statement) | 
|---|
| 226 | n/a | con.set_trace_callback(trace) | 
|---|
| 227 | n/a | con.set_trace_callback(None) | 
|---|
| 228 | n/a | con.execute("create table foo(a, b)") | 
|---|
| 229 | n/a | self.assertFalse(traced_statements, "trace callback was not cleared") | 
|---|
| 230 | n/a |  | 
|---|
| 231 | n/a | def CheckUnicodeContent(self): | 
|---|
| 232 | n/a | """ | 
|---|
| 233 | n/a | Test that the statement can contain unicode literals. | 
|---|
| 234 | n/a | """ | 
|---|
| 235 | n/a | unicode_value = '\xf6\xe4\xfc\xd6\xc4\xdc\xdf\u20ac' | 
|---|
| 236 | n/a | con = sqlite.connect(":memory:") | 
|---|
| 237 | n/a | traced_statements = [] | 
|---|
| 238 | n/a | def trace(statement): | 
|---|
| 239 | n/a | traced_statements.append(statement) | 
|---|
| 240 | n/a | con.set_trace_callback(trace) | 
|---|
| 241 | n/a | con.execute("create table foo(x)") | 
|---|
| 242 | n/a | # Can't execute bound parameters as their values don't appear | 
|---|
| 243 | n/a | # in traced statements before SQLite 3.6.21 | 
|---|
| 244 | n/a | # (cf. http://www.sqlite.org/draft/releaselog/3_6_21.html) | 
|---|
| 245 | n/a | con.execute('insert into foo(x) values ("%s")' % unicode_value) | 
|---|
| 246 | n/a | con.commit() | 
|---|
| 247 | n/a | self.assertTrue(any(unicode_value in stmt for stmt in traced_statements), | 
|---|
| 248 | n/a | "Unicode data %s garbled in trace callback: %s" | 
|---|
| 249 | n/a | % (ascii(unicode_value), ', '.join(map(ascii, traced_statements)))) | 
|---|
| 250 | n/a |  | 
|---|
| 251 | n/a |  | 
|---|
| 252 | n/a |  | 
|---|
| 253 | n/a | def suite(): | 
|---|
| 254 | n/a | collation_suite = unittest.makeSuite(CollationTests, "Check") | 
|---|
| 255 | n/a | progress_suite = unittest.makeSuite(ProgressTests, "Check") | 
|---|
| 256 | n/a | trace_suite = unittest.makeSuite(TraceCallbackTests, "Check") | 
|---|
| 257 | n/a | return unittest.TestSuite((collation_suite, progress_suite, trace_suite)) | 
|---|
| 258 | n/a |  | 
|---|
| 259 | n/a | def test(): | 
|---|
| 260 | n/a | runner = unittest.TextTestRunner() | 
|---|
| 261 | n/a | runner.run(suite()) | 
|---|
| 262 | n/a |  | 
|---|
| 263 | n/a | if __name__ == "__main__": | 
|---|
| 264 | n/a | test() | 
|---|