home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_2795 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  19.6 KB  |  511 lines

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