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

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import unittest
  5. import sqlite3 as sqlite
  6.  
  7. class MyConnection(sqlite.Connection):
  8.     
  9.     def __init__(self, *args, **kwargs):
  10.         sqlite.Connection.__init__(self, *args, **kwargs)
  11.  
  12.  
  13.  
  14. def dict_factory(cursor, row):
  15.     d = { }
  16.     for idx, col in enumerate(cursor.description):
  17.         d[col[0]] = row[idx]
  18.     
  19.     return d
  20.  
  21.  
  22. class MyCursor(sqlite.Cursor):
  23.     
  24.     def __init__(self, *args, **kwargs):
  25.         sqlite.Cursor.__init__(self, *args, **kwargs)
  26.         self.row_factory = dict_factory
  27.  
  28.  
  29.  
  30. class ConnectionFactoryTests(unittest.TestCase):
  31.     
  32.     def setUp(self):
  33.         self.con = sqlite.connect(':memory:', factory = MyConnection)
  34.  
  35.     
  36.     def tearDown(self):
  37.         self.con.close()
  38.  
  39.     
  40.     def CheckIsInstance(self):
  41.         self.failUnless(isinstance(self.con, MyConnection), 'connection is not instance of MyConnection')
  42.  
  43.  
  44.  
  45. class CursorFactoryTests(unittest.TestCase):
  46.     
  47.     def setUp(self):
  48.         self.con = sqlite.connect(':memory:')
  49.  
  50.     
  51.     def tearDown(self):
  52.         self.con.close()
  53.  
  54.     
  55.     def CheckIsInstance(self):
  56.         cur = self.con.cursor(factory = MyCursor)
  57.         self.failUnless(isinstance(cur, MyCursor), 'cursor is not instance of MyCursor')
  58.  
  59.  
  60.  
  61. class RowFactoryTestsBackwardsCompat(unittest.TestCase):
  62.     
  63.     def setUp(self):
  64.         self.con = sqlite.connect(':memory:')
  65.  
  66.     
  67.     def CheckIsProducedByFactory(self):
  68.         cur = self.con.cursor(factory = MyCursor)
  69.         cur.execute('select 4+5 as foo')
  70.         row = cur.fetchone()
  71.         self.failUnless(isinstance(row, dict), 'row is not instance of dict')
  72.         cur.close()
  73.  
  74.     
  75.     def tearDown(self):
  76.         self.con.close()
  77.  
  78.  
  79.  
  80. class RowFactoryTests(unittest.TestCase):
  81.     
  82.     def setUp(self):
  83.         self.con = sqlite.connect(':memory:')
  84.  
  85.     
  86.     def CheckCustomFactory(self):
  87.         
  88.         self.con.row_factory = lambda cur, row: list(row)
  89.         row = self.con.execute('select 1, 2').fetchone()
  90.         self.failUnless(isinstance(row, list), 'row is not instance of list')
  91.  
  92.     
  93.     def CheckSqliteRowIndex(self):
  94.         self.con.row_factory = sqlite.Row
  95.         row = self.con.execute('select 1 as a, 2 as b').fetchone()
  96.         self.failUnless(isinstance(row, sqlite.Row), 'row is not instance of sqlite.Row')
  97.         col1 = row['a']
  98.         col2 = row['b']
  99.         self.failUnless(col1 == 1, "by name: wrong result for column 'a'")
  100.         self.failUnless(col2 == 2, "by name: wrong result for column 'a'")
  101.         col1 = row['A']
  102.         col2 = row['B']
  103.         self.failUnless(col1 == 1, "by name: wrong result for column 'A'")
  104.         self.failUnless(col2 == 2, "by name: wrong result for column 'B'")
  105.         col1 = row[0]
  106.         col2 = row[1]
  107.         self.failUnless(col1 == 1, 'by index: wrong result for column 0')
  108.         self.failUnless(col2 == 2, 'by index: wrong result for column 1')
  109.  
  110.     
  111.     def CheckSqliteRowIter(self):
  112.         self.con.row_factory = sqlite.Row
  113.         row = self.con.execute('select 1 as a, 2 as b').fetchone()
  114.         for col in row:
  115.             pass
  116.         
  117.  
  118.     
  119.     def CheckSqliteRowAsTuple(self):
  120.         self.con.row_factory = sqlite.Row
  121.         row = self.con.execute('select 1 as a, 2 as b').fetchone()
  122.         t = tuple(row)
  123.  
  124.     
  125.     def CheckSqliteRowAsDict(self):
  126.         self.con.row_factory = sqlite.Row
  127.         row = self.con.execute('select 1 as a, 2 as b').fetchone()
  128.         d = dict(row)
  129.         self.failUnlessEqual(d['a'], row['a'])
  130.         self.failUnlessEqual(d['b'], row['b'])
  131.  
  132.     
  133.     def CheckSqliteRowHashCmp(self):
  134.         self.con.row_factory = sqlite.Row
  135.         row_1 = self.con.execute('select 1 as a, 2 as b').fetchone()
  136.         row_2 = self.con.execute('select 1 as a, 2 as b').fetchone()
  137.         row_3 = self.con.execute('select 1 as a, 3 as b').fetchone()
  138.         self.failUnless(row_1 == row_1)
  139.         self.failUnless(row_1 == row_2)
  140.         self.failUnless(row_2 != row_3)
  141.         self.failIf(row_1 != row_1)
  142.         self.failIf(row_1 != row_2)
  143.         self.failIf(row_2 == row_3)
  144.         self.failUnlessEqual(row_1, row_2)
  145.         self.failUnlessEqual(hash(row_1), hash(row_2))
  146.         self.failIfEqual(row_1, row_3)
  147.         self.failIfEqual(hash(row_1), hash(row_3))
  148.  
  149.     
  150.     def tearDown(self):
  151.         self.con.close()
  152.  
  153.  
  154.  
  155. class TextFactoryTests(unittest.TestCase):
  156.     
  157.     def setUp(self):
  158.         self.con = sqlite.connect(':memory:')
  159.  
  160.     
  161.     def CheckUnicode(self):
  162.         austria = unicode('\xd6sterreich', 'latin1')
  163.         row = self.con.execute('select ?', (austria,)).fetchone()
  164.         self.failUnless(type(row[0]) == unicode, 'type of row[0] must be unicode')
  165.  
  166.     
  167.     def CheckString(self):
  168.         self.con.text_factory = str
  169.         austria = unicode('\xd6sterreich', 'latin1')
  170.         row = self.con.execute('select ?', (austria,)).fetchone()
  171.         self.failUnless(type(row[0]) == str, 'type of row[0] must be str')
  172.         self.failUnless(row[0] == austria.encode('utf-8'), 'column must equal original data in UTF-8')
  173.  
  174.     
  175.     def CheckCustom(self):
  176.         
  177.         self.con.text_factory = lambda x: unicode(x, 'utf-8', 'ignore')
  178.         austria = unicode('\xd6sterreich', 'latin1')
  179.         row = self.con.execute('select ?', (austria.encode('latin1'),)).fetchone()
  180.         self.failUnless(type(row[0]) == unicode, 'type of row[0] must be unicode')
  181.         self.failUnless(row[0].endswith(u'reich'), 'column must contain original data')
  182.  
  183.     
  184.     def CheckOptimizedUnicode(self):
  185.         self.con.text_factory = sqlite.OptimizedUnicode
  186.         austria = unicode('\xd6sterreich', 'latin1')
  187.         germany = unicode('Deutchland')
  188.         a_row = self.con.execute('select ?', (austria,)).fetchone()
  189.         d_row = self.con.execute('select ?', (germany,)).fetchone()
  190.         self.failUnless(type(a_row[0]) == unicode, 'type of non-ASCII row must be unicode')
  191.         self.failUnless(type(d_row[0]) == str, 'type of ASCII-only row must be str')
  192.  
  193.     
  194.     def tearDown(self):
  195.         self.con.close()
  196.  
  197.  
  198.  
  199. def suite():
  200.     connection_suite = unittest.makeSuite(ConnectionFactoryTests, 'Check')
  201.     cursor_suite = unittest.makeSuite(CursorFactoryTests, 'Check')
  202.     row_suite_compat = unittest.makeSuite(RowFactoryTestsBackwardsCompat, 'Check')
  203.     row_suite = unittest.makeSuite(RowFactoryTests, 'Check')
  204.     text_suite = unittest.makeSuite(TextFactoryTests, 'Check')
  205.     return unittest.TestSuite((connection_suite, cursor_suite, row_suite_compat, row_suite, text_suite))
  206.  
  207.  
  208. def test():
  209.     runner = unittest.TextTestRunner()
  210.     runner.run(suite())
  211.  
  212. if __name__ == '__main__':
  213.     test()
  214.  
  215.