Petri Lehtinen | f854799 | 2012-02-02 17:17:36 +0200 | [diff] [blame] | 1 | #-*- coding: iso-8859-1 -*- |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2 | # pysqlite2/test/userfunctions.py: tests for user-defined functions and |
| 3 | # aggregates. |
| 4 | # |
Martin v. Löwis | 0311736 | 2008-03-30 20:21:00 +0000 | [diff] [blame] | 5 | # Copyright (C) 2005-2007 Gerhard Häring <gh@ghaering.de> |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6 | # |
| 7 | # This file is part of pysqlite. |
| 8 | # |
| 9 | # This software is provided 'as-is', without any express or implied |
| 10 | # warranty. In no event will the authors be held liable for any damages |
| 11 | # arising from the use of this software. |
| 12 | # |
| 13 | # Permission is granted to anyone to use this software for any purpose, |
| 14 | # including commercial applications, and to alter it and redistribute it |
| 15 | # freely, subject to the following restrictions: |
| 16 | # |
| 17 | # 1. The origin of this software must not be misrepresented; you must not |
| 18 | # claim that you wrote the original software. If you use this software |
| 19 | # in a product, an acknowledgment in the product documentation would be |
| 20 | # appreciated but is not required. |
| 21 | # 2. Altered source versions must be plainly marked as such, and must not be |
| 22 | # misrepresented as being the original software. |
| 23 | # 3. This notice may not be removed or altered from any source distribution. |
| 24 | |
| 25 | import unittest |
Sergey Fedoseev | 0830858 | 2018-07-08 12:09:20 +0500 | [diff] [blame] | 26 | import unittest.mock |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 27 | import sqlite3 as sqlite |
| 28 | |
| 29 | def func_returntext(): |
| 30 | return "foo" |
| 31 | def func_returnunicode(): |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 32 | return "bar" |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 33 | def func_returnint(): |
| 34 | return 42 |
| 35 | def func_returnfloat(): |
| 36 | return 3.14 |
| 37 | def func_returnnull(): |
| 38 | return None |
| 39 | def func_returnblob(): |
Guido van Rossum | bae07c9 | 2007-10-08 02:46:15 +0000 | [diff] [blame] | 40 | return b"blob" |
Petri Lehtinen | 4fe85ab | 2012-02-19 21:38:00 +0200 | [diff] [blame] | 41 | def func_returnlonglong(): |
| 42 | return 1<<31 |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 43 | def func_raiseexception(): |
| 44 | 5/0 |
| 45 | |
| 46 | def func_isstring(v): |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 47 | return type(v) is str |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 48 | def func_isint(v): |
| 49 | return type(v) is int |
| 50 | def func_isfloat(v): |
| 51 | return type(v) is float |
| 52 | def func_isnone(v): |
| 53 | return type(v) is type(None) |
| 54 | def func_isblob(v): |
Guido van Rossum | bae07c9 | 2007-10-08 02:46:15 +0000 | [diff] [blame] | 55 | return isinstance(v, (bytes, memoryview)) |
Petri Lehtinen | 4fe85ab | 2012-02-19 21:38:00 +0200 | [diff] [blame] | 56 | def func_islonglong(v): |
| 57 | return isinstance(v, int) and v >= 1<<31 |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 58 | |
Berker Peksag | fa0f62d | 2016-03-27 22:39:14 +0300 | [diff] [blame] | 59 | def func(*args): |
| 60 | return len(args) |
| 61 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 62 | class AggrNoStep: |
| 63 | def __init__(self): |
| 64 | pass |
| 65 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 66 | def finalize(self): |
| 67 | return 1 |
| 68 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 69 | class AggrNoFinalize: |
| 70 | def __init__(self): |
| 71 | pass |
| 72 | |
| 73 | def step(self, x): |
| 74 | pass |
| 75 | |
| 76 | class AggrExceptionInInit: |
| 77 | def __init__(self): |
| 78 | 5/0 |
| 79 | |
| 80 | def step(self, x): |
| 81 | pass |
| 82 | |
| 83 | def finalize(self): |
| 84 | pass |
| 85 | |
| 86 | class AggrExceptionInStep: |
| 87 | def __init__(self): |
| 88 | pass |
| 89 | |
| 90 | def step(self, x): |
| 91 | 5/0 |
| 92 | |
| 93 | def finalize(self): |
| 94 | return 42 |
| 95 | |
| 96 | class AggrExceptionInFinalize: |
| 97 | def __init__(self): |
| 98 | pass |
| 99 | |
| 100 | def step(self, x): |
| 101 | pass |
| 102 | |
| 103 | def finalize(self): |
| 104 | 5/0 |
| 105 | |
| 106 | class AggrCheckType: |
| 107 | def __init__(self): |
| 108 | self.val = None |
| 109 | |
| 110 | def step(self, whichType, val): |
Guido van Rossum | bae07c9 | 2007-10-08 02:46:15 +0000 | [diff] [blame] | 111 | theType = {"str": str, "int": int, "float": float, "None": type(None), |
| 112 | "blob": bytes} |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 113 | self.val = int(theType[whichType] is type(val)) |
| 114 | |
| 115 | def finalize(self): |
| 116 | return self.val |
| 117 | |
Berker Peksag | fa0f62d | 2016-03-27 22:39:14 +0300 | [diff] [blame] | 118 | class AggrCheckTypes: |
| 119 | def __init__(self): |
| 120 | self.val = 0 |
| 121 | |
| 122 | def step(self, whichType, *vals): |
| 123 | theType = {"str": str, "int": int, "float": float, "None": type(None), |
| 124 | "blob": bytes} |
| 125 | for val in vals: |
| 126 | self.val += int(theType[whichType] is type(val)) |
| 127 | |
| 128 | def finalize(self): |
| 129 | return self.val |
| 130 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 131 | class AggrSum: |
| 132 | def __init__(self): |
| 133 | self.val = 0.0 |
| 134 | |
| 135 | def step(self, val): |
| 136 | self.val += val |
| 137 | |
| 138 | def finalize(self): |
| 139 | return self.val |
| 140 | |
| 141 | class FunctionTests(unittest.TestCase): |
| 142 | def setUp(self): |
| 143 | self.con = sqlite.connect(":memory:") |
| 144 | |
| 145 | self.con.create_function("returntext", 0, func_returntext) |
| 146 | self.con.create_function("returnunicode", 0, func_returnunicode) |
| 147 | self.con.create_function("returnint", 0, func_returnint) |
| 148 | self.con.create_function("returnfloat", 0, func_returnfloat) |
| 149 | self.con.create_function("returnnull", 0, func_returnnull) |
| 150 | self.con.create_function("returnblob", 0, func_returnblob) |
Petri Lehtinen | 4fe85ab | 2012-02-19 21:38:00 +0200 | [diff] [blame] | 151 | self.con.create_function("returnlonglong", 0, func_returnlonglong) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 152 | self.con.create_function("raiseexception", 0, func_raiseexception) |
| 153 | |
| 154 | self.con.create_function("isstring", 1, func_isstring) |
| 155 | self.con.create_function("isint", 1, func_isint) |
| 156 | self.con.create_function("isfloat", 1, func_isfloat) |
| 157 | self.con.create_function("isnone", 1, func_isnone) |
| 158 | self.con.create_function("isblob", 1, func_isblob) |
Petri Lehtinen | 4fe85ab | 2012-02-19 21:38:00 +0200 | [diff] [blame] | 159 | self.con.create_function("islonglong", 1, func_islonglong) |
Berker Peksag | fa0f62d | 2016-03-27 22:39:14 +0300 | [diff] [blame] | 160 | self.con.create_function("spam", -1, func) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 161 | |
| 162 | def tearDown(self): |
| 163 | self.con.close() |
| 164 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 165 | def CheckFuncErrorOnCreate(self): |
Berker Peksag | 1003b34 | 2016-06-12 22:34:49 +0300 | [diff] [blame] | 166 | with self.assertRaises(sqlite.OperationalError): |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 167 | self.con.create_function("bla", -100, lambda x: 2*x) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 168 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 169 | def CheckFuncRefCount(self): |
| 170 | def getfunc(): |
| 171 | def f(): |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 172 | return 1 |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 173 | return f |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 174 | f = getfunc() |
| 175 | globals()["foo"] = f |
| 176 | # self.con.create_function("reftest", 0, getfunc()) |
| 177 | self.con.create_function("reftest", 0, f) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 178 | cur = self.con.cursor() |
| 179 | cur.execute("select reftest()") |
| 180 | |
| 181 | def CheckFuncReturnText(self): |
| 182 | cur = self.con.cursor() |
| 183 | cur.execute("select returntext()") |
| 184 | val = cur.fetchone()[0] |
Gregory P. Smith | 04cecaf | 2009-07-04 08:32:15 +0000 | [diff] [blame] | 185 | self.assertEqual(type(val), str) |
| 186 | self.assertEqual(val, "foo") |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 187 | |
| 188 | def CheckFuncReturnUnicode(self): |
| 189 | cur = self.con.cursor() |
| 190 | cur.execute("select returnunicode()") |
| 191 | val = cur.fetchone()[0] |
Gregory P. Smith | 04cecaf | 2009-07-04 08:32:15 +0000 | [diff] [blame] | 192 | self.assertEqual(type(val), str) |
| 193 | self.assertEqual(val, "bar") |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 194 | |
| 195 | def CheckFuncReturnInt(self): |
| 196 | cur = self.con.cursor() |
| 197 | cur.execute("select returnint()") |
| 198 | val = cur.fetchone()[0] |
Gregory P. Smith | 04cecaf | 2009-07-04 08:32:15 +0000 | [diff] [blame] | 199 | self.assertEqual(type(val), int) |
| 200 | self.assertEqual(val, 42) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 201 | |
| 202 | def CheckFuncReturnFloat(self): |
| 203 | cur = self.con.cursor() |
| 204 | cur.execute("select returnfloat()") |
| 205 | val = cur.fetchone()[0] |
Gregory P. Smith | 04cecaf | 2009-07-04 08:32:15 +0000 | [diff] [blame] | 206 | self.assertEqual(type(val), float) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 207 | if val < 3.139 or val > 3.141: |
| 208 | self.fail("wrong value") |
| 209 | |
| 210 | def CheckFuncReturnNull(self): |
| 211 | cur = self.con.cursor() |
| 212 | cur.execute("select returnnull()") |
| 213 | val = cur.fetchone()[0] |
Gregory P. Smith | 04cecaf | 2009-07-04 08:32:15 +0000 | [diff] [blame] | 214 | self.assertEqual(type(val), type(None)) |
| 215 | self.assertEqual(val, None) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 216 | |
| 217 | def CheckFuncReturnBlob(self): |
| 218 | cur = self.con.cursor() |
| 219 | cur.execute("select returnblob()") |
| 220 | val = cur.fetchone()[0] |
Gregory P. Smith | 04cecaf | 2009-07-04 08:32:15 +0000 | [diff] [blame] | 221 | self.assertEqual(type(val), bytes) |
| 222 | self.assertEqual(val, b"blob") |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 223 | |
Petri Lehtinen | 4fe85ab | 2012-02-19 21:38:00 +0200 | [diff] [blame] | 224 | def CheckFuncReturnLongLong(self): |
| 225 | cur = self.con.cursor() |
| 226 | cur.execute("select returnlonglong()") |
| 227 | val = cur.fetchone()[0] |
| 228 | self.assertEqual(val, 1<<31) |
| 229 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 230 | def CheckFuncException(self): |
| 231 | cur = self.con.cursor() |
Berker Peksag | 1003b34 | 2016-06-12 22:34:49 +0300 | [diff] [blame] | 232 | with self.assertRaises(sqlite.OperationalError) as cm: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 233 | cur.execute("select raiseexception()") |
| 234 | cur.fetchone() |
Berker Peksag | 1003b34 | 2016-06-12 22:34:49 +0300 | [diff] [blame] | 235 | self.assertEqual(str(cm.exception), 'user-defined function raised exception') |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 236 | |
| 237 | def CheckParamString(self): |
| 238 | cur = self.con.cursor() |
| 239 | cur.execute("select isstring(?)", ("foo",)) |
| 240 | val = cur.fetchone()[0] |
Gregory P. Smith | 04cecaf | 2009-07-04 08:32:15 +0000 | [diff] [blame] | 241 | self.assertEqual(val, 1) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 242 | |
| 243 | def CheckParamInt(self): |
| 244 | cur = self.con.cursor() |
| 245 | cur.execute("select isint(?)", (42,)) |
| 246 | val = cur.fetchone()[0] |
Gregory P. Smith | 04cecaf | 2009-07-04 08:32:15 +0000 | [diff] [blame] | 247 | self.assertEqual(val, 1) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 248 | |
| 249 | def CheckParamFloat(self): |
| 250 | cur = self.con.cursor() |
| 251 | cur.execute("select isfloat(?)", (3.14,)) |
| 252 | val = cur.fetchone()[0] |
Gregory P. Smith | 04cecaf | 2009-07-04 08:32:15 +0000 | [diff] [blame] | 253 | self.assertEqual(val, 1) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 254 | |
| 255 | def CheckParamNone(self): |
| 256 | cur = self.con.cursor() |
| 257 | cur.execute("select isnone(?)", (None,)) |
| 258 | val = cur.fetchone()[0] |
Gregory P. Smith | 04cecaf | 2009-07-04 08:32:15 +0000 | [diff] [blame] | 259 | self.assertEqual(val, 1) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 260 | |
| 261 | def CheckParamBlob(self): |
| 262 | cur = self.con.cursor() |
Guido van Rossum | bae07c9 | 2007-10-08 02:46:15 +0000 | [diff] [blame] | 263 | cur.execute("select isblob(?)", (memoryview(b"blob"),)) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 264 | val = cur.fetchone()[0] |
Gregory P. Smith | 04cecaf | 2009-07-04 08:32:15 +0000 | [diff] [blame] | 265 | self.assertEqual(val, 1) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 266 | |
Petri Lehtinen | 4fe85ab | 2012-02-19 21:38:00 +0200 | [diff] [blame] | 267 | def CheckParamLongLong(self): |
| 268 | cur = self.con.cursor() |
| 269 | cur.execute("select islonglong(?)", (1<<42,)) |
| 270 | val = cur.fetchone()[0] |
| 271 | self.assertEqual(val, 1) |
| 272 | |
Berker Peksag | fa0f62d | 2016-03-27 22:39:14 +0300 | [diff] [blame] | 273 | def CheckAnyArguments(self): |
| 274 | cur = self.con.cursor() |
| 275 | cur.execute("select spam(?, ?)", (1, 2)) |
| 276 | val = cur.fetchone()[0] |
| 277 | self.assertEqual(val, 2) |
| 278 | |
Sergey Fedoseev | 0830858 | 2018-07-08 12:09:20 +0500 | [diff] [blame] | 279 | def CheckFuncNonDeterministic(self): |
| 280 | mock = unittest.mock.Mock(return_value=None) |
| 281 | self.con.create_function("deterministic", 0, mock, deterministic=False) |
| 282 | self.con.execute("select deterministic() = deterministic()") |
| 283 | self.assertEqual(mock.call_count, 2) |
| 284 | |
| 285 | @unittest.skipIf(sqlite.sqlite_version_info < (3, 8, 3), "deterministic parameter not supported") |
| 286 | def CheckFuncDeterministic(self): |
| 287 | mock = unittest.mock.Mock(return_value=None) |
| 288 | self.con.create_function("deterministic", 0, mock, deterministic=True) |
| 289 | self.con.execute("select deterministic() = deterministic()") |
| 290 | self.assertEqual(mock.call_count, 1) |
| 291 | |
| 292 | @unittest.skipIf(sqlite.sqlite_version_info >= (3, 8, 3), "SQLite < 3.8.3 needed") |
| 293 | def CheckFuncDeterministicNotSupported(self): |
| 294 | with self.assertRaises(sqlite.NotSupportedError): |
| 295 | self.con.create_function("deterministic", 0, int, deterministic=True) |
| 296 | |
| 297 | def CheckFuncDeterministicKeywordOnly(self): |
| 298 | with self.assertRaises(TypeError): |
| 299 | self.con.create_function("deterministic", 0, int, True) |
| 300 | |
Berker Peksag | fa0f62d | 2016-03-27 22:39:14 +0300 | [diff] [blame] | 301 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 302 | class AggregateTests(unittest.TestCase): |
| 303 | def setUp(self): |
| 304 | self.con = sqlite.connect(":memory:") |
| 305 | cur = self.con.cursor() |
| 306 | cur.execute(""" |
| 307 | create table test( |
| 308 | t text, |
| 309 | i integer, |
| 310 | f float, |
| 311 | n, |
| 312 | b blob |
| 313 | ) |
| 314 | """) |
| 315 | cur.execute("insert into test(t, i, f, n, b) values (?, ?, ?, ?, ?)", |
Guido van Rossum | bae07c9 | 2007-10-08 02:46:15 +0000 | [diff] [blame] | 316 | ("foo", 5, 3.14, None, memoryview(b"blob"),)) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 317 | |
| 318 | self.con.create_aggregate("nostep", 1, AggrNoStep) |
| 319 | self.con.create_aggregate("nofinalize", 1, AggrNoFinalize) |
| 320 | self.con.create_aggregate("excInit", 1, AggrExceptionInInit) |
| 321 | self.con.create_aggregate("excStep", 1, AggrExceptionInStep) |
| 322 | self.con.create_aggregate("excFinalize", 1, AggrExceptionInFinalize) |
| 323 | self.con.create_aggregate("checkType", 2, AggrCheckType) |
Berker Peksag | fa0f62d | 2016-03-27 22:39:14 +0300 | [diff] [blame] | 324 | self.con.create_aggregate("checkTypes", -1, AggrCheckTypes) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 325 | self.con.create_aggregate("mysum", 1, AggrSum) |
| 326 | |
| 327 | def tearDown(self): |
| 328 | #self.cur.close() |
| 329 | #self.con.close() |
| 330 | pass |
| 331 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 332 | def CheckAggrErrorOnCreate(self): |
Berker Peksag | 1003b34 | 2016-06-12 22:34:49 +0300 | [diff] [blame] | 333 | with self.assertRaises(sqlite.OperationalError): |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 334 | self.con.create_function("bla", -100, AggrSum) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 335 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 336 | def CheckAggrNoStep(self): |
| 337 | cur = self.con.cursor() |
Berker Peksag | 1003b34 | 2016-06-12 22:34:49 +0300 | [diff] [blame] | 338 | with self.assertRaises(AttributeError) as cm: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 339 | cur.execute("select nostep(t) from test") |
Berker Peksag | 1003b34 | 2016-06-12 22:34:49 +0300 | [diff] [blame] | 340 | self.assertEqual(str(cm.exception), "'AggrNoStep' object has no attribute 'step'") |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 341 | |
| 342 | def CheckAggrNoFinalize(self): |
| 343 | cur = self.con.cursor() |
Berker Peksag | 1003b34 | 2016-06-12 22:34:49 +0300 | [diff] [blame] | 344 | with self.assertRaises(sqlite.OperationalError) as cm: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 345 | cur.execute("select nofinalize(t) from test") |
| 346 | val = cur.fetchone()[0] |
Berker Peksag | 1003b34 | 2016-06-12 22:34:49 +0300 | [diff] [blame] | 347 | self.assertEqual(str(cm.exception), "user-defined aggregate's 'finalize' method raised error") |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 348 | |
| 349 | def CheckAggrExceptionInInit(self): |
| 350 | cur = self.con.cursor() |
Berker Peksag | 1003b34 | 2016-06-12 22:34:49 +0300 | [diff] [blame] | 351 | with self.assertRaises(sqlite.OperationalError) as cm: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 352 | cur.execute("select excInit(t) from test") |
| 353 | val = cur.fetchone()[0] |
Berker Peksag | 1003b34 | 2016-06-12 22:34:49 +0300 | [diff] [blame] | 354 | self.assertEqual(str(cm.exception), "user-defined aggregate's '__init__' method raised error") |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 355 | |
| 356 | def CheckAggrExceptionInStep(self): |
| 357 | cur = self.con.cursor() |
Berker Peksag | 1003b34 | 2016-06-12 22:34:49 +0300 | [diff] [blame] | 358 | with self.assertRaises(sqlite.OperationalError) as cm: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 359 | cur.execute("select excStep(t) from test") |
| 360 | val = cur.fetchone()[0] |
Berker Peksag | 1003b34 | 2016-06-12 22:34:49 +0300 | [diff] [blame] | 361 | self.assertEqual(str(cm.exception), "user-defined aggregate's 'step' method raised error") |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 362 | |
| 363 | def CheckAggrExceptionInFinalize(self): |
| 364 | cur = self.con.cursor() |
Berker Peksag | 1003b34 | 2016-06-12 22:34:49 +0300 | [diff] [blame] | 365 | with self.assertRaises(sqlite.OperationalError) as cm: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 366 | cur.execute("select excFinalize(t) from test") |
| 367 | val = cur.fetchone()[0] |
Berker Peksag | 1003b34 | 2016-06-12 22:34:49 +0300 | [diff] [blame] | 368 | self.assertEqual(str(cm.exception), "user-defined aggregate's 'finalize' method raised error") |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 369 | |
| 370 | def CheckAggrCheckParamStr(self): |
| 371 | cur = self.con.cursor() |
| 372 | cur.execute("select checkType('str', ?)", ("foo",)) |
| 373 | val = cur.fetchone()[0] |
Gregory P. Smith | 04cecaf | 2009-07-04 08:32:15 +0000 | [diff] [blame] | 374 | self.assertEqual(val, 1) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 375 | |
| 376 | def CheckAggrCheckParamInt(self): |
| 377 | cur = self.con.cursor() |
| 378 | cur.execute("select checkType('int', ?)", (42,)) |
| 379 | val = cur.fetchone()[0] |
Gregory P. Smith | 04cecaf | 2009-07-04 08:32:15 +0000 | [diff] [blame] | 380 | self.assertEqual(val, 1) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 381 | |
Berker Peksag | fa0f62d | 2016-03-27 22:39:14 +0300 | [diff] [blame] | 382 | def CheckAggrCheckParamsInt(self): |
| 383 | cur = self.con.cursor() |
| 384 | cur.execute("select checkTypes('int', ?, ?)", (42, 24)) |
| 385 | val = cur.fetchone()[0] |
| 386 | self.assertEqual(val, 2) |
| 387 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 388 | def CheckAggrCheckParamFloat(self): |
| 389 | cur = self.con.cursor() |
| 390 | cur.execute("select checkType('float', ?)", (3.14,)) |
| 391 | val = cur.fetchone()[0] |
Gregory P. Smith | 04cecaf | 2009-07-04 08:32:15 +0000 | [diff] [blame] | 392 | self.assertEqual(val, 1) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 393 | |
| 394 | def CheckAggrCheckParamNone(self): |
| 395 | cur = self.con.cursor() |
| 396 | cur.execute("select checkType('None', ?)", (None,)) |
| 397 | val = cur.fetchone()[0] |
Gregory P. Smith | 04cecaf | 2009-07-04 08:32:15 +0000 | [diff] [blame] | 398 | self.assertEqual(val, 1) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 399 | |
| 400 | def CheckAggrCheckParamBlob(self): |
| 401 | cur = self.con.cursor() |
Guido van Rossum | bae07c9 | 2007-10-08 02:46:15 +0000 | [diff] [blame] | 402 | cur.execute("select checkType('blob', ?)", (memoryview(b"blob"),)) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 403 | val = cur.fetchone()[0] |
Gregory P. Smith | 04cecaf | 2009-07-04 08:32:15 +0000 | [diff] [blame] | 404 | self.assertEqual(val, 1) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 405 | |
| 406 | def CheckAggrCheckAggrSum(self): |
| 407 | cur = self.con.cursor() |
| 408 | cur.execute("delete from test") |
| 409 | cur.executemany("insert into test(i) values (?)", [(10,), (20,), (30,)]) |
| 410 | cur.execute("select mysum(i) from test") |
| 411 | val = cur.fetchone()[0] |
Gregory P. Smith | 04cecaf | 2009-07-04 08:32:15 +0000 | [diff] [blame] | 412 | self.assertEqual(val, 60) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 413 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 414 | class AuthorizerTests(unittest.TestCase): |
Serhiy Storchaka | 3cf96ac | 2013-02-07 17:01:47 +0200 | [diff] [blame] | 415 | @staticmethod |
| 416 | def authorizer_cb(action, arg1, arg2, dbname, source): |
| 417 | if action != sqlite.SQLITE_SELECT: |
| 418 | return sqlite.SQLITE_DENY |
| 419 | if arg2 == 'c2' or arg1 == 't2': |
| 420 | return sqlite.SQLITE_DENY |
| 421 | return sqlite.SQLITE_OK |
| 422 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 423 | def setUp(self): |
| 424 | self.con = sqlite.connect(":memory:") |
| 425 | self.con.executescript(""" |
| 426 | create table t1 (c1, c2); |
| 427 | create table t2 (c1, c2); |
| 428 | insert into t1 (c1, c2) values (1, 2); |
| 429 | insert into t2 (c1, c2) values (4, 5); |
| 430 | """) |
| 431 | |
| 432 | # For our security test: |
| 433 | self.con.execute("select c2 from t2") |
| 434 | |
Serhiy Storchaka | 3cf96ac | 2013-02-07 17:01:47 +0200 | [diff] [blame] | 435 | self.con.set_authorizer(self.authorizer_cb) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 436 | |
| 437 | def tearDown(self): |
| 438 | pass |
| 439 | |
Serhiy Storchaka | 3cf96ac | 2013-02-07 17:01:47 +0200 | [diff] [blame] | 440 | def test_table_access(self): |
Berker Peksag | 1003b34 | 2016-06-12 22:34:49 +0300 | [diff] [blame] | 441 | with self.assertRaises(sqlite.DatabaseError) as cm: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 442 | self.con.execute("select * from t2") |
Berker Peksag | 1003b34 | 2016-06-12 22:34:49 +0300 | [diff] [blame] | 443 | self.assertIn('prohibited', str(cm.exception)) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 444 | |
Serhiy Storchaka | 3cf96ac | 2013-02-07 17:01:47 +0200 | [diff] [blame] | 445 | def test_column_access(self): |
Berker Peksag | 1003b34 | 2016-06-12 22:34:49 +0300 | [diff] [blame] | 446 | with self.assertRaises(sqlite.DatabaseError) as cm: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 447 | self.con.execute("select c2 from t1") |
Berker Peksag | 1003b34 | 2016-06-12 22:34:49 +0300 | [diff] [blame] | 448 | self.assertIn('prohibited', str(cm.exception)) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 449 | |
Serhiy Storchaka | 3cf96ac | 2013-02-07 17:01:47 +0200 | [diff] [blame] | 450 | class AuthorizerRaiseExceptionTests(AuthorizerTests): |
| 451 | @staticmethod |
| 452 | def authorizer_cb(action, arg1, arg2, dbname, source): |
| 453 | if action != sqlite.SQLITE_SELECT: |
| 454 | raise ValueError |
| 455 | if arg2 == 'c2' or arg1 == 't2': |
| 456 | raise ValueError |
| 457 | return sqlite.SQLITE_OK |
| 458 | |
| 459 | class AuthorizerIllegalTypeTests(AuthorizerTests): |
| 460 | @staticmethod |
| 461 | def authorizer_cb(action, arg1, arg2, dbname, source): |
| 462 | if action != sqlite.SQLITE_SELECT: |
| 463 | return 0.0 |
| 464 | if arg2 == 'c2' or arg1 == 't2': |
| 465 | return 0.0 |
| 466 | return sqlite.SQLITE_OK |
| 467 | |
| 468 | class AuthorizerLargeIntegerTests(AuthorizerTests): |
| 469 | @staticmethod |
| 470 | def authorizer_cb(action, arg1, arg2, dbname, source): |
| 471 | if action != sqlite.SQLITE_SELECT: |
| 472 | return 2**32 |
| 473 | if arg2 == 'c2' or arg1 == 't2': |
| 474 | return 2**32 |
| 475 | return sqlite.SQLITE_OK |
| 476 | |
| 477 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 478 | def suite(): |
| 479 | function_suite = unittest.makeSuite(FunctionTests, "Check") |
| 480 | aggregate_suite = unittest.makeSuite(AggregateTests, "Check") |
Serhiy Storchaka | 3cf96ac | 2013-02-07 17:01:47 +0200 | [diff] [blame] | 481 | authorizer_suite = unittest.makeSuite(AuthorizerTests) |
| 482 | return unittest.TestSuite(( |
| 483 | function_suite, |
| 484 | aggregate_suite, |
| 485 | authorizer_suite, |
| 486 | unittest.makeSuite(AuthorizerRaiseExceptionTests), |
| 487 | unittest.makeSuite(AuthorizerIllegalTypeTests), |
| 488 | unittest.makeSuite(AuthorizerLargeIntegerTests), |
| 489 | )) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 490 | |
| 491 | def test(): |
| 492 | runner = unittest.TextTestRunner() |
| 493 | runner.run(suite()) |
| 494 | |
| 495 | if __name__ == "__main__": |
| 496 | test() |