/Lib/sqlite3/test/regression.py

http://unladen-swallow.googlecode.com/ · Python · 180 lines · 110 code · 16 blank · 54 comment · 12 complexity · 0a38ea77d9d2425a209ba6ebc2863f25 MD5 · raw file

  1. #-*- coding: ISO-8859-1 -*-
  2. # pysqlite2/test/regression.py: pysqlite regression tests
  3. #
  4. # Copyright (C) 2006-2007 Gerhard Häring <gh@ghaering.de>
  5. #
  6. # This file is part of pysqlite.
  7. #
  8. # This software is provided 'as-is', without any express or implied
  9. # warranty. In no event will the authors be held liable for any damages
  10. # arising from the use of this software.
  11. #
  12. # Permission is granted to anyone to use this software for any purpose,
  13. # including commercial applications, and to alter it and redistribute it
  14. # freely, subject to the following restrictions:
  15. #
  16. # 1. The origin of this software must not be misrepresented; you must not
  17. # claim that you wrote the original software. If you use this software
  18. # in a product, an acknowledgment in the product documentation would be
  19. # appreciated but is not required.
  20. # 2. Altered source versions must be plainly marked as such, and must not be
  21. # misrepresented as being the original software.
  22. # 3. This notice may not be removed or altered from any source distribution.
  23. import datetime
  24. import unittest
  25. import sqlite3 as sqlite
  26. class RegressionTests(unittest.TestCase):
  27. def setUp(self):
  28. self.con = sqlite.connect(":memory:")
  29. def tearDown(self):
  30. self.con.close()
  31. def CheckPragmaUserVersion(self):
  32. # This used to crash pysqlite because this pragma command returns NULL for the column name
  33. cur = self.con.cursor()
  34. cur.execute("pragma user_version")
  35. def CheckPragmaSchemaVersion(self):
  36. # This still crashed pysqlite <= 2.2.1
  37. con = sqlite.connect(":memory:", detect_types=sqlite.PARSE_COLNAMES)
  38. try:
  39. cur = self.con.cursor()
  40. cur.execute("pragma schema_version")
  41. finally:
  42. cur.close()
  43. con.close()
  44. def CheckStatementReset(self):
  45. # pysqlite 2.1.0 to 2.2.0 have the problem that not all statements are
  46. # reset before a rollback, but only those that are still in the
  47. # statement cache. The others are not accessible from the connection object.
  48. con = sqlite.connect(":memory:", cached_statements=5)
  49. cursors = [con.cursor() for x in xrange(5)]
  50. cursors[0].execute("create table test(x)")
  51. for i in range(10):
  52. cursors[0].executemany("insert into test(x) values (?)", [(x,) for x in xrange(10)])
  53. for i in range(5):
  54. cursors[i].execute(" " * i + "select x from test")
  55. con.rollback()
  56. def CheckColumnNameWithSpaces(self):
  57. cur = self.con.cursor()
  58. cur.execute('select 1 as "foo bar [datetime]"')
  59. self.failUnlessEqual(cur.description[0][0], "foo bar")
  60. cur.execute('select 1 as "foo baz"')
  61. self.failUnlessEqual(cur.description[0][0], "foo baz")
  62. def CheckStatementAvailable(self):
  63. # pysqlite up to 2.3.2 crashed on this, because the active statement handle was not checked
  64. # before trying to fetch data from it. close() destroys the active statement ...
  65. con = sqlite.connect(":memory:", detect_types=sqlite.PARSE_DECLTYPES)
  66. cur = con.cursor()
  67. cur.execute("select 4 union select 5")
  68. cur.close()
  69. cur.fetchone()
  70. cur.fetchone()
  71. def CheckStatementFinalizationOnCloseDb(self):
  72. # pysqlite versions <= 2.3.3 only finalized statements in the statement
  73. # cache when closing the database. statements that were still
  74. # referenced in cursors weren't closed an could provoke "
  75. # "OperationalError: Unable to close due to unfinalised statements".
  76. con = sqlite.connect(":memory:")
  77. cursors = []
  78. # default statement cache size is 100
  79. for i in range(105):
  80. cur = con.cursor()
  81. cursors.append(cur)
  82. cur.execute("select 1 x union select " + str(i))
  83. con.close()
  84. def CheckOnConflictRollback(self):
  85. if sqlite.sqlite_version_info < (3, 2, 2):
  86. return
  87. con = sqlite.connect(":memory:")
  88. con.execute("create table foo(x, unique(x) on conflict rollback)")
  89. con.execute("insert into foo(x) values (1)")
  90. try:
  91. con.execute("insert into foo(x) values (1)")
  92. except sqlite.DatabaseError:
  93. pass
  94. con.execute("insert into foo(x) values (2)")
  95. try:
  96. con.commit()
  97. except sqlite.OperationalError:
  98. self.fail("pysqlite knew nothing about the implicit ROLLBACK")
  99. def CheckWorkaroundForBuggySqliteTransferBindings(self):
  100. """
  101. pysqlite would crash with older SQLite versions unless
  102. a workaround is implemented.
  103. """
  104. self.con.execute("create table foo(bar)")
  105. self.con.execute("drop table foo")
  106. self.con.execute("create table foo(bar)")
  107. def CheckEmptyStatement(self):
  108. """
  109. pysqlite used to segfault with SQLite versions 3.5.x. These return NULL
  110. for "no-operation" statements
  111. """
  112. self.con.execute("")
  113. def CheckUnicodeConnect(self):
  114. """
  115. With pysqlite 2.4.0 you needed to use a string or a APSW connection
  116. object for opening database connections.
  117. Formerly, both bytestrings and unicode strings used to work.
  118. Let's make sure unicode strings work in the future.
  119. """
  120. con = sqlite.connect(u":memory:")
  121. con.close()
  122. def CheckTypeMapUsage(self):
  123. """
  124. pysqlite until 2.4.1 did not rebuild the row_cast_map when recompiling
  125. a statement. This test exhibits the problem.
  126. """
  127. SELECT = "select * from foo"
  128. con = sqlite.connect(":memory:",detect_types=sqlite.PARSE_DECLTYPES)
  129. con.execute("create table foo(bar timestamp)")
  130. con.execute("insert into foo(bar) values (?)", (datetime.datetime.now(),))
  131. con.execute(SELECT)
  132. con.execute("drop table foo")
  133. con.execute("create table foo(bar integer)")
  134. con.execute("insert into foo(bar) values (5)")
  135. con.execute(SELECT)
  136. def CheckRegisterAdapter(self):
  137. """
  138. See issue 3312.
  139. """
  140. self.assertRaises(TypeError, sqlite.register_adapter, {}, None)
  141. def CheckSetIsolationLevel(self):
  142. """
  143. See issue 3312.
  144. """
  145. con = sqlite.connect(":memory:")
  146. self.assertRaises(UnicodeEncodeError, setattr, con,
  147. "isolation_level", u"\xe9")
  148. def suite():
  149. regression_suite = unittest.makeSuite(RegressionTests, "Check")
  150. return unittest.TestSuite((regression_suite,))
  151. def test():
  152. runner = unittest.TextTestRunner()
  153. runner.run(suite())
  154. if __name__ == "__main__":
  155. test()