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)
def from_excel(cls, path, worksheet=0): new_cls = cls.__new__(cls) new_cls._connection = DEFAULT_CONNECTION cursor = new_cls._connection.cursor() with savepoint(cursor): table = new_table_name(cursor) reader = get_reader.from_excel(path, worksheet=0) load_data(cursor, table, reader) new_cls._table = table if table_exists(cursor, table) else None new_cls._data = path new_cls._args = tuple() new_cls._kwds = dict() if worksheet != 0: new_cls._kwds['worksheet'] = worksheet new_cls._update_list = [] return new_cls
def from_csv(cls, file, encoding=None, **fmtparams): if isinstance(file, string_types) or isinstance(file, file_types): data_list = [file] else: data_list = file new_cls = cls.__new__(cls) new_cls._connection = DEFAULT_CONNECTION cursor = new_cls._connection.cursor() with savepoint(cursor): table = new_table_name(cursor) for obj in data_list: load_csv(cursor, table, obj, encoding=encoding, **fmtparams) new_cls._table = table if table_exists(cursor, table) else None new_cls._data = file new_cls._args = (encoding, ) new_cls._kwds = fmtparams new_cls._update_list = [] return new_cls