Skip to content
This repository has been archived by the owner on Mar 15, 2023. It is now read-only.

Commit

Permalink
[AIRFLOW-XXX] Use mocking in SimpleHttpOperator tests (apache#4144)
Browse files Browse the repository at this point in the history
This changes the tests in [AIRFLOW-3262]/(apache#4135) to use requests_mock
rather than making actual HTTP requests as we have had this test fail on
Travis with connection refused.
  • Loading branch information
ashb authored and wyndhblb committed Nov 9, 2018
1 parent b5f4825 commit b6b2c9a
Showing 1 changed file with 9 additions and 16 deletions.
25 changes: 9 additions & 16 deletions tests/operators/test_http_operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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')

0 comments on commit b6b2c9a

Please sign in to comment.