/Lib/sqlite3/test/transactions.py

http://unladen-swallow.googlecode.com/ · Python · 185 lines · 129 code · 25 blank · 31 comment · 11 complexity · cfafc2da7556061f81642f61b126c4a5 MD5 · raw file

  1. #-*- coding: ISO-8859-1 -*-
  2. # pysqlite2/test/transactions.py: tests transactions
  3. #
  4. # Copyright (C) 2005-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 sys
  24. import os, unittest
  25. import sqlite3 as sqlite
  26. def get_db_path():
  27. return "sqlite_testdb"
  28. class TransactionTests(unittest.TestCase):
  29. def setUp(self):
  30. try:
  31. os.remove(get_db_path())
  32. except OSError:
  33. pass
  34. self.con1 = sqlite.connect(get_db_path(), timeout=0.1)
  35. self.cur1 = self.con1.cursor()
  36. self.con2 = sqlite.connect(get_db_path(), timeout=0.1)
  37. self.cur2 = self.con2.cursor()
  38. def tearDown(self):
  39. self.cur1.close()
  40. self.con1.close()
  41. self.cur2.close()
  42. self.con2.close()
  43. try:
  44. os.unlink(get_db_path())
  45. except OSError:
  46. pass
  47. def CheckDMLdoesAutoCommitBefore(self):
  48. self.cur1.execute("create table test(i)")
  49. self.cur1.execute("insert into test(i) values (5)")
  50. self.cur1.execute("create table test2(j)")
  51. self.cur2.execute("select i from test")
  52. res = self.cur2.fetchall()
  53. self.failUnlessEqual(len(res), 1)
  54. def CheckInsertStartsTransaction(self):
  55. self.cur1.execute("create table test(i)")
  56. self.cur1.execute("insert into test(i) values (5)")
  57. self.cur2.execute("select i from test")
  58. res = self.cur2.fetchall()
  59. self.failUnlessEqual(len(res), 0)
  60. def CheckUpdateStartsTransaction(self):
  61. self.cur1.execute("create table test(i)")
  62. self.cur1.execute("insert into test(i) values (5)")
  63. self.con1.commit()
  64. self.cur1.execute("update test set i=6")
  65. self.cur2.execute("select i from test")
  66. res = self.cur2.fetchone()[0]
  67. self.failUnlessEqual(res, 5)
  68. def CheckDeleteStartsTransaction(self):
  69. self.cur1.execute("create table test(i)")
  70. self.cur1.execute("insert into test(i) values (5)")
  71. self.con1.commit()
  72. self.cur1.execute("delete from test")
  73. self.cur2.execute("select i from test")
  74. res = self.cur2.fetchall()
  75. self.failUnlessEqual(len(res), 1)
  76. def CheckReplaceStartsTransaction(self):
  77. self.cur1.execute("create table test(i)")
  78. self.cur1.execute("insert into test(i) values (5)")
  79. self.con1.commit()
  80. self.cur1.execute("replace into test(i) values (6)")
  81. self.cur2.execute("select i from test")
  82. res = self.cur2.fetchall()
  83. self.failUnlessEqual(len(res), 1)
  84. self.failUnlessEqual(res[0][0], 5)
  85. def CheckToggleAutoCommit(self):
  86. self.cur1.execute("create table test(i)")
  87. self.cur1.execute("insert into test(i) values (5)")
  88. self.con1.isolation_level = None
  89. self.failUnlessEqual(self.con1.isolation_level, None)
  90. self.cur2.execute("select i from test")
  91. res = self.cur2.fetchall()
  92. self.failUnlessEqual(len(res), 1)
  93. self.con1.isolation_level = "DEFERRED"
  94. self.failUnlessEqual(self.con1.isolation_level , "DEFERRED")
  95. self.cur1.execute("insert into test(i) values (5)")
  96. self.cur2.execute("select i from test")
  97. res = self.cur2.fetchall()
  98. self.failUnlessEqual(len(res), 1)
  99. def CheckRaiseTimeout(self):
  100. if sqlite.sqlite_version_info < (3, 2, 2):
  101. # This will fail (hang) on earlier versions of sqlite.
  102. # Determine exact version it was fixed. 3.2.1 hangs.
  103. return
  104. self.cur1.execute("create table test(i)")
  105. self.cur1.execute("insert into test(i) values (5)")
  106. try:
  107. self.cur2.execute("insert into test(i) values (5)")
  108. self.fail("should have raised an OperationalError")
  109. except sqlite.OperationalError:
  110. pass
  111. except:
  112. self.fail("should have raised an OperationalError")
  113. def CheckLocking(self):
  114. """
  115. This tests the improved concurrency with pysqlite 2.3.4. You needed
  116. to roll back con2 before you could commit con1.
  117. """
  118. if sqlite.sqlite_version_info < (3, 2, 2):
  119. # This will fail (hang) on earlier versions of sqlite.
  120. # Determine exact version it was fixed. 3.2.1 hangs.
  121. return
  122. self.cur1.execute("create table test(i)")
  123. self.cur1.execute("insert into test(i) values (5)")
  124. try:
  125. self.cur2.execute("insert into test(i) values (5)")
  126. self.fail("should have raised an OperationalError")
  127. except sqlite.OperationalError:
  128. pass
  129. except:
  130. self.fail("should have raised an OperationalError")
  131. # NO self.con2.rollback() HERE!!!
  132. self.con1.commit()
  133. class SpecialCommandTests(unittest.TestCase):
  134. def setUp(self):
  135. self.con = sqlite.connect(":memory:")
  136. self.cur = self.con.cursor()
  137. def CheckVacuum(self):
  138. self.cur.execute("create table test(i)")
  139. self.cur.execute("insert into test(i) values (5)")
  140. self.cur.execute("vacuum")
  141. def CheckDropTable(self):
  142. self.cur.execute("create table test(i)")
  143. self.cur.execute("insert into test(i) values (5)")
  144. self.cur.execute("drop table test")
  145. def CheckPragma(self):
  146. self.cur.execute("create table test(i)")
  147. self.cur.execute("insert into test(i) values (5)")
  148. self.cur.execute("pragma count_changes=1")
  149. def tearDown(self):
  150. self.cur.close()
  151. self.con.close()
  152. def suite():
  153. default_suite = unittest.makeSuite(TransactionTests, "Check")
  154. special_command_suite = unittest.makeSuite(SpecialCommandTests, "Check")
  155. return unittest.TestSuite((default_suite, special_command_suite))
  156. def test():
  157. runner = unittest.TextTestRunner()
  158. runner.run(suite())
  159. if __name__ == "__main__":
  160. test()