Exemplo n.º 1
0
    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',)])
Exemplo n.º 2
0
    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',)])
Exemplo n.º 3
0
    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.')
Exemplo n.º 4
0
    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
Exemplo n.º 5
0
    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)