From b6b2c9a5a43f8439b437da0663f3c7fd2e2b72b8 Mon Sep 17 00:00:00 2001 From: Ash Berlin-Taylor Date: Tue, 6 Nov 2018 14:54:26 +0000 Subject: [PATCH] [AIRFLOW-XXX] Use mocking in SimpleHttpOperator tests (#4144) This changes the tests in [AIRFLOW-3262]/(#4135) to use requests_mock rather than making actual HTTP requests as we have had this test fail on Travis with connection refused. --- tests/operators/test_http_operator.py | 25 +++++++++---------------- 1 file changed, 9 insertions(+), 16 deletions(-) diff --git a/tests/operators/test_http_operator.py b/tests/operators/test_http_operator.py index 5e87d06f28c80..4252867669dd1 100644 --- a/tests/operators/test_http_operator.py +++ b/tests/operators/test_http_operator.py @@ -20,35 +20,28 @@ import os import unittest -from mock import patch +import requests +import requests_mock from airflow.operators.http_operator import SimpleHttpOperator try: from unittest import mock except ImportError: - try: - import mock - except ImportError: - mock = None - - -class AnyStringWith(str): - """ - Helper class to check if a substring is a part of a string - """ - def __eq__(self, other): - return self in other + import mock class SimpleHttpOpTests(unittest.TestCase): def setUp(self): os.environ['AIRFLOW_CONN_HTTP_EXAMPLE'] = 'http://www.example.com' - def test_response_in_logs(self): + @requests_mock.mock() + def test_response_in_logs(self, m): """ Test that when using SimpleHttpOperator with 'GET', the log contains 'Example Domain' in it """ + + m.get('http://www.example.com', text='Example.com fake response') operator = SimpleHttpOperator( task_id='test_HTTP_op', method='GET', @@ -57,6 +50,6 @@ def test_response_in_logs(self): log_response=True, ) - with patch.object(operator.log, 'info') as mock_info: + with mock.patch.object(operator.log, 'info') as mock_info: operator.execute(None) - mock_info.assert_called_with(AnyStringWith('Example Domain')) + mock_info.assert_called_with('Example.com fake response')