def test_nested_releases(self): cursor = self.cursor with savepoint(cursor): cursor.execute('CREATE TEMPORARY TABLE test_table ("A")') cursor.execute("INSERT INTO test_table VALUES ('one')") with savepoint(cursor): # <- Nested! cursor.execute("INSERT INTO test_table VALUES ('two')") cursor.execute("INSERT INTO test_table VALUES ('three')") cursor.execute('SELECT * FROM test_table') self.assertEqual(cursor.fetchall(), [('one',), ('two',), ('three',)])
def test_nested_rollback(self): cursor = self.cursor with savepoint(cursor): # <- Released. cursor.execute('CREATE TEMPORARY TABLE test_table ("A")') cursor.execute("INSERT INTO test_table VALUES ('one')") try: with savepoint(cursor): # <- Nested rollback! cursor.execute("INSERT INTO test_table VALUES ('two')") raise Exception() except Exception: pass cursor.execute("INSERT INTO test_table VALUES ('three')") cursor.execute('SELECT * FROM test_table') self.assertEqual(cursor.fetchall(), [('one',), ('three',)])
def test_rollback(self): cursor = self.cursor with savepoint(cursor): # <- Released. cursor.execute('CREATE TEMPORARY TABLE test_table ("A")') try: with savepoint(cursor): # <- Rolled back! cursor.execute("INSERT INTO test_table VALUES ('one')") cursor.execute("INSERT INTO test_table VALUES ('two')") cursor.execute("INSERT INTO missing_table VALUES ('three')") # <- Bad table. except sqlite3.OperationalError: pass cursor.execute('SELECT * FROM test_table') self.assertEqual(cursor.fetchall(), [], 'Table should exist but contain no records.')
def test_bad_isolation_level(self): connection = sqlite3.connect(':memory:') connection.isolation_level = 'DEFERRED' # <- Expects None/autocommit! cursor = connection.cursor() with self.assertRaises(ValueError): with savepoint(cursor): pass
def test_transaction_status(self): connection = self.cursor.connection if not hasattr(connection, 'in_transaction'): # New in 3.2. return self.assertFalse(connection.in_transaction) with savepoint(self.cursor): self.assertTrue(connection.in_transaction) self.assertFalse(connection.in_transaction)