blob: 9501f535c499996ee62d2294794e923ac2f66f27 [file] [log] [blame]
Petri Lehtinenf8547992012-02-02 17:17:36 +02001#-*- coding: iso-8859-1 -*-
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002# pysqlite2/test/userfunctions.py: tests for user-defined functions and
3# aggregates.
4#
Martin v. Löwis03117362008-03-30 20:21:00 +00005# Copyright (C) 2005-2007 Gerhard Häring <gh@ghaering.de>
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006#
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
25import unittest
Sergey Fedoseev08308582018-07-08 12:09:20 +050026import unittest.mock
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000027import sqlite3 as sqlite
28
29def func_returntext():
30 return "foo"
31def func_returnunicode():
Guido van Rossumef87d6e2007-05-02 19:09:54 +000032 return "bar"
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000033def func_returnint():
34 return 42
35def func_returnfloat():
36 return 3.14
37def func_returnnull():
38 return None
39def func_returnblob():
Guido van Rossumbae07c92007-10-08 02:46:15 +000040 return b"blob"
Petri Lehtinen4fe85ab2012-02-19 21:38:00 +020041def func_returnlonglong():
42 return 1<<31
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000043def func_raiseexception():
44 5/0
45
46def func_isstring(v):
Guido van Rossumef87d6e2007-05-02 19:09:54 +000047 return type(v) is str
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000048def func_isint(v):
49 return type(v) is int
50def func_isfloat(v):
51 return type(v) is float
52def func_isnone(v):
53 return type(v) is type(None)
54def func_isblob(v):
Guido van Rossumbae07c92007-10-08 02:46:15 +000055 return isinstance(v, (bytes, memoryview))
Petri Lehtinen4fe85ab2012-02-19 21:38:00 +020056def func_islonglong(v):
57 return isinstance(v, int) and v >= 1<<31
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000058
Berker Peksagfa0f62d2016-03-27 22:39:14 +030059def func(*args):
60 return len(args)
61
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000062class AggrNoStep:
63 def __init__(self):
64 pass
65
Thomas Wouters0e3f5912006-08-11 14:57:12 +000066 def finalize(self):
67 return 1
68
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000069class AggrNoFinalize:
70 def __init__(self):
71 pass
72
73 def step(self, x):
74 pass
75
76class AggrExceptionInInit:
77 def __init__(self):
78 5/0
79
80 def step(self, x):
81 pass
82
83 def finalize(self):
84 pass
85
86class 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
96class AggrExceptionInFinalize:
97 def __init__(self):
98 pass
99
100 def step(self, x):
101 pass
102
103 def finalize(self):
104 5/0
105
106class AggrCheckType:
107 def __init__(self):
108 self.val = None
109
110 def step(self, whichType, val):
Guido van Rossumbae07c92007-10-08 02:46:15 +0000111 theType = {"str": str, "int": int, "float": float, "None": type(None),
112 "blob": bytes}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000113 self.val = int(theType[whichType] is type(val))
114
115 def finalize(self):
116 return self.val
117
Berker Peksagfa0f62d2016-03-27 22:39:14 +0300118class 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 Wouters49fd7fa2006-04-21 10:40:58 +0000131class 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
141class 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 Lehtinen4fe85ab2012-02-19 21:38:00 +0200151 self.con.create_function("returnlonglong", 0, func_returnlonglong)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000152 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 Lehtinen4fe85ab2012-02-19 21:38:00 +0200159 self.con.create_function("islonglong", 1, func_islonglong)
Berker Peksagfa0f62d2016-03-27 22:39:14 +0300160 self.con.create_function("spam", -1, func)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000161
162 def tearDown(self):
163 self.con.close()
164
Thomas Wouters477c8d52006-05-27 19:21:47 +0000165 def CheckFuncErrorOnCreate(self):
Berker Peksag1003b342016-06-12 22:34:49 +0300166 with self.assertRaises(sqlite.OperationalError):
Thomas Wouters477c8d52006-05-27 19:21:47 +0000167 self.con.create_function("bla", -100, lambda x: 2*x)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000168
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000169 def CheckFuncRefCount(self):
170 def getfunc():
171 def f():
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000172 return 1
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000173 return f
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000174 f = getfunc()
175 globals()["foo"] = f
176 # self.con.create_function("reftest", 0, getfunc())
177 self.con.create_function("reftest", 0, f)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000178 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. Smith04cecaf2009-07-04 08:32:15 +0000185 self.assertEqual(type(val), str)
186 self.assertEqual(val, "foo")
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000187
188 def CheckFuncReturnUnicode(self):
189 cur = self.con.cursor()
190 cur.execute("select returnunicode()")
191 val = cur.fetchone()[0]
Gregory P. Smith04cecaf2009-07-04 08:32:15 +0000192 self.assertEqual(type(val), str)
193 self.assertEqual(val, "bar")
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000194
195 def CheckFuncReturnInt(self):
196 cur = self.con.cursor()
197 cur.execute("select returnint()")
198 val = cur.fetchone()[0]
Gregory P. Smith04cecaf2009-07-04 08:32:15 +0000199 self.assertEqual(type(val), int)
200 self.assertEqual(val, 42)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000201
202 def CheckFuncReturnFloat(self):
203 cur = self.con.cursor()
204 cur.execute("select returnfloat()")
205 val = cur.fetchone()[0]
Gregory P. Smith04cecaf2009-07-04 08:32:15 +0000206 self.assertEqual(type(val), float)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000207 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. Smith04cecaf2009-07-04 08:32:15 +0000214 self.assertEqual(type(val), type(None))
215 self.assertEqual(val, None)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000216
217 def CheckFuncReturnBlob(self):
218 cur = self.con.cursor()
219 cur.execute("select returnblob()")
220 val = cur.fetchone()[0]
Gregory P. Smith04cecaf2009-07-04 08:32:15 +0000221 self.assertEqual(type(val), bytes)
222 self.assertEqual(val, b"blob")
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000223
Petri Lehtinen4fe85ab2012-02-19 21:38:00 +0200224 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 Wouters49fd7fa2006-04-21 10:40:58 +0000230 def CheckFuncException(self):
231 cur = self.con.cursor()
Berker Peksag1003b342016-06-12 22:34:49 +0300232 with self.assertRaises(sqlite.OperationalError) as cm:
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000233 cur.execute("select raiseexception()")
234 cur.fetchone()
Berker Peksag1003b342016-06-12 22:34:49 +0300235 self.assertEqual(str(cm.exception), 'user-defined function raised exception')
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000236
237 def CheckParamString(self):
238 cur = self.con.cursor()
239 cur.execute("select isstring(?)", ("foo",))
240 val = cur.fetchone()[0]
Gregory P. Smith04cecaf2009-07-04 08:32:15 +0000241 self.assertEqual(val, 1)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000242
243 def CheckParamInt(self):
244 cur = self.con.cursor()
245 cur.execute("select isint(?)", (42,))
246 val = cur.fetchone()[0]
Gregory P. Smith04cecaf2009-07-04 08:32:15 +0000247 self.assertEqual(val, 1)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000248
249 def CheckParamFloat(self):
250 cur = self.con.cursor()
251 cur.execute("select isfloat(?)", (3.14,))
252 val = cur.fetchone()[0]
Gregory P. Smith04cecaf2009-07-04 08:32:15 +0000253 self.assertEqual(val, 1)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000254
255 def CheckParamNone(self):
256 cur = self.con.cursor()
257 cur.execute("select isnone(?)", (None,))
258 val = cur.fetchone()[0]
Gregory P. Smith04cecaf2009-07-04 08:32:15 +0000259 self.assertEqual(val, 1)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000260
261 def CheckParamBlob(self):
262 cur = self.con.cursor()
Guido van Rossumbae07c92007-10-08 02:46:15 +0000263 cur.execute("select isblob(?)", (memoryview(b"blob"),))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000264 val = cur.fetchone()[0]
Gregory P. Smith04cecaf2009-07-04 08:32:15 +0000265 self.assertEqual(val, 1)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000266
Petri Lehtinen4fe85ab2012-02-19 21:38:00 +0200267 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 Peksagfa0f62d2016-03-27 22:39:14 +0300273 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 Fedoseev08308582018-07-08 12:09:20 +0500279 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 Peksagfa0f62d2016-03-27 22:39:14 +0300301
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000302class 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 Rossumbae07c92007-10-08 02:46:15 +0000316 ("foo", 5, 3.14, None, memoryview(b"blob"),))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000317
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 Peksagfa0f62d2016-03-27 22:39:14 +0300324 self.con.create_aggregate("checkTypes", -1, AggrCheckTypes)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000325 self.con.create_aggregate("mysum", 1, AggrSum)
326
327 def tearDown(self):
328 #self.cur.close()
329 #self.con.close()
330 pass
331
Thomas Wouters477c8d52006-05-27 19:21:47 +0000332 def CheckAggrErrorOnCreate(self):
Berker Peksag1003b342016-06-12 22:34:49 +0300333 with self.assertRaises(sqlite.OperationalError):
Thomas Wouters477c8d52006-05-27 19:21:47 +0000334 self.con.create_function("bla", -100, AggrSum)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000335
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000336 def CheckAggrNoStep(self):
337 cur = self.con.cursor()
Berker Peksag1003b342016-06-12 22:34:49 +0300338 with self.assertRaises(AttributeError) as cm:
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000339 cur.execute("select nostep(t) from test")
Berker Peksag1003b342016-06-12 22:34:49 +0300340 self.assertEqual(str(cm.exception), "'AggrNoStep' object has no attribute 'step'")
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000341
342 def CheckAggrNoFinalize(self):
343 cur = self.con.cursor()
Berker Peksag1003b342016-06-12 22:34:49 +0300344 with self.assertRaises(sqlite.OperationalError) as cm:
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000345 cur.execute("select nofinalize(t) from test")
346 val = cur.fetchone()[0]
Berker Peksag1003b342016-06-12 22:34:49 +0300347 self.assertEqual(str(cm.exception), "user-defined aggregate's 'finalize' method raised error")
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000348
349 def CheckAggrExceptionInInit(self):
350 cur = self.con.cursor()
Berker Peksag1003b342016-06-12 22:34:49 +0300351 with self.assertRaises(sqlite.OperationalError) as cm:
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000352 cur.execute("select excInit(t) from test")
353 val = cur.fetchone()[0]
Berker Peksag1003b342016-06-12 22:34:49 +0300354 self.assertEqual(str(cm.exception), "user-defined aggregate's '__init__' method raised error")
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000355
356 def CheckAggrExceptionInStep(self):
357 cur = self.con.cursor()
Berker Peksag1003b342016-06-12 22:34:49 +0300358 with self.assertRaises(sqlite.OperationalError) as cm:
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000359 cur.execute("select excStep(t) from test")
360 val = cur.fetchone()[0]
Berker Peksag1003b342016-06-12 22:34:49 +0300361 self.assertEqual(str(cm.exception), "user-defined aggregate's 'step' method raised error")
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000362
363 def CheckAggrExceptionInFinalize(self):
364 cur = self.con.cursor()
Berker Peksag1003b342016-06-12 22:34:49 +0300365 with self.assertRaises(sqlite.OperationalError) as cm:
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000366 cur.execute("select excFinalize(t) from test")
367 val = cur.fetchone()[0]
Berker Peksag1003b342016-06-12 22:34:49 +0300368 self.assertEqual(str(cm.exception), "user-defined aggregate's 'finalize' method raised error")
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000369
370 def CheckAggrCheckParamStr(self):
371 cur = self.con.cursor()
372 cur.execute("select checkType('str', ?)", ("foo",))
373 val = cur.fetchone()[0]
Gregory P. Smith04cecaf2009-07-04 08:32:15 +0000374 self.assertEqual(val, 1)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000375
376 def CheckAggrCheckParamInt(self):
377 cur = self.con.cursor()
378 cur.execute("select checkType('int', ?)", (42,))
379 val = cur.fetchone()[0]
Gregory P. Smith04cecaf2009-07-04 08:32:15 +0000380 self.assertEqual(val, 1)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000381
Berker Peksagfa0f62d2016-03-27 22:39:14 +0300382 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 Wouters49fd7fa2006-04-21 10:40:58 +0000388 def CheckAggrCheckParamFloat(self):
389 cur = self.con.cursor()
390 cur.execute("select checkType('float', ?)", (3.14,))
391 val = cur.fetchone()[0]
Gregory P. Smith04cecaf2009-07-04 08:32:15 +0000392 self.assertEqual(val, 1)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000393
394 def CheckAggrCheckParamNone(self):
395 cur = self.con.cursor()
396 cur.execute("select checkType('None', ?)", (None,))
397 val = cur.fetchone()[0]
Gregory P. Smith04cecaf2009-07-04 08:32:15 +0000398 self.assertEqual(val, 1)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000399
400 def CheckAggrCheckParamBlob(self):
401 cur = self.con.cursor()
Guido van Rossumbae07c92007-10-08 02:46:15 +0000402 cur.execute("select checkType('blob', ?)", (memoryview(b"blob"),))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000403 val = cur.fetchone()[0]
Gregory P. Smith04cecaf2009-07-04 08:32:15 +0000404 self.assertEqual(val, 1)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000405
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. Smith04cecaf2009-07-04 08:32:15 +0000412 self.assertEqual(val, 60)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000413
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000414class AuthorizerTests(unittest.TestCase):
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200415 @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 Wouters0e3f5912006-08-11 14:57:12 +0000423 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 Storchaka3cf96ac2013-02-07 17:01:47 +0200435 self.con.set_authorizer(self.authorizer_cb)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000436
437 def tearDown(self):
438 pass
439
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200440 def test_table_access(self):
Berker Peksag1003b342016-06-12 22:34:49 +0300441 with self.assertRaises(sqlite.DatabaseError) as cm:
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000442 self.con.execute("select * from t2")
Berker Peksag1003b342016-06-12 22:34:49 +0300443 self.assertIn('prohibited', str(cm.exception))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000444
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200445 def test_column_access(self):
Berker Peksag1003b342016-06-12 22:34:49 +0300446 with self.assertRaises(sqlite.DatabaseError) as cm:
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000447 self.con.execute("select c2 from t1")
Berker Peksag1003b342016-06-12 22:34:49 +0300448 self.assertIn('prohibited', str(cm.exception))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000449
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200450class 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
459class 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
468class 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 Wouters49fd7fa2006-04-21 10:40:58 +0000478def suite():
479 function_suite = unittest.makeSuite(FunctionTests, "Check")
480 aggregate_suite = unittest.makeSuite(AggregateTests, "Check")
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200481 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 Wouters49fd7fa2006-04-21 10:40:58 +0000490
491def test():
492 runner = unittest.TextTestRunner()
493 runner.run(suite())
494
495if __name__ == "__main__":
496 test()