PageRenderTime 51ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/lib-python/modified-2.7/sqlite3/test/userfunctions.py

https://bitbucket.org/dac_io/pypy
Python | 415 lines | 350 code | 41 blank | 24 comment | 6 complexity | 9c2674914450899b88af068c3cedda3b MD5 | raw file
  1. #-*- coding: ISO-8859-1 -*-
  2. # pysqlite2/test/userfunctions.py: tests for user-defined functions and
  3. # aggregates.
  4. #
  5. # Copyright (C) 2005-2007 Gerhard Häring <gh@ghaering.de>
  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. import unittest
  25. import sqlite3 as sqlite
  26. def func_returntext():
  27. return "foo"
  28. def func_returnunicode():
  29. return u"bar"
  30. def func_returnint():
  31. return 42
  32. def func_returnfloat():
  33. return 3.14
  34. def func_returnnull():
  35. return None
  36. def func_returnblob():
  37. return buffer("blob")
  38. def func_raiseexception():
  39. 5 // 0
  40. def func_isstring(v):
  41. return type(v) is unicode
  42. def func_isint(v):
  43. return type(v) is int
  44. def func_isfloat(v):
  45. return type(v) is float
  46. def func_isnone(v):
  47. return type(v) is type(None)
  48. def func_isblob(v):
  49. return type(v) is buffer
  50. class AggrNoStep:
  51. def __init__(self):
  52. pass
  53. def finalize(self):
  54. return 1
  55. class AggrNoFinalize:
  56. def __init__(self):
  57. pass
  58. def step(self, x):
  59. pass
  60. class AggrExceptionInInit:
  61. def __init__(self):
  62. 5 // 0
  63. def step(self, x):
  64. pass
  65. def finalize(self):
  66. pass
  67. class AggrExceptionInStep:
  68. def __init__(self):
  69. pass
  70. def step(self, x):
  71. 5 // 0
  72. def finalize(self):
  73. return 42
  74. class AggrExceptionInFinalize:
  75. def __init__(self):
  76. pass
  77. def step(self, x):
  78. pass
  79. def finalize(self):
  80. 5 // 0
  81. class AggrCheckType:
  82. def __init__(self):
  83. self.val = None
  84. def step(self, whichType, val):
  85. theType = {"str": unicode, "int": int, "float": float, "None": type(None), "blob": buffer}
  86. self.val = int(theType[whichType] is type(val))
  87. def finalize(self):
  88. return self.val
  89. class AggrSum:
  90. def __init__(self):
  91. self.val = 0.0
  92. def step(self, val):
  93. self.val += val
  94. def finalize(self):
  95. return self.val
  96. class FunctionTests(unittest.TestCase):
  97. def setUp(self):
  98. self.con = sqlite.connect(":memory:")
  99. self.con.create_function("returntext", 0, func_returntext)
  100. self.con.create_function("returnunicode", 0, func_returnunicode)
  101. self.con.create_function("returnint", 0, func_returnint)
  102. self.con.create_function("returnfloat", 0, func_returnfloat)
  103. self.con.create_function("returnnull", 0, func_returnnull)
  104. self.con.create_function("returnblob", 0, func_returnblob)
  105. self.con.create_function("raiseexception", 0, func_raiseexception)
  106. self.con.create_function("isstring", 1, func_isstring)
  107. self.con.create_function("isint", 1, func_isint)
  108. self.con.create_function("isfloat", 1, func_isfloat)
  109. self.con.create_function("isnone", 1, func_isnone)
  110. self.con.create_function("isblob", 1, func_isblob)
  111. def tearDown(self):
  112. self.con.close()
  113. def CheckFuncErrorOnCreate(self):
  114. try:
  115. self.con.create_function("bla", -100, lambda x: 2*x)
  116. self.fail("should have raised an OperationalError")
  117. except sqlite.OperationalError:
  118. pass
  119. def CheckFuncRefCount(self):
  120. def getfunc():
  121. def f():
  122. return 1
  123. return f
  124. f = getfunc()
  125. globals()["foo"] = f
  126. # self.con.create_function("reftest", 0, getfunc())
  127. self.con.create_function("reftest", 0, f)
  128. cur = self.con.cursor()
  129. cur.execute("select reftest()")
  130. def CheckFuncReturnText(self):
  131. cur = self.con.cursor()
  132. cur.execute("select returntext()")
  133. val = cur.fetchone()[0]
  134. self.assertEqual(type(val), unicode)
  135. self.assertEqual(val, "foo")
  136. def CheckFuncReturnUnicode(self):
  137. cur = self.con.cursor()
  138. cur.execute("select returnunicode()")
  139. val = cur.fetchone()[0]
  140. self.assertEqual(type(val), unicode)
  141. self.assertEqual(val, u"bar")
  142. def CheckFuncReturnInt(self):
  143. cur = self.con.cursor()
  144. cur.execute("select returnint()")
  145. val = cur.fetchone()[0]
  146. self.assertEqual(type(val), int)
  147. self.assertEqual(val, 42)
  148. def CheckFuncReturnFloat(self):
  149. cur = self.con.cursor()
  150. cur.execute("select returnfloat()")
  151. val = cur.fetchone()[0]
  152. self.assertEqual(type(val), float)
  153. if val < 3.139 or val > 3.141:
  154. self.fail("wrong value")
  155. def CheckFuncReturnNull(self):
  156. cur = self.con.cursor()
  157. cur.execute("select returnnull()")
  158. val = cur.fetchone()[0]
  159. self.assertEqual(type(val), type(None))
  160. self.assertEqual(val, None)
  161. def CheckFuncReturnBlob(self):
  162. cur = self.con.cursor()
  163. cur.execute("select returnblob()")
  164. val = cur.fetchone()[0]
  165. self.assertEqual(type(val), buffer)
  166. self.assertEqual(val, buffer("blob"))
  167. def CheckFuncException(self):
  168. cur = self.con.cursor()
  169. try:
  170. cur.execute("select raiseexception()")
  171. cur.fetchone()
  172. self.fail("should have raised OperationalError")
  173. except sqlite.OperationalError, e:
  174. self.assertEqual(e.args[0], 'user-defined function raised exception')
  175. def CheckParamString(self):
  176. cur = self.con.cursor()
  177. cur.execute("select isstring(?)", ("foo",))
  178. val = cur.fetchone()[0]
  179. self.assertEqual(val, 1)
  180. def CheckParamInt(self):
  181. cur = self.con.cursor()
  182. cur.execute("select isint(?)", (42,))
  183. val = cur.fetchone()[0]
  184. self.assertEqual(val, 1)
  185. def CheckParamFloat(self):
  186. cur = self.con.cursor()
  187. cur.execute("select isfloat(?)", (3.14,))
  188. val = cur.fetchone()[0]
  189. self.assertEqual(val, 1)
  190. def CheckParamNone(self):
  191. cur = self.con.cursor()
  192. cur.execute("select isnone(?)", (None,))
  193. val = cur.fetchone()[0]
  194. self.assertEqual(val, 1)
  195. def CheckParamBlob(self):
  196. cur = self.con.cursor()
  197. cur.execute("select isblob(?)", (buffer("blob"),))
  198. val = cur.fetchone()[0]
  199. self.assertEqual(val, 1)
  200. class AggregateTests(unittest.TestCase):
  201. def setUp(self):
  202. self.con = sqlite.connect(":memory:")
  203. cur = self.con.cursor()
  204. cur.execute("""
  205. create table test(
  206. t text,
  207. i integer,
  208. f float,
  209. n,
  210. b blob
  211. )
  212. """)
  213. cur.execute("insert into test(t, i, f, n, b) values (?, ?, ?, ?, ?)",
  214. ("foo", 5, 3.14, None, buffer("blob"),))
  215. self.con.create_aggregate("nostep", 1, AggrNoStep)
  216. self.con.create_aggregate("nofinalize", 1, AggrNoFinalize)
  217. self.con.create_aggregate("excInit", 1, AggrExceptionInInit)
  218. self.con.create_aggregate("excStep", 1, AggrExceptionInStep)
  219. self.con.create_aggregate("excFinalize", 1, AggrExceptionInFinalize)
  220. self.con.create_aggregate("checkType", 2, AggrCheckType)
  221. self.con.create_aggregate("mysum", 1, AggrSum)
  222. def tearDown(self):
  223. #self.cur.close()
  224. #self.con.close()
  225. pass
  226. def CheckAggrErrorOnCreate(self):
  227. try:
  228. self.con.create_function("bla", -100, AggrSum)
  229. self.fail("should have raised an OperationalError")
  230. except sqlite.OperationalError:
  231. pass
  232. def CheckAggrNoStep(self):
  233. # XXX it's better to raise OperationalError in order to stop
  234. # the query earlier.
  235. cur = self.con.cursor()
  236. try:
  237. cur.execute("select nostep(t) from test")
  238. self.fail("should have raised an OperationalError")
  239. except sqlite.OperationalError, e:
  240. self.assertEqual(e.args[0], "user-defined aggregate's 'step' method raised error")
  241. def CheckAggrNoFinalize(self):
  242. cur = self.con.cursor()
  243. try:
  244. cur.execute("select nofinalize(t) from test")
  245. val = cur.fetchone()[0]
  246. self.fail("should have raised an OperationalError")
  247. except sqlite.OperationalError, e:
  248. self.assertEqual(e.args[0], "user-defined aggregate's 'finalize' method raised error")
  249. def CheckAggrExceptionInInit(self):
  250. cur = self.con.cursor()
  251. try:
  252. cur.execute("select excInit(t) from test")
  253. val = cur.fetchone()[0]
  254. self.fail("should have raised an OperationalError")
  255. except sqlite.OperationalError, e:
  256. self.assertEqual(e.args[0], "user-defined aggregate's '__init__' method raised error")
  257. def CheckAggrExceptionInStep(self):
  258. cur = self.con.cursor()
  259. try:
  260. cur.execute("select excStep(t) from test")
  261. val = cur.fetchone()[0]
  262. self.fail("should have raised an OperationalError")
  263. except sqlite.OperationalError, e:
  264. self.assertEqual(e.args[0], "user-defined aggregate's 'step' method raised error")
  265. def CheckAggrExceptionInFinalize(self):
  266. cur = self.con.cursor()
  267. try:
  268. cur.execute("select excFinalize(t) from test")
  269. val = cur.fetchone()[0]
  270. self.fail("should have raised an OperationalError")
  271. except sqlite.OperationalError, e:
  272. self.assertEqual(e.args[0], "user-defined aggregate's 'finalize' method raised error")
  273. def CheckAggrCheckParamStr(self):
  274. cur = self.con.cursor()
  275. cur.execute("select checkType('str', ?)", ("foo",))
  276. val = cur.fetchone()[0]
  277. self.assertEqual(val, 1)
  278. def CheckAggrCheckParamInt(self):
  279. cur = self.con.cursor()
  280. cur.execute("select checkType('int', ?)", (42,))
  281. val = cur.fetchone()[0]
  282. self.assertEqual(val, 1)
  283. def CheckAggrCheckParamFloat(self):
  284. cur = self.con.cursor()
  285. cur.execute("select checkType('float', ?)", (3.14,))
  286. val = cur.fetchone()[0]
  287. self.assertEqual(val, 1)
  288. def CheckAggrCheckParamNone(self):
  289. cur = self.con.cursor()
  290. cur.execute("select checkType('None', ?)", (None,))
  291. val = cur.fetchone()[0]
  292. self.assertEqual(val, 1)
  293. def CheckAggrCheckParamBlob(self):
  294. cur = self.con.cursor()
  295. cur.execute("select checkType('blob', ?)", (buffer("blob"),))
  296. val = cur.fetchone()[0]
  297. self.assertEqual(val, 1)
  298. def CheckAggrCheckAggrSum(self):
  299. cur = self.con.cursor()
  300. cur.execute("delete from test")
  301. cur.executemany("insert into test(i) values (?)", [(10,), (20,), (30,)])
  302. cur.execute("select mysum(i) from test")
  303. val = cur.fetchone()[0]
  304. self.assertEqual(val, 60)
  305. def authorizer_cb(action, arg1, arg2, dbname, source):
  306. if action != sqlite.SQLITE_SELECT:
  307. return sqlite.SQLITE_DENY
  308. if arg2 == 'c2' or arg1 == 't2':
  309. return sqlite.SQLITE_DENY
  310. return sqlite.SQLITE_OK
  311. class AuthorizerTests(unittest.TestCase):
  312. def setUp(self):
  313. self.con = sqlite.connect(":memory:")
  314. self.con.executescript("""
  315. create table t1 (c1, c2);
  316. create table t2 (c1, c2);
  317. insert into t1 (c1, c2) values (1, 2);
  318. insert into t2 (c1, c2) values (4, 5);
  319. """)
  320. # For our security test:
  321. self.con.execute("select c2 from t2")
  322. self.con.set_authorizer(authorizer_cb)
  323. def tearDown(self):
  324. pass
  325. def CheckTableAccess(self):
  326. try:
  327. self.con.execute("select * from t2")
  328. except sqlite.DatabaseError, e:
  329. if not e.args[0].endswith("prohibited"):
  330. self.fail("wrong exception text: %s" % e.args[0])
  331. return
  332. self.fail("should have raised an exception due to missing privileges")
  333. def CheckColumnAccess(self):
  334. try:
  335. self.con.execute("select c2 from t1")
  336. except sqlite.DatabaseError, e:
  337. if not e.args[0].endswith("prohibited"):
  338. self.fail("wrong exception text: %s" % e.args[0])
  339. return
  340. self.fail("should have raised an exception due to missing privileges")
  341. def suite():
  342. function_suite = unittest.makeSuite(FunctionTests, "Check")
  343. aggregate_suite = unittest.makeSuite(AggregateTests, "Check")
  344. authorizer_suite = unittest.makeSuite(AuthorizerTests, "Check")
  345. return unittest.TestSuite((function_suite, aggregate_suite, authorizer_suite))
  346. def test():
  347. runner = unittest.TextTestRunner()
  348. runner.run(suite())
  349. if __name__ == "__main__":
  350. test()