def test_callbacks_properly_execute_callables_and_tuples(self): call_history = [] def my_callback(*args, **kwargs): call_history.append(args) # adding on init: batch = Batch(self.conn) batch.add_callback(my_callback) batch.add_callback(my_callback, 'more', 'args') batch.execute_batch() self.assertEqual(len(call_history), 2) self.assertEqual([(), ('more', 'args')], call_history)
def test_callbacks_work_multiple_times(self): """ Tests that multiple executions of execute on a batch statement logs a warning, and that we don't encounter an attribute error. @since 3.1 @jira_ticket PYTHON-445 @expected_result warning message is logged @test_category object_mapper """ call_history = [] def my_callback(*args, **kwargs): call_history.append(args) with warnings.catch_warnings(record=True) as w: batch = Batch(self.conn) batch.add_callback(my_callback) batch.execute_batch() batch.execute_batch() self.assertEqual(len(w), 1) self.assertRegexpMatches(str(w[0].message), r"^Batch.*multiple.*")
def test_API_managing_callbacks(self): # Callbacks can be added at init and after def my_callback(*args, **kwargs): pass # adding on init: batch = Batch(self.conn) batch.add_callback(my_callback) batch.add_callback(my_callback, 2, named_arg='value') batch.add_callback(my_callback, 1, 3) self.assertEqual(batch._callbacks, [(my_callback, (), {}), (my_callback, (2, ), { 'named_arg': 'value' }), (my_callback, (1, 3), {})])
def test_callbacks_tied_to_execute(self): """Batch callbacks should NOT fire if batch is not executed in context manager mode""" call_history = [] def my_callback(*args, **kwargs): call_history.append(args) b_conn = Batch(self.conn) b_conn.add_callback(my_callback) b_conn.execute_batch() self.assertEqual(len(call_history), 1) class SomeError(Exception): pass with self.assertRaises(SomeError): with Batch(self.conn) as b_conn: b_conn.add_callback(my_callback) # this error bubbling up through context manager # should prevent callback runs (along with b.execute()) raise SomeError # still same call history. Nothing added self.assertEqual(len(call_history), 1) # but if execute ran, even with an error bubbling through # the callbacks also would have fired with self.assertRaises(SomeError): with Batch(self.conn, execute_on_exception=True) as b_conn: b_conn.add_callback(my_callback) raise SomeError # updated call history self.assertEqual(len(call_history), 2)