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

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import unittest
  5. import sys
  6. import threading
  7. import sqlite3 as sqlite
  8.  
  9. class ModuleTests(unittest.TestCase):
  10.     
  11.     def CheckAPILevel(self):
  12.         self.assertEqual(sqlite.apilevel, '2.0', 'apilevel is %s, should be 2.0' % sqlite.apilevel)
  13.  
  14.     
  15.     def CheckThreadSafety(self):
  16.         self.assertEqual(sqlite.threadsafety, 1, 'threadsafety is %d, should be 1' % sqlite.threadsafety)
  17.  
  18.     
  19.     def CheckParamStyle(self):
  20.         self.assertEqual(sqlite.paramstyle, 'qmark', "paramstyle is '%s', should be 'qmark'" % sqlite.paramstyle)
  21.  
  22.     
  23.     def CheckWarning(self):
  24.         self.assert_(issubclass(sqlite.Warning, StandardError), 'Warning is not a subclass of StandardError')
  25.  
  26.     
  27.     def CheckError(self):
  28.         self.failUnless(issubclass(sqlite.Error, StandardError), 'Error is not a subclass of StandardError')
  29.  
  30.     
  31.     def CheckInterfaceError(self):
  32.         self.failUnless(issubclass(sqlite.InterfaceError, sqlite.Error), 'InterfaceError is not a subclass of Error')
  33.  
  34.     
  35.     def CheckDatabaseError(self):
  36.         self.failUnless(issubclass(sqlite.DatabaseError, sqlite.Error), 'DatabaseError is not a subclass of Error')
  37.  
  38.     
  39.     def CheckDataError(self):
  40.         self.failUnless(issubclass(sqlite.DataError, sqlite.DatabaseError), 'DataError is not a subclass of DatabaseError')
  41.  
  42.     
  43.     def CheckOperationalError(self):
  44.         self.failUnless(issubclass(sqlite.OperationalError, sqlite.DatabaseError), 'OperationalError is not a subclass of DatabaseError')
  45.  
  46.     
  47.     def CheckIntegrityError(self):
  48.         self.failUnless(issubclass(sqlite.IntegrityError, sqlite.DatabaseError), 'IntegrityError is not a subclass of DatabaseError')
  49.  
  50.     
  51.     def CheckInternalError(self):
  52.         self.failUnless(issubclass(sqlite.InternalError, sqlite.DatabaseError), 'InternalError is not a subclass of DatabaseError')
  53.  
  54.     
  55.     def CheckProgrammingError(self):
  56.         self.failUnless(issubclass(sqlite.ProgrammingError, sqlite.DatabaseError), 'ProgrammingError is not a subclass of DatabaseError')
  57.  
  58.     
  59.     def CheckNotSupportedError(self):
  60.         self.failUnless(issubclass(sqlite.NotSupportedError, sqlite.DatabaseError), 'NotSupportedError is not a subclass of DatabaseError')
  61.  
  62.  
  63.  
  64. class ConnectionTests(unittest.TestCase):
  65.     
  66.     def setUp(self):
  67.         self.cx = sqlite.connect(':memory:')
  68.         cu = self.cx.cursor()
  69.         cu.execute('create table test(id integer primary key, name text)')
  70.         cu.execute('insert into test(name) values (?)', ('foo',))
  71.  
  72.     
  73.     def tearDown(self):
  74.         self.cx.close()
  75.  
  76.     
  77.     def CheckCommit(self):
  78.         self.cx.commit()
  79.  
  80.     
  81.     def CheckCommitAfterNoChanges(self):
  82.         self.cx.commit()
  83.         self.cx.commit()
  84.  
  85.     
  86.     def CheckRollback(self):
  87.         self.cx.rollback()
  88.  
  89.     
  90.     def CheckRollbackAfterNoChanges(self):
  91.         self.cx.rollback()
  92.         self.cx.rollback()
  93.  
  94.     
  95.     def CheckCursor(self):
  96.         cu = self.cx.cursor()
  97.  
  98.     
  99.     def CheckFailedOpen(self):
  100.         YOU_CANNOT_OPEN_THIS = '/foo/bar/bla/23534/mydb.db'
  101.         
  102.         try:
  103.             con = sqlite.connect(YOU_CANNOT_OPEN_THIS)
  104.         except sqlite.OperationalError:
  105.             return None
  106.  
  107.         self.fail('should have raised an OperationalError')
  108.  
  109.     
  110.     def CheckClose(self):
  111.         self.cx.close()
  112.  
  113.     
  114.     def CheckExceptions(self):
  115.         self.failUnlessEqual(self.cx.Warning, sqlite.Warning)
  116.         self.failUnlessEqual(self.cx.Error, sqlite.Error)
  117.         self.failUnlessEqual(self.cx.InterfaceError, sqlite.InterfaceError)
  118.         self.failUnlessEqual(self.cx.DatabaseError, sqlite.DatabaseError)
  119.         self.failUnlessEqual(self.cx.DataError, sqlite.DataError)
  120.         self.failUnlessEqual(self.cx.OperationalError, sqlite.OperationalError)
  121.         self.failUnlessEqual(self.cx.IntegrityError, sqlite.IntegrityError)
  122.         self.failUnlessEqual(self.cx.InternalError, sqlite.InternalError)
  123.         self.failUnlessEqual(self.cx.ProgrammingError, sqlite.ProgrammingError)
  124.         self.failUnlessEqual(self.cx.NotSupportedError, sqlite.NotSupportedError)
  125.  
  126.  
  127.  
  128. class CursorTests(unittest.TestCase):
  129.     
  130.     def setUp(self):
  131.         self.cx = sqlite.connect(':memory:')
  132.         self.cu = self.cx.cursor()
  133.         self.cu.execute('create table test(id integer primary key, name text, income number)')
  134.         self.cu.execute('insert into test(name) values (?)', ('foo',))
  135.  
  136.     
  137.     def tearDown(self):
  138.         self.cu.close()
  139.         self.cx.close()
  140.  
  141.     
  142.     def CheckExecuteNoArgs(self):
  143.         self.cu.execute('delete from test')
  144.  
  145.     
  146.     def CheckExecuteIllegalSql(self):
  147.         
  148.         try:
  149.             self.cu.execute('select asdf')
  150.             self.fail('should have raised an OperationalError')
  151.         except sqlite.OperationalError:
  152.             return None
  153.             self.fail('raised wrong exception')
  154.  
  155.  
  156.     
  157.     def CheckExecuteTooMuchSql(self):
  158.         
  159.         try:
  160.             self.cu.execute('select 5+4; select 4+5')
  161.             self.fail('should have raised a Warning')
  162.         except sqlite.Warning:
  163.             return None
  164.             self.fail('raised wrong exception')
  165.  
  166.  
  167.     
  168.     def CheckExecuteTooMuchSql2(self):
  169.         self.cu.execute('select 5+4; -- foo bar')
  170.  
  171.     
  172.     def CheckExecuteTooMuchSql3(self):
  173.         self.cu.execute('\n            select 5+4;\n\n            /*\n            foo\n            */\n            ')
  174.  
  175.     
  176.     def CheckExecuteWrongSqlArg(self):
  177.         
  178.         try:
  179.             self.cu.execute(42)
  180.             self.fail('should have raised a ValueError')
  181.         except ValueError:
  182.             return None
  183.             self.fail('raised wrong exception.')
  184.  
  185.  
  186.     
  187.     def CheckExecuteArgInt(self):
  188.         self.cu.execute('insert into test(id) values (?)', (42,))
  189.  
  190.     
  191.     def CheckExecuteArgFloat(self):
  192.         self.cu.execute('insert into test(income) values (?)', (2500.32,))
  193.  
  194.     
  195.     def CheckExecuteArgString(self):
  196.         self.cu.execute('insert into test(name) values (?)', ('Hugo',))
  197.  
  198.     
  199.     def CheckExecuteWrongNoOfArgs1(self):
  200.         
  201.         try:
  202.             self.cu.execute('insert into test(id) values (?)', (17, 'Egon'))
  203.             self.fail('should have raised ProgrammingError')
  204.         except sqlite.ProgrammingError:
  205.             pass
  206.  
  207.  
  208.     
  209.     def CheckExecuteWrongNoOfArgs2(self):
  210.         
  211.         try:
  212.             self.cu.execute('insert into test(id) values (?)')
  213.             self.fail('should have raised ProgrammingError')
  214.         except sqlite.ProgrammingError:
  215.             pass
  216.  
  217.  
  218.     
  219.     def CheckExecuteWrongNoOfArgs3(self):
  220.         
  221.         try:
  222.             self.cu.execute('insert into test(id) values (?)')
  223.             self.fail('should have raised ProgrammingError')
  224.         except sqlite.ProgrammingError:
  225.             pass
  226.  
  227.  
  228.     
  229.     def CheckExecuteParamList(self):
  230.         self.cu.execute("insert into test(name) values ('foo')")
  231.         self.cu.execute('select name from test where name=?', [
  232.             'foo'])
  233.         row = self.cu.fetchone()
  234.         self.failUnlessEqual(row[0], 'foo')
  235.  
  236.     
  237.     def CheckExecuteParamSequence(self):
  238.         
  239.         class L(object):
  240.             
  241.             def __len__(self):
  242.                 return 1
  243.  
  244.             
  245.             def __getitem__(self, x):
  246.                 return 'foo'
  247.  
  248.  
  249.         self.cu.execute("insert into test(name) values ('foo')")
  250.         self.cu.execute('select name from test where name=?', L())
  251.         row = self.cu.fetchone()
  252.         self.failUnlessEqual(row[0], 'foo')
  253.  
  254.     
  255.     def CheckExecuteDictMapping(self):
  256.         self.cu.execute("insert into test(name) values ('foo')")
  257.         self.cu.execute('select name from test where name=:name', {
  258.             'name': 'foo' })
  259.         row = self.cu.fetchone()
  260.         self.failUnlessEqual(row[0], 'foo')
  261.  
  262.     
  263.     def CheckExecuteDictMapping_Mapping(self):
  264.         if sys.version_info < (2, 5, 0):
  265.             return None
  266.         
  267.         class D(dict):
  268.             
  269.             def __missing__(self, key):
  270.                 return 'foo'
  271.  
  272.  
  273.         self.cu.execute("insert into test(name) values ('foo')")
  274.         self.cu.execute('select name from test where name=:name', D())
  275.         row = self.cu.fetchone()
  276.         self.failUnlessEqual(row[0], 'foo')
  277.  
  278.     
  279.     def CheckExecuteDictMappingTooLittleArgs(self):
  280.         self.cu.execute("insert into test(name) values ('foo')")
  281.         
  282.         try:
  283.             self.cu.execute('select name from test where name=:name and id=:id', {
  284.                 'name': 'foo' })
  285.             self.fail('should have raised ProgrammingError')
  286.         except sqlite.ProgrammingError:
  287.             pass
  288.  
  289.  
  290.     
  291.     def CheckExecuteDictMappingNoArgs(self):
  292.         self.cu.execute("insert into test(name) values ('foo')")
  293.         
  294.         try:
  295.             self.cu.execute('select name from test where name=:name')
  296.             self.fail('should have raised ProgrammingError')
  297.         except sqlite.ProgrammingError:
  298.             pass
  299.  
  300.  
  301.     
  302.     def CheckExecuteDictMappingUnnamed(self):
  303.         self.cu.execute("insert into test(name) values ('foo')")
  304.         
  305.         try:
  306.             self.cu.execute('select name from test where name=?', {
  307.                 'name': 'foo' })
  308.             self.fail('should have raised ProgrammingError')
  309.         except sqlite.ProgrammingError:
  310.             pass
  311.  
  312.  
  313.     
  314.     def CheckClose(self):
  315.         self.cu.close()
  316.  
  317.     
  318.     def CheckRowcountExecute(self):
  319.         self.cu.execute('delete from test')
  320.         self.cu.execute("insert into test(name) values ('foo')")
  321.         self.cu.execute("insert into test(name) values ('foo')")
  322.         self.cu.execute("update test set name='bar'")
  323.         self.failUnlessEqual(self.cu.rowcount, 2)
  324.  
  325.     
  326.     def CheckRowcountSelect(self):
  327.         self.cu.execute('select 5 union select 6')
  328.         self.failUnlessEqual(self.cu.rowcount, -1)
  329.  
  330.     
  331.     def CheckRowcountExecutemany(self):
  332.         self.cu.execute('delete from test')
  333.         self.cu.executemany('insert into test(name) values (?)', [
  334.             (1,),
  335.             (2,),
  336.             (3,)])
  337.         self.failUnlessEqual(self.cu.rowcount, 3)
  338.  
  339.     
  340.     def CheckTotalChanges(self):
  341.         self.cu.execute("insert into test(name) values ('foo')")
  342.         self.cu.execute("insert into test(name) values ('foo')")
  343.         if self.cx.total_changes < 2:
  344.             self.fail('total changes reported wrong value')
  345.         
  346.  
  347.     
  348.     def CheckExecuteManySequence(self):
  349.         []([], [ (x,) for x in range(100, 110) ])
  350.  
  351.     
  352.     def CheckExecuteManyIterator(self):
  353.         
  354.         class MyIter:
  355.             
  356.             def __init__(self):
  357.                 self.value = 5
  358.  
  359.             
  360.             def next(self):
  361.                 if self.value == 10:
  362.                     raise StopIteration
  363.                 self.value == 10
  364.                 self.value += 1
  365.                 return (self.value,)
  366.  
  367.  
  368.         self.cu.executemany('insert into test(income) values (?)', MyIter())
  369.  
  370.     
  371.     def CheckExecuteManyGenerator(self):
  372.         
  373.         def mygen():
  374.             for i in range(5):
  375.                 yield (i,)
  376.             
  377.  
  378.         self.cu.executemany('insert into test(income) values (?)', mygen())
  379.  
  380.     
  381.     def CheckExecuteManyWrongSqlArg(self):
  382.         
  383.         try:
  384.             self.cu.executemany(42, [
  385.                 (3,)])
  386.             self.fail('should have raised a ValueError')
  387.         except ValueError:
  388.             return None
  389.             self.fail('raised wrong exception.')
  390.  
  391.  
  392.     
  393.     def CheckExecuteManySelect(self):
  394.         
  395.         try:
  396.             self.cu.executemany('select ?', [
  397.                 (3,)])
  398.             self.fail('should have raised a ProgrammingError')
  399.         except sqlite.ProgrammingError:
  400.             return None
  401.             self.fail('raised wrong exception.')
  402.  
  403.  
  404.     
  405.     def CheckExecuteManyNotIterable(self):
  406.         
  407.         try:
  408.             self.cu.executemany('insert into test(income) values (?)', 42)
  409.             self.fail('should have raised a TypeError')
  410.         except TypeError:
  411.             return None
  412.             except Exception:
  413.                 e = None
  414.                 print 'raised', e.__class__
  415.                 self.fail('raised wrong exception.')
  416.             except:
  417.                 None<EXCEPTION MATCH>Exception
  418.             
  419.  
  420.  
  421.     
  422.     def CheckFetchIter(self):
  423.         self.cu.execute('delete from test')
  424.         self.cu.execute('insert into test(id) values (?)', (5,))
  425.         self.cu.execute('insert into test(id) values (?)', (6,))
  426.         self.cu.execute('select id from test order by id')
  427.         lst = []
  428.         for row in self.cu:
  429.             lst.append(row[0])
  430.         
  431.         self.failUnlessEqual(lst[0], 5)
  432.         self.failUnlessEqual(lst[1], 6)
  433.  
  434.     
  435.     def CheckFetchone(self):
  436.         self.cu.execute('select name from test')
  437.         row = self.cu.fetchone()
  438.         self.failUnlessEqual(row[0], 'foo')
  439.         row = self.cu.fetchone()
  440.         self.failUnlessEqual(row, None)
  441.  
  442.     
  443.     def CheckFetchoneNoStatement(self):
  444.         cur = self.cx.cursor()
  445.         row = cur.fetchone()
  446.         self.failUnlessEqual(row, None)
  447.  
  448.     
  449.     def CheckArraySize(self):
  450.         self.failUnlessEqual(self.cu.arraysize, 1)
  451.         self.cu.arraysize = 2
  452.         self.cu.execute('delete from test')
  453.         self.cu.execute("insert into test(name) values ('A')")
  454.         self.cu.execute("insert into test(name) values ('B')")
  455.         self.cu.execute("insert into test(name) values ('C')")
  456.         self.cu.execute('select name from test')
  457.         res = self.cu.fetchmany()
  458.         self.failUnlessEqual(len(res), 2)
  459.  
  460.     
  461.     def CheckFetchmany(self):
  462.         self.cu.execute('select name from test')
  463.         res = self.cu.fetchmany(100)
  464.         self.failUnlessEqual(len(res), 1)
  465.         res = self.cu.fetchmany(100)
  466.         self.failUnlessEqual(res, [])
  467.  
  468.     
  469.     def CheckFetchmanyKwArg(self):
  470.         self.cu.execute('select name from test')
  471.         res = self.cu.fetchmany(size = 100)
  472.         self.failUnlessEqual(len(res), 1)
  473.  
  474.     
  475.     def CheckFetchall(self):
  476.         self.cu.execute('select name from test')
  477.         res = self.cu.fetchall()
  478.         self.failUnlessEqual(len(res), 1)
  479.         res = self.cu.fetchall()
  480.         self.failUnlessEqual(res, [])
  481.  
  482.     
  483.     def CheckSetinputsizes(self):
  484.         self.cu.setinputsizes([
  485.             3,
  486.             4,
  487.             5])
  488.  
  489.     
  490.     def CheckSetoutputsize(self):
  491.         self.cu.setoutputsize(5, 0)
  492.  
  493.     
  494.     def CheckSetoutputsizeNoColumn(self):
  495.         self.cu.setoutputsize(42)
  496.  
  497.     
  498.     def CheckCursorConnection(self):
  499.         self.failUnlessEqual(self.cu.connection, self.cx)
  500.  
  501.     
  502.     def CheckWrongCursorCallable(self):
  503.         
  504.         try:
  505.             
  506.             def f():
  507.                 pass
  508.  
  509.             cur = self.cx.cursor(f)
  510.             self.fail('should have raised a TypeError')
  511.         except TypeError:
  512.             return None
  513.  
  514.         self.fail('should have raised a ValueError')
  515.  
  516.     
  517.     def CheckCursorWrongClass(self):
  518.         
  519.         class Foo:
  520.             pass
  521.  
  522.         foo = Foo()
  523.         
  524.         try:
  525.             cur = sqlite.Cursor(foo)
  526.             self.fail('should have raised a ValueError')
  527.         except TypeError:
  528.             pass
  529.  
  530.  
  531.  
  532.  
  533. class ThreadTests(unittest.TestCase):
  534.     
  535.     def setUp(self):
  536.         self.con = sqlite.connect(':memory:')
  537.         self.cur = self.con.cursor()
  538.         self.cur.execute('create table test(id integer primary key, name text, bin binary, ratio number, ts timestamp)')
  539.  
  540.     
  541.     def tearDown(self):
  542.         self.cur.close()
  543.         self.con.close()
  544.  
  545.     
  546.     def CheckConCursor(self):
  547.         
  548.         def run(con, errors):
  549.             
  550.             try:
  551.                 cur = con.cursor()
  552.                 errors.append('did not raise ProgrammingError')
  553.                 return None
  554.             except sqlite.ProgrammingError:
  555.                 return None
  556.                 errors.append('raised wrong exception')
  557.  
  558.  
  559.         errors = []
  560.         t = threading.Thread(target = run, kwargs = {
  561.             'con': self.con,
  562.             'errors': errors })
  563.         t.start()
  564.         t.join()
  565.         if len(errors) > 0:
  566.             self.fail('\n'.join(errors))
  567.         
  568.  
  569.     
  570.     def CheckConCommit(self):
  571.         
  572.         def run(con, errors):
  573.             
  574.             try:
  575.                 con.commit()
  576.                 errors.append('did not raise ProgrammingError')
  577.                 return None
  578.             except sqlite.ProgrammingError:
  579.                 return None
  580.                 errors.append('raised wrong exception')
  581.  
  582.  
  583.         errors = []
  584.         t = threading.Thread(target = run, kwargs = {
  585.             'con': self.con,
  586.             'errors': errors })
  587.         t.start()
  588.         t.join()
  589.         if len(errors) > 0:
  590.             self.fail('\n'.join(errors))
  591.         
  592.  
  593.     
  594.     def CheckConRollback(self):
  595.         
  596.         def run(con, errors):
  597.             
  598.             try:
  599.                 con.rollback()
  600.                 errors.append('did not raise ProgrammingError')
  601.                 return None
  602.             except sqlite.ProgrammingError:
  603.                 return None
  604.                 errors.append('raised wrong exception')
  605.  
  606.  
  607.         errors = []
  608.         t = threading.Thread(target = run, kwargs = {
  609.             'con': self.con,
  610.             'errors': errors })
  611.         t.start()
  612.         t.join()
  613.         if len(errors) > 0:
  614.             self.fail('\n'.join(errors))
  615.         
  616.  
  617.     
  618.     def CheckConClose(self):
  619.         
  620.         def run(con, errors):
  621.             
  622.             try:
  623.                 con.close()
  624.                 errors.append('did not raise ProgrammingError')
  625.                 return None
  626.             except sqlite.ProgrammingError:
  627.                 return None
  628.                 errors.append('raised wrong exception')
  629.  
  630.  
  631.         errors = []
  632.         t = threading.Thread(target = run, kwargs = {
  633.             'con': self.con,
  634.             'errors': errors })
  635.         t.start()
  636.         t.join()
  637.         if len(errors) > 0:
  638.             self.fail('\n'.join(errors))
  639.         
  640.  
  641.     
  642.     def CheckCurImplicitBegin(self):
  643.         
  644.         def run(cur, errors):
  645.             
  646.             try:
  647.                 cur.execute("insert into test(name) values ('a')")
  648.                 errors.append('did not raise ProgrammingError')
  649.                 return None
  650.             except sqlite.ProgrammingError:
  651.                 return None
  652.                 errors.append('raised wrong exception')
  653.  
  654.  
  655.         errors = []
  656.         t = threading.Thread(target = run, kwargs = {
  657.             'cur': self.cur,
  658.             'errors': errors })
  659.         t.start()
  660.         t.join()
  661.         if len(errors) > 0:
  662.             self.fail('\n'.join(errors))
  663.         
  664.  
  665.     
  666.     def CheckCurClose(self):
  667.         
  668.         def run(cur, errors):
  669.             
  670.             try:
  671.                 cur.close()
  672.                 errors.append('did not raise ProgrammingError')
  673.                 return None
  674.             except sqlite.ProgrammingError:
  675.                 return None
  676.                 errors.append('raised wrong exception')
  677.  
  678.  
  679.         errors = []
  680.         t = threading.Thread(target = run, kwargs = {
  681.             'cur': self.cur,
  682.             'errors': errors })
  683.         t.start()
  684.         t.join()
  685.         if len(errors) > 0:
  686.             self.fail('\n'.join(errors))
  687.         
  688.  
  689.     
  690.     def CheckCurExecute(self):
  691.         
  692.         def run(cur, errors):
  693.             
  694.             try:
  695.                 cur.execute('select name from test')
  696.                 errors.append('did not raise ProgrammingError')
  697.                 return None
  698.             except sqlite.ProgrammingError:
  699.                 return None
  700.                 errors.append('raised wrong exception')
  701.  
  702.  
  703.         errors = []
  704.         self.cur.execute("insert into test(name) values ('a')")
  705.         t = threading.Thread(target = run, kwargs = {
  706.             'cur': self.cur,
  707.             'errors': errors })
  708.         t.start()
  709.         t.join()
  710.         if len(errors) > 0:
  711.             self.fail('\n'.join(errors))
  712.         
  713.  
  714.     
  715.     def CheckCurIterNext(self):
  716.         
  717.         def run(cur, errors):
  718.             
  719.             try:
  720.                 row = cur.fetchone()
  721.                 errors.append('did not raise ProgrammingError')
  722.                 return None
  723.             except sqlite.ProgrammingError:
  724.                 return None
  725.                 errors.append('raised wrong exception')
  726.  
  727.  
  728.         errors = []
  729.         self.cur.execute("insert into test(name) values ('a')")
  730.         self.cur.execute('select name from test')
  731.         t = threading.Thread(target = run, kwargs = {
  732.             'cur': self.cur,
  733.             'errors': errors })
  734.         t.start()
  735.         t.join()
  736.         if len(errors) > 0:
  737.             self.fail('\n'.join(errors))
  738.         
  739.  
  740.  
  741.  
  742. class ConstructorTests(unittest.TestCase):
  743.     
  744.     def CheckDate(self):
  745.         d = sqlite.Date(2004, 10, 28)
  746.  
  747.     
  748.     def CheckTime(self):
  749.         t = sqlite.Time(12, 39, 35)
  750.  
  751.     
  752.     def CheckTimestamp(self):
  753.         ts = sqlite.Timestamp(2004, 10, 28, 12, 39, 35)
  754.  
  755.     
  756.     def CheckDateFromTicks(self):
  757.         d = sqlite.DateFromTicks(42)
  758.  
  759.     
  760.     def CheckTimeFromTicks(self):
  761.         t = sqlite.TimeFromTicks(42)
  762.  
  763.     
  764.     def CheckTimestampFromTicks(self):
  765.         ts = sqlite.TimestampFromTicks(42)
  766.  
  767.     
  768.     def CheckBinary(self):
  769.         b = sqlite.Binary(chr(0) + "'")
  770.  
  771.  
  772.  
  773. class ExtensionTests(unittest.TestCase):
  774.     
  775.     def CheckScriptStringSql(self):
  776.         con = sqlite.connect(':memory:')
  777.         cur = con.cursor()
  778.         cur.executescript('\n            -- bla bla\n            /* a stupid comment */\n            create table a(i);\n            insert into a(i) values (5);\n            ')
  779.         cur.execute('select i from a')
  780.         res = cur.fetchone()[0]
  781.         self.failUnlessEqual(res, 5)
  782.  
  783.     
  784.     def CheckScriptStringUnicode(self):
  785.         con = sqlite.connect(':memory:')
  786.         cur = con.cursor()
  787.         cur.executescript(u'\n            create table a(i);\n            insert into a(i) values (5);\n            select i from a;\n            delete from a;\n            insert into a(i) values (6);\n            ')
  788.         cur.execute('select i from a')
  789.         res = cur.fetchone()[0]
  790.         self.failUnlessEqual(res, 6)
  791.  
  792.     
  793.     def CheckScriptErrorIncomplete(self):
  794.         con = sqlite.connect(':memory:')
  795.         cur = con.cursor()
  796.         raised = False
  797.         
  798.         try:
  799.             cur.executescript('create table test(sadfsadfdsa')
  800.         except sqlite.ProgrammingError:
  801.             raised = True
  802.  
  803.         self.failUnlessEqual(raised, True, 'should have raised an exception')
  804.  
  805.     
  806.     def CheckScriptErrorNormal(self):
  807.         con = sqlite.connect(':memory:')
  808.         cur = con.cursor()
  809.         raised = False
  810.         
  811.         try:
  812.             cur.executescript('create table test(sadfsadfdsa); select foo from hurz;')
  813.         except sqlite.OperationalError:
  814.             raised = True
  815.  
  816.         self.failUnlessEqual(raised, True, 'should have raised an exception')
  817.  
  818.     
  819.     def CheckConnectionExecute(self):
  820.         con = sqlite.connect(':memory:')
  821.         result = con.execute('select 5').fetchone()[0]
  822.         self.failUnlessEqual(result, 5, 'Basic test of Connection.execute')
  823.  
  824.     
  825.     def CheckConnectionExecutemany(self):
  826.         con = sqlite.connect(':memory:')
  827.         con.execute('create table test(foo)')
  828.         con.executemany('insert into test(foo) values (?)', [
  829.             (3,),
  830.             (4,)])
  831.         result = con.execute('select foo from test order by foo').fetchall()
  832.         self.failUnlessEqual(result[0][0], 3, 'Basic test of Connection.executemany')
  833.         self.failUnlessEqual(result[1][0], 4, 'Basic test of Connection.executemany')
  834.  
  835.     
  836.     def CheckConnectionExecutescript(self):
  837.         con = sqlite.connect(':memory:')
  838.         con.executescript('create table test(foo); insert into test(foo) values (5);')
  839.         result = con.execute('select foo from test').fetchone()[0]
  840.         self.failUnlessEqual(result, 5, 'Basic test of Connection.executescript')
  841.  
  842.  
  843.  
  844. class ClosedTests(unittest.TestCase):
  845.     
  846.     def setUp(self):
  847.         pass
  848.  
  849.     
  850.     def tearDown(self):
  851.         pass
  852.  
  853.     
  854.     def CheckClosedConCursor(self):
  855.         con = sqlite.connect(':memory:')
  856.         con.close()
  857.         
  858.         try:
  859.             cur = con.cursor()
  860.             self.fail('Should have raised a ProgrammingError')
  861.         except sqlite.ProgrammingError:
  862.             pass
  863.         except:
  864.             self.fail('Should have raised a ProgrammingError')
  865.  
  866.  
  867.     
  868.     def CheckClosedConCommit(self):
  869.         con = sqlite.connect(':memory:')
  870.         con.close()
  871.         
  872.         try:
  873.             con.commit()
  874.             self.fail('Should have raised a ProgrammingError')
  875.         except sqlite.ProgrammingError:
  876.             pass
  877.         except:
  878.             self.fail('Should have raised a ProgrammingError')
  879.  
  880.  
  881.     
  882.     def CheckClosedConRollback(self):
  883.         con = sqlite.connect(':memory:')
  884.         con.close()
  885.         
  886.         try:
  887.             con.rollback()
  888.             self.fail('Should have raised a ProgrammingError')
  889.         except sqlite.ProgrammingError:
  890.             pass
  891.         except:
  892.             self.fail('Should have raised a ProgrammingError')
  893.  
  894.  
  895.     
  896.     def CheckClosedCurExecute(self):
  897.         con = sqlite.connect(':memory:')
  898.         cur = con.cursor()
  899.         con.close()
  900.         
  901.         try:
  902.             cur.execute('select 4')
  903.             self.fail('Should have raised a ProgrammingError')
  904.         except sqlite.ProgrammingError:
  905.             pass
  906.         except:
  907.             self.fail('Should have raised a ProgrammingError')
  908.  
  909.  
  910.  
  911.  
  912. def suite():
  913.     module_suite = unittest.makeSuite(ModuleTests, 'Check')
  914.     connection_suite = unittest.makeSuite(ConnectionTests, 'Check')
  915.     cursor_suite = unittest.makeSuite(CursorTests, 'Check')
  916.     thread_suite = unittest.makeSuite(ThreadTests, 'Check')
  917.     constructor_suite = unittest.makeSuite(ConstructorTests, 'Check')
  918.     ext_suite = unittest.makeSuite(ExtensionTests, 'Check')
  919.     closed_suite = unittest.makeSuite(ClosedTests, 'Check')
  920.     return unittest.TestSuite((module_suite, connection_suite, cursor_suite, thread_suite, constructor_suite, ext_suite, closed_suite))
  921.  
  922.  
  923. def test():
  924.     runner = unittest.TextTestRunner()
  925.     runner.run(suite())
  926.  
  927. if __name__ == '__main__':
  928.     test()
  929.  
  930.