def test_recursive_transaction(self): """test recursive transaction""" table = self._create_table(('name VARCHAR(10)', ), None) connection_holder = Connection(self._context.make_connection()) query = transaction(self.get_insert_query(table)) connection_holder.commit = self.count_calls(connection_holder.commit) connection_holder.commit.call_count = 0 self._context.wait(connection_holder.execute(transaction(query))) self.assertEqual(1, connection_holder.commit.call_count)
def test_recursive_transaction(self): """test recursive transaction""" table = self._create_table(('name VARCHAR(10)',), None) connection_holder = Connection(self._context.make_connection()) query = transaction(self.get_insert_query(table)) connection_holder.commit = self.count_calls(connection_holder.commit) connection_holder.commit.call_count = 0 self._context.wait(connection_holder.execute(transaction(query))) self.assertEqual(1, connection_holder.commit.call_count)
def test_commit(self): """test transaction commit""" table = self._create_table(('name VARCHAR(10)',), None) connection = Connection(self._context.make_connection()) query = self.get_insert_query(table) self.assertIs(self._context.wait(connection.execute(transaction(query))), True) self._context.wait(connection.rollback()) cursor = self._context.cursor() self._context.wait(cursor.execute("SELECT * FROM %s" % table)) self.assertEqual([('Evelina',)], cursor.fetchall())
def test_commit(self): """test transaction commit""" table = self._create_table(('name VARCHAR(10)', ), None) connection = Connection(self._context.make_connection()) query = self.get_insert_query(table) self.assertIs( self._context.wait(connection.execute(transaction(query))), True) self._context.wait(connection.rollback()) cursor = self._context.cursor() self._context.wait(cursor.execute("SELECT * FROM %s" % table)) self.assertEqual([('Evelina', )], cursor.fetchall())
def test_transaction_rollback(self): """test transaction rollback""" table = self._create_table(('name VARCHAR(10)', ), None) connection_holder = Connection(self._context.make_connection()) query = self.get_insert_query(table, Exception('rollback expected!')) self.assertRaisesRegex(Exception, 'rollback expected!', self._context.call_and_wait, connection_holder.execute, transaction(query)) cursor = self._context.cursor() self._context.wait(cursor.execute("SELECT * FROM %s" % table)) self.assertEqual([], cursor.fetchall())
def test_cluster(self): """ test commutator """ read_connection = Connection(self._context.make_connection()) write_connection = Connection(self._context.make_connection()) read_connection.readonly = True write_connection.readonly = False read_query = self.wrap_query(lambda x: self.assertTrue(x.readonly)) write_query = transaction(self.wrap_query(lambda x: self.assertFalse(x.readonly))) cluster = Cluster(master=write_connection, slave=read_connection) self._context.wait(cluster.execute(read_query)) self._context.wait(cluster.execute(write_query)) cluster = Cluster(master=None, slave=read_connection) self.assertRaisesRegex(Exception, "the operation is not permitted on read-only cluster", cluster.execute, write_query)
def test_cluster(self): """ test commutator """ read_connection = Connection(self._context.make_connection()) write_connection = Connection(self._context.make_connection()) read_connection.readonly = True write_connection.readonly = False read_query = self.wrap_query(lambda x: self.assertTrue(x.readonly)) write_query = transaction( self.wrap_query(lambda x: self.assertFalse(x.readonly))) cluster = Cluster(master=write_connection, slave=read_connection) self._context.wait(cluster.execute(read_query)) self._context.wait(cluster.execute(write_query)) cluster = Cluster(master=None, slave=read_connection) self.assertRaisesRegex( Exception, "the operation is not permitted on read-only cluster", cluster.execute, write_query)
def test_transaction_rollback(self): """test transaction rollback""" table = self._create_table(('name VARCHAR(10)',), None) connection_holder = Connection(self._context.make_connection()) query = self.get_insert_query(table, Exception('rollback expected!')) self.assertRaisesRegex(Exception, 'rollback expected!', self._context.call_and_wait, connection_holder.execute, transaction(query)) cursor = self._context.cursor() self._context.wait(cursor.execute("SELECT * FROM %s" % table)) self.assertEqual([], cursor.fetchall())