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

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import zlib
  5. import datetime
  6. import unittest
  7. import sqlite3 as sqlite
  8.  
  9. class SqliteTypeTests(unittest.TestCase):
  10.     
  11.     def setUp(self):
  12.         self.con = sqlite.connect(':memory:')
  13.         self.cur = self.con.cursor()
  14.         self.cur.execute('create table test(i integer, s varchar, f number, b blob)')
  15.  
  16.     
  17.     def tearDown(self):
  18.         self.cur.close()
  19.         self.con.close()
  20.  
  21.     
  22.     def CheckString(self):
  23.         self.cur.execute('insert into test(s) values (?)', (u'├ûsterreich',))
  24.         self.cur.execute('select s from test')
  25.         row = self.cur.fetchone()
  26.         self.failUnlessEqual(row[0], u'├ûsterreich')
  27.  
  28.     
  29.     def CheckSmallInt(self):
  30.         self.cur.execute('insert into test(i) values (?)', (42,))
  31.         self.cur.execute('select i from test')
  32.         row = self.cur.fetchone()
  33.         self.failUnlessEqual(row[0], 42)
  34.  
  35.     
  36.     def CheckLargeInt(self):
  37.         num = 0x10000000000L
  38.         self.cur.execute('insert into test(i) values (?)', (num,))
  39.         self.cur.execute('select i from test')
  40.         row = self.cur.fetchone()
  41.         self.failUnlessEqual(row[0], num)
  42.  
  43.     
  44.     def CheckFloat(self):
  45.         val = 3.14
  46.         self.cur.execute('insert into test(f) values (?)', (val,))
  47.         self.cur.execute('select f from test')
  48.         row = self.cur.fetchone()
  49.         self.failUnlessEqual(row[0], val)
  50.  
  51.     
  52.     def CheckBlob(self):
  53.         val = buffer('Guglhupf')
  54.         self.cur.execute('insert into test(b) values (?)', (val,))
  55.         self.cur.execute('select b from test')
  56.         row = self.cur.fetchone()
  57.         self.failUnlessEqual(row[0], val)
  58.  
  59.     
  60.     def CheckUnicodeExecute(self):
  61.         self.cur.execute(u"select '├ûsterreich'")
  62.         row = self.cur.fetchone()
  63.         self.failUnlessEqual(row[0], u'├ûsterreich')
  64.  
  65.  
  66.  
  67. class DeclTypesTests(unittest.TestCase):
  68.     
  69.     class Foo:
  70.         
  71.         def __init__(self, _val):
  72.             self.val = _val
  73.  
  74.         
  75.         def __cmp__(self, other):
  76.             if not isinstance(other, DeclTypesTests.Foo):
  77.                 raise ValueError
  78.             isinstance(other, DeclTypesTests.Foo)
  79.             if self.val == other.val:
  80.                 return 0
  81.             return 1
  82.  
  83.         
  84.         def __conform__(self, protocol):
  85.             if protocol is sqlite.PrepareProtocol:
  86.                 return self.val
  87.             return None
  88.  
  89.         
  90.         def __str__(self):
  91.             return '<%s>' % self.val
  92.  
  93.  
  94.     
  95.     def setUp(self):
  96.         self.con = sqlite.connect(':memory:', detect_types = sqlite.PARSE_DECLTYPES)
  97.         self.cur = self.con.cursor()
  98.         self.cur.execute('create table test(i int, s str, f float, b bool, u unicode, foo foo, bin blob, n1 number, n2 number(5))')
  99.         
  100.         sqlite.converters['FLOAT'] = lambda x: 47.2
  101.         
  102.         sqlite.converters['BOOL'] = lambda x: bool(int(x))
  103.         sqlite.converters['FOO'] = DeclTypesTests.Foo
  104.         
  105.         sqlite.converters['WRONG'] = lambda x: 'WRONG'
  106.         sqlite.converters['NUMBER'] = float
  107.  
  108.     
  109.     def tearDown(self):
  110.         del sqlite.converters['FLOAT']
  111.         del sqlite.converters['BOOL']
  112.         del sqlite.converters['FOO']
  113.         del sqlite.converters['NUMBER']
  114.         self.cur.close()
  115.         self.con.close()
  116.  
  117.     
  118.     def CheckString(self):
  119.         self.cur.execute('insert into test(s) values (?)', ('foo',))
  120.         self.cur.execute('select s as "s [WRONG]" from test')
  121.         row = self.cur.fetchone()
  122.         self.failUnlessEqual(row[0], 'foo')
  123.  
  124.     
  125.     def CheckSmallInt(self):
  126.         self.cur.execute('insert into test(i) values (?)', (42,))
  127.         self.cur.execute('select i from test')
  128.         row = self.cur.fetchone()
  129.         self.failUnlessEqual(row[0], 42)
  130.  
  131.     
  132.     def CheckLargeInt(self):
  133.         num = 0x10000000000L
  134.         self.cur.execute('insert into test(i) values (?)', (num,))
  135.         self.cur.execute('select i from test')
  136.         row = self.cur.fetchone()
  137.         self.failUnlessEqual(row[0], num)
  138.  
  139.     
  140.     def CheckFloat(self):
  141.         val = 3.14
  142.         self.cur.execute('insert into test(f) values (?)', (val,))
  143.         self.cur.execute('select f from test')
  144.         row = self.cur.fetchone()
  145.         self.failUnlessEqual(row[0], 47.2)
  146.  
  147.     
  148.     def CheckBool(self):
  149.         self.cur.execute('insert into test(b) values (?)', (False,))
  150.         self.cur.execute('select b from test')
  151.         row = self.cur.fetchone()
  152.         self.failUnlessEqual(row[0], False)
  153.         self.cur.execute('delete from test')
  154.         self.cur.execute('insert into test(b) values (?)', (True,))
  155.         self.cur.execute('select b from test')
  156.         row = self.cur.fetchone()
  157.         self.failUnlessEqual(row[0], True)
  158.  
  159.     
  160.     def CheckUnicode(self):
  161.         val = u'├ûsterreich'
  162.         self.cur.execute('insert into test(u) values (?)', (val,))
  163.         self.cur.execute('select u from test')
  164.         row = self.cur.fetchone()
  165.         self.failUnlessEqual(row[0], val)
  166.  
  167.     
  168.     def CheckFoo(self):
  169.         val = DeclTypesTests.Foo('bla')
  170.         self.cur.execute('insert into test(foo) values (?)', (val,))
  171.         self.cur.execute('select foo from test')
  172.         row = self.cur.fetchone()
  173.         self.failUnlessEqual(row[0], val)
  174.  
  175.     
  176.     def CheckUnsupportedSeq(self):
  177.         
  178.         class Bar:
  179.             pass
  180.  
  181.         val = Bar()
  182.         
  183.         try:
  184.             self.cur.execute('insert into test(f) values (?)', (val,))
  185.             self.fail('should have raised an InterfaceError')
  186.         except sqlite.InterfaceError:
  187.             pass
  188.         except:
  189.             self.fail('should have raised an InterfaceError')
  190.  
  191.  
  192.     
  193.     def CheckUnsupportedDict(self):
  194.         
  195.         class Bar:
  196.             pass
  197.  
  198.         val = Bar()
  199.         
  200.         try:
  201.             self.cur.execute('insert into test(f) values (:val)', {
  202.                 'val': val })
  203.             self.fail('should have raised an InterfaceError')
  204.         except sqlite.InterfaceError:
  205.             pass
  206.         except:
  207.             self.fail('should have raised an InterfaceError')
  208.  
  209.  
  210.     
  211.     def CheckBlob(self):
  212.         val = buffer('Guglhupf')
  213.         self.cur.execute('insert into test(bin) values (?)', (val,))
  214.         self.cur.execute('select bin from test')
  215.         row = self.cur.fetchone()
  216.         self.failUnlessEqual(row[0], val)
  217.  
  218.     
  219.     def CheckNumber1(self):
  220.         self.cur.execute('insert into test(n1) values (5)')
  221.         value = self.cur.execute('select n1 from test').fetchone()[0]
  222.         self.failUnlessEqual(type(value), float)
  223.  
  224.     
  225.     def CheckNumber2(self):
  226.         self.cur.execute('insert into test(n2) values (5)')
  227.         value = self.cur.execute('select n2 from test').fetchone()[0]
  228.         self.failUnlessEqual(type(value), float)
  229.  
  230.  
  231.  
  232. class ColNamesTests(unittest.TestCase):
  233.     
  234.     def setUp(self):
  235.         self.con = sqlite.connect(':memory:', detect_types = sqlite.PARSE_COLNAMES)
  236.         self.cur = self.con.cursor()
  237.         self.cur.execute('create table test(x foo)')
  238.         
  239.         sqlite.converters['FOO'] = lambda x: '[%s]' % x
  240.         
  241.         sqlite.converters['BAR'] = lambda x: '<%s>' % x
  242.         
  243.         sqlite.converters['EXC'] = lambda x: 5 / 0
  244.         
  245.         sqlite.converters['B1B1'] = lambda x: 'MARKER'
  246.  
  247.     
  248.     def tearDown(self):
  249.         del sqlite.converters['FOO']
  250.         del sqlite.converters['BAR']
  251.         del sqlite.converters['EXC']
  252.         del sqlite.converters['B1B1']
  253.         self.cur.close()
  254.         self.con.close()
  255.  
  256.     
  257.     def CheckDeclTypeNotUsed(self):
  258.         self.cur.execute('insert into test(x) values (?)', ('xxx',))
  259.         self.cur.execute('select x from test')
  260.         val = self.cur.fetchone()[0]
  261.         self.failUnlessEqual(val, 'xxx')
  262.  
  263.     
  264.     def CheckNone(self):
  265.         self.cur.execute('insert into test(x) values (?)', (None,))
  266.         self.cur.execute('select x from test')
  267.         val = self.cur.fetchone()[0]
  268.         self.failUnlessEqual(val, None)
  269.  
  270.     
  271.     def CheckColName(self):
  272.         self.cur.execute('insert into test(x) values (?)', ('xxx',))
  273.         self.cur.execute('select x as "x [bar]" from test')
  274.         val = self.cur.fetchone()[0]
  275.         self.failUnlessEqual(val, '<xxx>')
  276.         self.failUnlessEqual(self.cur.description[0][0], 'x')
  277.  
  278.     
  279.     def CheckCaseInConverterName(self):
  280.         self.cur.execute('select \'other\' as "x [b1b1]"')
  281.         val = self.cur.fetchone()[0]
  282.         self.failUnlessEqual(val, 'MARKER')
  283.  
  284.     
  285.     def CheckCursorDescriptionNoRow(self):
  286.         self.cur.execute('select * from test where 0 = 1')
  287.         self.assert_(self.cur.description[0][0] == 'x')
  288.  
  289.  
  290.  
  291. class ObjectAdaptationTests(unittest.TestCase):
  292.     
  293.     def cast(obj):
  294.         return float(obj)
  295.  
  296.     cast = staticmethod(cast)
  297.     
  298.     def setUp(self):
  299.         self.con = sqlite.connect(':memory:')
  300.         
  301.         try:
  302.             del sqlite.adapters[int]
  303.         except:
  304.             pass
  305.  
  306.         sqlite.register_adapter(int, ObjectAdaptationTests.cast)
  307.         self.cur = self.con.cursor()
  308.  
  309.     
  310.     def tearDown(self):
  311.         del sqlite.adapters[(int, sqlite.PrepareProtocol)]
  312.         self.cur.close()
  313.         self.con.close()
  314.  
  315.     
  316.     def CheckCasterIsUsed(self):
  317.         self.cur.execute('select ?', (4,))
  318.         val = self.cur.fetchone()[0]
  319.         self.failUnlessEqual(type(val), float)
  320.  
  321.  
  322.  
  323. class BinaryConverterTests(unittest.TestCase):
  324.     
  325.     def convert(s):
  326.         return zlib.decompress(s)
  327.  
  328.     convert = staticmethod(convert)
  329.     
  330.     def setUp(self):
  331.         self.con = sqlite.connect(':memory:', detect_types = sqlite.PARSE_COLNAMES)
  332.         sqlite.register_converter('bin', BinaryConverterTests.convert)
  333.  
  334.     
  335.     def tearDown(self):
  336.         self.con.close()
  337.  
  338.     
  339.     def CheckBinaryInputForConverter(self):
  340.         testdata = 'abcdefg' * 10
  341.         result = self.con.execute('select ? as "x [bin]"', (buffer(zlib.compress(testdata)),)).fetchone()[0]
  342.         self.failUnlessEqual(testdata, result)
  343.  
  344.  
  345.  
  346. class DateTimeTests(unittest.TestCase):
  347.     
  348.     def setUp(self):
  349.         self.con = sqlite.connect(':memory:', detect_types = sqlite.PARSE_DECLTYPES)
  350.         self.cur = self.con.cursor()
  351.         self.cur.execute('create table test(d date, ts timestamp)')
  352.  
  353.     
  354.     def tearDown(self):
  355.         self.cur.close()
  356.         self.con.close()
  357.  
  358.     
  359.     def CheckSqliteDate(self):
  360.         d = sqlite.Date(2004, 2, 14)
  361.         self.cur.execute('insert into test(d) values (?)', (d,))
  362.         self.cur.execute('select d from test')
  363.         d2 = self.cur.fetchone()[0]
  364.         self.failUnlessEqual(d, d2)
  365.  
  366.     
  367.     def CheckSqliteTimestamp(self):
  368.         ts = sqlite.Timestamp(2004, 2, 14, 7, 15, 0)
  369.         self.cur.execute('insert into test(ts) values (?)', (ts,))
  370.         self.cur.execute('select ts from test')
  371.         ts2 = self.cur.fetchone()[0]
  372.         self.failUnlessEqual(ts, ts2)
  373.  
  374.     
  375.     def CheckSqlTimestamp(self):
  376.         if sqlite.sqlite_version_info < (3, 1):
  377.             return None
  378.         now = datetime.datetime.now()
  379.         self.cur.execute('insert into test(ts) values (current_timestamp)')
  380.         self.cur.execute('select ts from test')
  381.         ts = self.cur.fetchone()[0]
  382.         self.failUnlessEqual(type(ts), datetime.datetime)
  383.         self.failUnlessEqual(ts.year, now.year)
  384.  
  385.     
  386.     def CheckDateTimeSubSeconds(self):
  387.         ts = sqlite.Timestamp(2004, 2, 14, 7, 15, 0, 500000)
  388.         self.cur.execute('insert into test(ts) values (?)', (ts,))
  389.         self.cur.execute('select ts from test')
  390.         ts2 = self.cur.fetchone()[0]
  391.         self.failUnlessEqual(ts, ts2)
  392.  
  393.     
  394.     def CheckDateTimeSubSecondsFloatingPoint(self):
  395.         ts = sqlite.Timestamp(2004, 2, 14, 7, 15, 0, 510241)
  396.         self.cur.execute('insert into test(ts) values (?)', (ts,))
  397.         self.cur.execute('select ts from test')
  398.         ts2 = self.cur.fetchone()[0]
  399.         self.failUnlessEqual(ts, ts2)
  400.  
  401.  
  402.  
  403. def suite():
  404.     sqlite_type_suite = unittest.makeSuite(SqliteTypeTests, 'Check')
  405.     decltypes_type_suite = unittest.makeSuite(DeclTypesTests, 'Check')
  406.     colnames_type_suite = unittest.makeSuite(ColNamesTests, 'Check')
  407.     adaptation_suite = unittest.makeSuite(ObjectAdaptationTests, 'Check')
  408.     bin_suite = unittest.makeSuite(BinaryConverterTests, 'Check')
  409.     date_suite = unittest.makeSuite(DateTimeTests, 'Check')
  410.     return unittest.TestSuite((sqlite_type_suite, decltypes_type_suite, colnames_type_suite, adaptation_suite, bin_suite, date_suite))
  411.  
  412.  
  413. def test():
  414.     runner = unittest.TextTestRunner()
  415.     runner.run(suite())
  416.  
  417. if __name__ == '__main__':
  418.     test()
  419.  
  420.