Changeset 391 for python/trunk/Lib/sqlite3/test/userfunctions.py
- Timestamp:
- Mar 19, 2014, 11:31:01 PM (11 years ago)
- Location:
- python/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
python/trunk
-
Property svn:mergeinfo
set to
/python/vendor/Python-2.7.6 merged eligible /python/vendor/current merged eligible
-
Property svn:mergeinfo
set to
-
python/trunk/Lib/sqlite3/test/userfunctions.py
r2 r391 38 38 def func_returnblob(): 39 39 return buffer("blob") 40 def func_returnlonglong(): 41 return 1<<31 40 42 def func_raiseexception(): 41 5 /043 5 // 0 42 44 43 45 def func_isstring(v): … … 51 53 def func_isblob(v): 52 54 return type(v) is buffer 55 def func_islonglong(v): 56 return isinstance(v, (int, long)) and v >= 1<<31 53 57 54 58 class AggrNoStep: … … 68 72 class AggrExceptionInInit: 69 73 def __init__(self): 70 5 /074 5 // 0 71 75 72 76 def step(self, x): … … 81 85 82 86 def step(self, x): 83 5 /087 5 // 0 84 88 85 89 def finalize(self): … … 94 98 95 99 def finalize(self): 96 5 /0100 5 // 0 97 101 98 102 class AggrCheckType: … … 127 131 self.con.create_function("returnnull", 0, func_returnnull) 128 132 self.con.create_function("returnblob", 0, func_returnblob) 133 self.con.create_function("returnlonglong", 0, func_returnlonglong) 129 134 self.con.create_function("raiseexception", 0, func_raiseexception) 130 135 … … 134 139 self.con.create_function("isnone", 1, func_isnone) 135 140 self.con.create_function("isblob", 1, func_isblob) 141 self.con.create_function("islonglong", 1, func_islonglong) 136 142 137 143 def tearDown(self): … … 161 167 cur.execute("select returntext()") 162 168 val = cur.fetchone()[0] 163 self. failUnlessEqual(type(val), unicode)164 self. failUnlessEqual(val, "foo")169 self.assertEqual(type(val), unicode) 170 self.assertEqual(val, "foo") 165 171 166 172 def CheckFuncReturnUnicode(self): … … 168 174 cur.execute("select returnunicode()") 169 175 val = cur.fetchone()[0] 170 self. failUnlessEqual(type(val), unicode)171 self. failUnlessEqual(val, u"bar")176 self.assertEqual(type(val), unicode) 177 self.assertEqual(val, u"bar") 172 178 173 179 def CheckFuncReturnInt(self): … … 175 181 cur.execute("select returnint()") 176 182 val = cur.fetchone()[0] 177 self. failUnlessEqual(type(val), int)178 self. failUnlessEqual(val, 42)183 self.assertEqual(type(val), int) 184 self.assertEqual(val, 42) 179 185 180 186 def CheckFuncReturnFloat(self): … … 182 188 cur.execute("select returnfloat()") 183 189 val = cur.fetchone()[0] 184 self. failUnlessEqual(type(val), float)190 self.assertEqual(type(val), float) 185 191 if val < 3.139 or val > 3.141: 186 192 self.fail("wrong value") … … 190 196 cur.execute("select returnnull()") 191 197 val = cur.fetchone()[0] 192 self. failUnlessEqual(type(val), type(None))193 self. failUnlessEqual(val, None)198 self.assertEqual(type(val), type(None)) 199 self.assertEqual(val, None) 194 200 195 201 def CheckFuncReturnBlob(self): … … 197 203 cur.execute("select returnblob()") 198 204 val = cur.fetchone()[0] 199 self.failUnlessEqual(type(val), buffer) 200 self.failUnlessEqual(val, buffer("blob")) 205 self.assertEqual(type(val), buffer) 206 self.assertEqual(val, buffer("blob")) 207 208 def CheckFuncReturnLongLong(self): 209 cur = self.con.cursor() 210 cur.execute("select returnlonglong()") 211 val = cur.fetchone()[0] 212 self.assertEqual(val, 1<<31) 201 213 202 214 def CheckFuncException(self): … … 207 219 self.fail("should have raised OperationalError") 208 220 except sqlite.OperationalError, e: 209 self. failUnlessEqual(e.args[0], 'user-defined function raised exception')221 self.assertEqual(e.args[0], 'user-defined function raised exception') 210 222 211 223 def CheckParamString(self): … … 213 225 cur.execute("select isstring(?)", ("foo",)) 214 226 val = cur.fetchone()[0] 215 self. failUnlessEqual(val, 1)227 self.assertEqual(val, 1) 216 228 217 229 def CheckParamInt(self): … … 219 231 cur.execute("select isint(?)", (42,)) 220 232 val = cur.fetchone()[0] 221 self. failUnlessEqual(val, 1)233 self.assertEqual(val, 1) 222 234 223 235 def CheckParamFloat(self): … … 225 237 cur.execute("select isfloat(?)", (3.14,)) 226 238 val = cur.fetchone()[0] 227 self. failUnlessEqual(val, 1)239 self.assertEqual(val, 1) 228 240 229 241 def CheckParamNone(self): … … 231 243 cur.execute("select isnone(?)", (None,)) 232 244 val = cur.fetchone()[0] 233 self. failUnlessEqual(val, 1)245 self.assertEqual(val, 1) 234 246 235 247 def CheckParamBlob(self): … … 237 249 cur.execute("select isblob(?)", (buffer("blob"),)) 238 250 val = cur.fetchone()[0] 239 self.failUnlessEqual(val, 1) 251 self.assertEqual(val, 1) 252 253 def CheckParamLongLong(self): 254 cur = self.con.cursor() 255 cur.execute("select islonglong(?)", (1<<42,)) 256 val = cur.fetchone()[0] 257 self.assertEqual(val, 1) 240 258 241 259 class AggregateTests(unittest.TestCase): … … 281 299 self.fail("should have raised an AttributeError") 282 300 except AttributeError, e: 283 self. failUnlessEqual(e.args[0], "AggrNoStep instance has no attribute 'step'")301 self.assertEqual(e.args[0], "AggrNoStep instance has no attribute 'step'") 284 302 285 303 def CheckAggrNoFinalize(self): … … 290 308 self.fail("should have raised an OperationalError") 291 309 except sqlite.OperationalError, e: 292 self. failUnlessEqual(e.args[0], "user-defined aggregate's 'finalize' method raised error")310 self.assertEqual(e.args[0], "user-defined aggregate's 'finalize' method raised error") 293 311 294 312 def CheckAggrExceptionInInit(self): … … 299 317 self.fail("should have raised an OperationalError") 300 318 except sqlite.OperationalError, e: 301 self. failUnlessEqual(e.args[0], "user-defined aggregate's '__init__' method raised error")319 self.assertEqual(e.args[0], "user-defined aggregate's '__init__' method raised error") 302 320 303 321 def CheckAggrExceptionInStep(self): … … 308 326 self.fail("should have raised an OperationalError") 309 327 except sqlite.OperationalError, e: 310 self. failUnlessEqual(e.args[0], "user-defined aggregate's 'step' method raised error")328 self.assertEqual(e.args[0], "user-defined aggregate's 'step' method raised error") 311 329 312 330 def CheckAggrExceptionInFinalize(self): … … 317 335 self.fail("should have raised an OperationalError") 318 336 except sqlite.OperationalError, e: 319 self. failUnlessEqual(e.args[0], "user-defined aggregate's 'finalize' method raised error")337 self.assertEqual(e.args[0], "user-defined aggregate's 'finalize' method raised error") 320 338 321 339 def CheckAggrCheckParamStr(self): … … 323 341 cur.execute("select checkType('str', ?)", ("foo",)) 324 342 val = cur.fetchone()[0] 325 self. failUnlessEqual(val, 1)343 self.assertEqual(val, 1) 326 344 327 345 def CheckAggrCheckParamInt(self): … … 329 347 cur.execute("select checkType('int', ?)", (42,)) 330 348 val = cur.fetchone()[0] 331 self. failUnlessEqual(val, 1)349 self.assertEqual(val, 1) 332 350 333 351 def CheckAggrCheckParamFloat(self): … … 335 353 cur.execute("select checkType('float', ?)", (3.14,)) 336 354 val = cur.fetchone()[0] 337 self. failUnlessEqual(val, 1)355 self.assertEqual(val, 1) 338 356 339 357 def CheckAggrCheckParamNone(self): … … 341 359 cur.execute("select checkType('None', ?)", (None,)) 342 360 val = cur.fetchone()[0] 343 self. failUnlessEqual(val, 1)361 self.assertEqual(val, 1) 344 362 345 363 def CheckAggrCheckParamBlob(self): … … 347 365 cur.execute("select checkType('blob', ?)", (buffer("blob"),)) 348 366 val = cur.fetchone()[0] 349 self. failUnlessEqual(val, 1)367 self.assertEqual(val, 1) 350 368 351 369 def CheckAggrCheckAggrSum(self): … … 355 373 cur.execute("select mysum(i) from test") 356 374 val = cur.fetchone()[0] 357 self.failUnlessEqual(val, 60) 358 359 def authorizer_cb(action, arg1, arg2, dbname, source): 360 if action != sqlite.SQLITE_SELECT: 361 return sqlite.SQLITE_DENY 362 if arg2 == 'c2' or arg1 == 't2': 363 return sqlite.SQLITE_DENY 364 return sqlite.SQLITE_OK 375 self.assertEqual(val, 60) 365 376 366 377 class AuthorizerTests(unittest.TestCase): 378 @staticmethod 379 def authorizer_cb(action, arg1, arg2, dbname, source): 380 if action != sqlite.SQLITE_SELECT: 381 return sqlite.SQLITE_DENY 382 if arg2 == 'c2' or arg1 == 't2': 383 return sqlite.SQLITE_DENY 384 return sqlite.SQLITE_OK 385 367 386 def setUp(self): 368 387 self.con = sqlite.connect(":memory:") … … 377 396 self.con.execute("select c2 from t2") 378 397 379 self.con.set_authorizer( authorizer_cb)398 self.con.set_authorizer(self.authorizer_cb) 380 399 381 400 def tearDown(self): 382 401 pass 383 402 384 def CheckTableAccess(self):403 def test_table_access(self): 385 404 try: 386 405 self.con.execute("select * from t2") … … 391 410 self.fail("should have raised an exception due to missing privileges") 392 411 393 def CheckColumnAccess(self):412 def test_column_access(self): 394 413 try: 395 414 self.con.execute("select c2 from t1") … … 400 419 self.fail("should have raised an exception due to missing privileges") 401 420 421 class AuthorizerRaiseExceptionTests(AuthorizerTests): 422 @staticmethod 423 def authorizer_cb(action, arg1, arg2, dbname, source): 424 if action != sqlite.SQLITE_SELECT: 425 raise ValueError 426 if arg2 == 'c2' or arg1 == 't2': 427 raise ValueError 428 return sqlite.SQLITE_OK 429 430 class AuthorizerIllegalTypeTests(AuthorizerTests): 431 @staticmethod 432 def authorizer_cb(action, arg1, arg2, dbname, source): 433 if action != sqlite.SQLITE_SELECT: 434 return 0.0 435 if arg2 == 'c2' or arg1 == 't2': 436 return 0.0 437 return sqlite.SQLITE_OK 438 439 class AuthorizerLargeIntegerTests(AuthorizerTests): 440 @staticmethod 441 def authorizer_cb(action, arg1, arg2, dbname, source): 442 if action != sqlite.SQLITE_SELECT: 443 return 2**32 444 if arg2 == 'c2' or arg1 == 't2': 445 return 2**32 446 return sqlite.SQLITE_OK 447 448 402 449 def suite(): 403 450 function_suite = unittest.makeSuite(FunctionTests, "Check") 404 451 aggregate_suite = unittest.makeSuite(AggregateTests, "Check") 405 authorizer_suite = unittest.makeSuite(AuthorizerTests, "Check") 406 return unittest.TestSuite((function_suite, aggregate_suite, authorizer_suite)) 452 authorizer_suite = unittest.makeSuite(AuthorizerTests) 453 return unittest.TestSuite(( 454 function_suite, 455 aggregate_suite, 456 authorizer_suite, 457 unittest.makeSuite(AuthorizerRaiseExceptionTests), 458 unittest.makeSuite(AuthorizerIllegalTypeTests), 459 unittest.makeSuite(AuthorizerLargeIntegerTests), 460 )) 407 461 408 462 def test():
Note:
See TracChangeset
for help on using the changeset viewer.