Skip to content

Commit

Permalink
Resolve PT012 in telegram provider (apache#38386)
Browse files Browse the repository at this point in the history
  • Loading branch information
Taragolis authored and utkarsharma2 committed Apr 22, 2024
1 parent 580ec96 commit 0074f69
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 23 deletions.
2 changes: 0 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -1557,8 +1557,6 @@ combine-as-imports = true
"tests/providers/sftp/triggers/test_sftp.py" = ["PT012"]
"tests/providers/ssh/hooks/test_ssh.py" = ["PT012"]
"tests/providers/ssh/operators/test_ssh.py" = ["PT012"]
"tests/providers/telegram/hooks/test_telegram.py" = ["PT012"]
"tests/providers/telegram/operators/test_telegram.py" = ["PT012"]

[tool.ruff.lint.flake8-tidy-imports]
# Ban certain modules from being imported at module level, instead requiring
Expand Down
21 changes: 9 additions & 12 deletions tests/providers/telegram/hooks/test_telegram.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import pytest
import telegram
import tenacity

import airflow
from airflow.models import Connection
Expand Down Expand Up @@ -82,20 +83,18 @@ def test_should_raise_exception_if_conn_id_doesnt_contain_token(self):

@mock.patch("airflow.providers.telegram.hooks.telegram.TelegramHook.get_conn")
def test_should_raise_exception_if_chat_id_is_not_provided_anywhere(self, mock_get_conn):
with pytest.raises(airflow.exceptions.AirflowException) as ctx:
hook = TelegramHook(telegram_conn_id="telegram_default")
hook = TelegramHook(telegram_conn_id="telegram_default")
error_message = "'chat_id' must be provided for telegram message"
with pytest.raises(airflow.exceptions.AirflowException, match=error_message):
hook.send_message({"text": "test telegram message"})

assert "'chat_id' must be provided for telegram message" == str(ctx.value)

@mock.patch("airflow.providers.telegram.hooks.telegram.TelegramHook.get_conn")
def test_should_raise_exception_if_message_text_is_not_provided(self, mock_get_conn):
with pytest.raises(airflow.exceptions.AirflowException) as ctx:
hook = TelegramHook(telegram_conn_id="telegram_default")
hook = TelegramHook(telegram_conn_id="telegram_default")
error_message = "'text' must be provided for telegram message"
with pytest.raises(airflow.exceptions.AirflowException, match=error_message):
hook.send_message({"chat_id": -420913222})

assert "'text' must be provided for telegram message" == str(ctx.value)

@mock.patch("airflow.providers.telegram.hooks.telegram.TelegramHook.get_conn")
def test_should_send_message_if_all_parameters_are_correctly_provided(self, mock_get_conn):
mock_get_conn.return_value = AsyncMock(password="some_token")
Expand Down Expand Up @@ -163,11 +162,9 @@ def side_effect(*args, **kwargs):

mock_get_conn.return_value.send_message.side_effect = side_effect

with pytest.raises(Exception) as ctx:
hook = TelegramHook(telegram_conn_id="telegram-webhook-with-chat_id")
hook = TelegramHook(telegram_conn_id="telegram-webhook-with-chat_id")
with pytest.raises(tenacity.RetryError) as ctx:
hook.send_message({"text": "test telegram message"})

assert "RetryError" in str(ctx.value)
assert "state=finished raised TelegramError" in str(ctx.value)

mock_get_conn.assert_called_once()
Expand Down
16 changes: 7 additions & 9 deletions tests/providers/telegram/operators/test_telegram.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,13 @@ def side_effect(*args, **kwargs):
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(None)

assert "cosmic rays caused bit flips" == str(ctx.value)
op = TelegramOperator(
telegram_conn_id="telegram_default",
task_id="telegram",
text="some non empty text",
)
with pytest.raises(telegram.error.TelegramError, match="cosmic rays caused bit flips"):
op.execute({})

@mock.patch("airflow.providers.telegram.operators.telegram.TelegramHook")
def test_should_forward_all_args_to_telegram(self, mock_telegram_hook):
Expand Down

0 comments on commit 0074f69

Please sign in to comment.