How to mock PythonOperator's python_callable and on_failure_callback? - python

What and where do I mock, with an Airflow PythonOperator, such that:
the python_callback raises a exception, triggering the call of the on_failure_callback, and
I can test whether that callback is called, and with what arguments?
I have tried mocking both the {python_callable} and PythonOperator.execute, in a number of places, without success.
The code files look something like this:
dags/my_code.py
class CustomException(Exception): pass
def a_callable():
if OurSqlAlchemyTable.count() == 0:
raise CustomException("{} is empty".format(OurSqlAlchemyTable.name))
return True
def a_failure_callable(context):
SlackWebhookHook(
http_conn_id=slack_conn_id,
message= context['exception'].msg,
channel='#alert-channel'
).execute()
dags/a_dag.py
from my_code import a_callable, a_failure_callable
new_task = PythonOperator(
task_id='new-task', dag=dag-named-sue, conn_id='a_conn_id', timeout=30,
python_callable=a_callable,
on_failure_callback=a_failure_callable)
dags/test_a_dag.py
class TestCallback(unittest.TestCase):
def test_on_failure_callback(self):
tested_task = DagBag().get_dag('dag-named-sue').get_task('new-task')
with patch('airflow.operators.python_operator.PythonOperator.execute') as mock_execute:
with patch('dags.a_dag.a_failure_callable') as mock_callback:
mock_execute.side_effect = CustomException
tested_task.execute(context={})
# does failure of the python_callable trigger the failure callback?
mock_callback.assert_called()
# did the exception message make it to the failure callback?
failure_context = mock_callback.call_args[0]
self.assertEqual(failure_context['exception'].msg,
'OurSqlAlchemyTable is empty')O
The test does raise CustomException at the line self.task.execute(context={}) -- but, in the test code itself. What I want is for that error to be
raised in the Airflow code such that the PythonOperator fails and calls on_failure_callback.
I have tried any number of permutations, all either raising in test without
triggering, calling the python_callable, or not finding an object to patch:
patch('dags.a_dag.a_callable') as mock_callable
'a_dag.a_callable'
'dags.my_code.a_callable'
'my_code.a_callable'
'airflow.models.Task.execute'
(Python3, pytest, and mock.)
What am I missing / doing wrong?
(Better still, I would like to verify the arguments passed to SlackWebhookHook. Something like:
with patch('???.SlackWebhookHook.execute') as mock_webhook:
... as above ...
kw_dict = mock_webhook.call_args[-1]
assert kw_dict['http_conn_id'] == slack_conn_id
assert kw_dict['message'] == 'OurSqlAlchemyTable is empty'
assert kw_dict['channel'] == '#alert-channel'
(But I am first focussing on testing the failure callback.)
Thank you in advance.

Related

Python Mock: Raising Error for function of Mocked Class

I'm trying to test some code that create or changes directories and files based on some inputs. My issue is raising exceptions when a method of a mocked object is called. For example, say I have some code:
def create_if_not_exists(dest):
dir = os.path.dirname(dest)
if not dir:
# create if it doesn't exist
try:
os.makedirs(os.path.dirname(dest))
except OSError:
pass # Nevermind what goes here, it's unimportant to the question
and a unit test:
#patch('my.package.os')
def test_create_dir_if_not_exists(self, mock_os):
mock_os.path.dirname.return_value = None
mock_os.makedirs.raiseError.side_effect = OSError()
with self.assertRaises(OSError)
create_if_not_exists('test')
This setup returns AssertionError: OSError not raised but my understanding is it should raise the error when the makedirs call is made in the actual (non-test) method. Is my understanding incorrect?
Try with the following patch in the test file:
#patch('my.package.os.path.dirname')
#patch('my.package.os.makedirs')
def test_create_dir_if_not_exists(self, mock_os_makedirs, mock_os_path_dirname):
mock_os_path_dirname.return_value = None
mock_os_makedirs.raiseError.side_effect = OSError()
with self.assertRaises(OSError)
create_if_not_exists('test')

Assertion Error when logging.exception(error)

I have this function in a script called mymodule.py
import logging
def foo():
try:
raise ConnectionError('My Connection Error')
except ConnectionError as ce:
logging.exception(ce)
And I have the test for it called test_mymodule.py:
import unittest
import unittest.mock as um
import mymodule
class TestLoggingException(unittest.TestCase):
#um.patch('mymodule.logging')
def test_connection_error_correctly_logged_without_raising(self, mock_logging):
mymodule.foo()
mock_logging.assert_has_calls(
[um.call(ConnectionError('My Connection Error'))]
)
However, when running test_mymodule.py, the below assertion error is raised.
AssertionError: Calls not found.
Expected: [call(ConnectionError('My Connection Error'))]
Actual: [call(ConnectionError('My Connection Error'))]
Why is it thinking they are different and how could I work around this?
The problem is that two instances of ConnectionError, even if create with the same arguments, are not equal.
You create two instances in your code : in foo and in the um.call().
However, those two instance are not the same, and are therefore not equal. You can illustrate that simply:
>>> ConnectionError("test") == ConnectionError("test")
False
One solution is to check which calls were made to the mockup. The calls are exposed through a variable called mockup_calls.
Something like this
class TestLoggingException(unittest.TestCase):
#um.patch('mymodule.logging')
def test_connection_error_correctly_logged_without_raising(self, mock_logging):
mymodule.foo()
print("Calls are: ", mock_logging.mock_calls)
# Check that logging was called with logging.exception(ConnectionError("My Connection Error"))
calls = mock_logging.mock_calls
assert(len(calls) == 1)
# Unpack call
function_called, args, kwargs = calls[0]
assert(function_called == "exception")
connection_error = args[0]
assert(isinstance(connection_error, ConnectionError))
assert(connection_error.args[0] == "My Connection Error") # This will depend on how ConnectionError is defined
What is tricky about this example is that it would work with types that evaluate equal even if they are not the same, like str("hi" == "hi" will yield True), but not most classes.
Does it help ?

Assert exception message?

I'm using pytest in a project with a good many custom exceptions.
pytest provides a handy syntax for checking that an exception has been raised, however, I'm not aware of one that asserts that the correct exception message has been raised.
Say I had a CustomException that prints "boo!", how could I assert that "boo!" has indeed been printed and not, say, "<unprintable CustomException object>"?
#errors.py
class CustomException(Exception):
def __str__(self): return "ouch!"
#test.py
import pytest, myModule
def test_custom_error(): # SHOULD FAIL
with pytest.raises(myModule.CustomException):
raise myModule.CustomException == "boo!"
I think what you're looking for is:
def failer():
raise myModule.CustomException()
def test_failer():
with pytest.raises(myModule.CustomException) as excinfo:
failer()
assert str(excinfo.value) == "boo!"
You can use match keyword in raises. Try something like
with pytest.raises(
RuntimeError, match=<error string here>
):
pass

Mocked unit test raises a "stop called on unstarted patcher" error

When running the test bellow, I got a stop called on unstarted patcher.
def test_get_subvention_internal_no_triggered_admission(self):
billing_cluster = BillingClusterFactory()
subvention = SubventionFactory(billing_cluster=billing_cluster)
convive_sub = ConviveFactory(subvention=subvention, billing_cluster=billing_cluster)
order_5 = OrderFactory(beneficiary=convive_sub)
order_operation_5 = CreationOrderOperationFactory(order=order_5)
with patch('orders.models.Order.subvention_triggered_same_day', return_value=True):
with patch('builtins.hasattr', return_value=False):
self.assertIsNone(order_operation_5._get_subvention())
I read stuff about this error on stack overflow, and concluded that I should avoid mocking the same stuff (stacked mocks). But it's not what I'm doing here. I'm nesting mocks, and it seems to be ok.
If I invert the return values (first mock returns False, second returns True), the test works well.
Any idea?
Thanks.
In short, you cannot patch the builtins func hasattr
patch('builtins.hasattr', return_value=False)
reason: used by mock.py
if not _is_started(self):
raise RuntimeError('stop called on unstarted patcher')
def _is_started(patcher):
# XXXX horrible
return hasattr(patcher, 'is_local')
to repeat the error:
#mock.patch('__builtin__.hasattr')
def test_mock_hasattr(self, mocked_hasattr):
# as long as it is set to False, it will trigger
mocked_hasattr.return_value = False
to mock a builtins func inside models.py:
# narrow the mock scope
#mock.patch('orders.models.hasattr')

How to throw exception from mocked instance's method?

This demo function I want to test is pretty straight forward.
def is_email_deliverable(email):
try:
return external.verify(email)
except Exception:
logger.error("External failed failed")
return False
This function uses an external service which I want to mock out.
But I can't figure out how to throw an exception from external.verify(email) i.e. how to force the except clause to be executed.
My attempt:
#patch.object(other_module, 'external')
def test_is_email_deliverable(patched_external):
def my_side_effect(email):
raise Exception("Test")
patched_external.verify.side_effects = my_side_effect
# Or,
# patched_external.verify.side_effects = Exception("Test")
# Or,
# patched_external.verify.side_effects = Mock(side_effect=Exception("Test"))
assert is_email_deliverable("some_mail#domain.com") == False
This question claims to have the answer, but didn't work for me.
You have used side_effects instead of side_effect.
Its something like this
#patch.object(Class, "attribute")
def foo(attribute):
attribute.side_effect = Exception()
# Other things can go here
BTW, its not good approach to catch all the Exception and handle according to it.
You can set the side_effect value to None.

Categories

Resources