def test_should_give_precedence_to_text_passed_in_constructor( self, mock_telegram_hook): mock_telegram_hook.return_value = mock.Mock() mock_telegram_hook.return_value.send_message.return_value = True hook = TelegramOperator( telegram_conn_id='telegram_default', chat_id='-420913222', task_id='telegram', text="some non empty text - higher precedence", telegram_kwargs={ "custom_arg": "value", "text": "some text, that will be ignored" }, ) hook.execute() mock_telegram_hook.assert_called_once_with( telegram_conn_id='telegram_default', chat_id='-420913222', token=None, ) mock_telegram_hook.return_value.send_message.assert_called_once_with( { 'custom_arg': 'value', 'text': 'some non empty text - higher precedence' }, )
def test_should_throw_exception_if_telegram_hook_throws_any_exception(self, mock_telegram_hook): def side_effect(*args, **kwargs): raise telegram.error.TelegramError("cosmic rays caused bit flips") mock_telegram_hook.return_value = mock.Mock() mock_telegram_hook.return_value.send_message.side_effect = side_effect with pytest.raises(telegram.error.TelegramError) as ctx: hook = TelegramOperator( telegram_conn_id='telegram_default', task_id='telegram', text="some non empty text", ) hook.execute() assert "cosmic rays caused bit flips" == str(ctx.value)
def test_should_return_template_fields(self): hook = TelegramOperator( telegram_conn_id='telegram_default', chat_id='-420913222', task_id='telegram', text="some non empty text - higher precedence", telegram_kwargs={"custom_arg": "value", "text": "some text, that will be ignored"}, ) assert ('text', 'chat_id') == hook.template_fields
def test_should_send_message_when_all_parameters_are_provided( self, mock_telegram_hook): mock_telegram_hook.return_value = mock.Mock() mock_telegram_hook.return_value.send_message.return_value = True hook = TelegramOperator( telegram_conn_id='telegram_default', chat_id='-420913222', task_id='telegram', text="some non empty text", ) hook.execute() mock_telegram_hook.assert_called_once_with( telegram_conn_id='telegram_default', chat_id='-420913222', token=None, ) mock_telegram_hook.return_value.send_message.assert_called_once_with( {'text': 'some non empty text'}, )
def test_should_forward_all_args_to_telegram(self, mock_telegram_hook): mock_telegram_hook.return_value = mock.Mock() mock_telegram_hook.return_value.send_message.return_value = True hook = TelegramOperator( telegram_conn_id='telegram_default', chat_id='-420913222', task_id='telegram', text="some non empty text", telegram_kwargs={"custom_arg": "value"}, ) hook.execute() mock_telegram_hook.assert_called_once_with( telegram_conn_id='telegram_default', chat_id='-420913222', token=None, ) mock_telegram_hook.return_value.send_message.assert_called_once_with( {'custom_arg': 'value', 'text': 'some non empty text'}, )
""" Example use of Telegram operator. """ from airflow import DAG from airflow.providers.telegram.operators.telegram import TelegramOperator from airflow.utils.dates import days_ago default_args = { 'owner': 'airflow', } dag = DAG( 'example_telegram', default_args=default_args, start_date=days_ago(2), tags=['example'], ) # [START howto_operator_telegram] send_message_telegram_task = TelegramOperator( task_id='send_message_telegram', telegram_conn_id='telegram_conn_id', chat_id='-3222103937', text='Hello from Airflow!', dag=dag, ) # [END howto_operator_telegram]
def test_should_throw_exception_if_connection_id_is_none(self): with self.assertRaises(airflow.exceptions.AirflowException) as e: TelegramOperator(task_id="telegram", telegram_conn_id=None) self.assertEqual("No valid Telegram connection id supplied.", str(e.exception))
def test_should_throw_exception_if_connection_id_is_none(self): with pytest.raises(airflow.exceptions.AirflowException) as ctx: TelegramOperator(task_id="telegram", telegram_conn_id=None) assert "No valid Telegram connection id supplied." == str(ctx.value)