def execute(self, **kwargs): """ The SlackAPIOperator calls will not fail even if the call is not unsuccessful. It should not prevent a DAG from completing in success """ slack = SlackHook(token=self.token, slack_conn_id=self.slack_conn_id) # If file content is passed. if self.content is not None: self.api_params = { 'channels': self.channel, 'content': self.content, 'initial_comment': self.initial_comment, } slack.call(self.method, data=self.api_params) # If file name is passed. elif self.filename is not None: self.api_params = { 'channels': self.channel, 'filename': self.filename, 'filetype': self.filetype, 'initial_comment': self.initial_comment, } with open(self.filename, "rb") as file_handle: slack.call(self.method, data=self.api_params, files={'file': file_handle}) file_handle.close()
def test_api_call(self, mock_slack_client, mock_slack_api_call): slack_hook = SlackHook(token='test_token') test_api_json = {'channel': 'test_channel'} slack_hook.call("chat.postMessage", json=test_api_json) mock_slack_api_call.assert_called_once_with( mock_slack_client, "chat.postMessage", json=test_api_json)
def execute(self, **kwargs): # noqa: D403 """ SlackAPIOperator calls will not fail even if the call is not unsuccessful. It should not prevent a DAG from completing in success """ if not self.api_params: self.construct_api_call_params() slack = SlackHook(token=self.token, slack_conn_id=self.slack_conn_id) slack.call(self.method, json=self.api_params)
def test_call_with_failure(self, slack_client_class_mock): slack_client_mock = mock.Mock() slack_client_class_mock.return_value = slack_client_mock expected_exception = SlackApiError(message='foo', response='bar') slack_client_mock.api_call = mock.Mock(side_effect=expected_exception) test_token = 'test_token' test_slack_conn_id = 'test_slack_conn_id' slack_hook = SlackHook(token=test_token, slack_conn_id=test_slack_conn_id) test_method = 'test_method' test_api_params = {'key1': 'value1', 'key2': 'value2'} with self.assertRaises(SlackApiError): slack_hook.call(test_method, test_api_params)
def test_call_with_success(self, slack_client_class_mock): slack_client_mock = mock.Mock() slack_client_class_mock.return_value = slack_client_mock slack_client_mock.api_call.return_value = {'ok': True} test_token = 'test_token' test_slack_conn_id = 'test_slack_conn_id' slack_hook = SlackHook(token=test_token, slack_conn_id=test_slack_conn_id) test_method = 'test_method' test_api_params = {'key1': 'value1', 'key2': 'value2'} slack_hook.call(test_method, test_api_params) slack_client_class_mock.assert_called_once_with(test_token) slack_client_mock.api_call.assert_called_once_with(test_method, **test_api_params)
def test_call_with_success(self, slack_client_class_mock): slack_client_mock = mock.Mock() slack_client_class_mock.return_value = slack_client_mock slack_response = mock.Mock() slack_client_mock.api_call.return_value = slack_response slack_response.validate.return_value = True test_token = 'test_token' test_slack_conn_id = 'test_slack_conn_id' slack_hook = SlackHook(token=test_token, slack_conn_id=test_slack_conn_id) test_method = 'test_method' test_api_params = {'key1': 'value1', 'key2': 'value2'} slack_hook.call(test_method, json=test_api_params) slack_client_class_mock.assert_called_once_with(test_token) slack_client_mock.api_call.assert_called_once_with( test_method, json=test_api_params) self.assertEqual(slack_response.validate.call_count, 1)
def test_call_with_failure(self, slack_client_class_mock): slack_client_mock = mock.Mock() slack_client_class_mock.return_value = slack_client_mock slack_response = mock.Mock() slack_client_mock.api_call.return_value = slack_response expected_exception = SlackApiError(message='foo', response='bar') slack_response.validate = mock.Mock(side_effect=expected_exception) test_token = 'test_token' test_slack_conn_id = 'test_slack_conn_id' slack_hook = SlackHook(token=test_token, slack_conn_id=test_slack_conn_id) test_method = 'test_method' test_api_params = {'key1': 'value1', 'key2': 'value2'} try: slack_hook.call(test_method, test_api_params) self.fail() except AirflowException as exc: self.assertIn("foo", str(exc)) self.assertIn("bar", str(exc))