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