forked from getindata/dbt-airflow-factory
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_notifications.py
More file actions
166 lines (139 loc) · 5.17 KB
/
Copy pathtest_notifications.py
File metadata and controls
166 lines (139 loc) · 5.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import json
import pathlib
from os import path
from unittest.mock import MagicMock, patch
import pytest
from airflow.models import Connection
from dbt_airflow_factory.constants import (
IS_AIRFLOW_NEWER_THAN_2_4,
IS_FIRST_AIRFLOW_VERSION,
)
if IS_FIRST_AIRFLOW_VERSION:
from airflow.contrib.operators.slack_webhook_operator import SlackWebhookOperator
else:
from airflow.providers.slack.operators.slack_webhook import SlackWebhookOperator
from dbt_airflow_factory.airflow_dag_factory import AirflowDagFactory
from dbt_airflow_factory.notifications.handler import NotificationHandlersFactory
@pytest.mark.parametrize(
"config_dir",
(
"notifications_slack",
"notifications_teams",
"notifications_google_chat",
),
)
def test_notification_callback_creation(config_dir):
# given
factory = AirflowDagFactory(path.dirname(path.abspath(__file__)), config_dir)
# when
dag = factory.create()
# then
assert dag.default_args["on_failure_callback"]
@patch(
"airflow.hooks.base.BaseHook.get_connection"
if IS_AIRFLOW_NEWER_THAN_2_4
else "airflow.hooks.base_hook.BaseHook.get_connection"
)
@patch(
"airflow.contrib.operators.slack_webhook_operator.SlackWebhookOperator.__new__"
if IS_FIRST_AIRFLOW_VERSION
else "airflow.providers.slack.operators.slack_webhook.SlackWebhookOperator.__new__"
)
def test_notification_send_for_slack(mock_operator_init, mock_get_connection):
# given
notifications_config = AirflowDagFactory(
path.dirname(path.abspath(__file__)), "notifications_slack"
).airflow_config["failure_handlers"]
factory = NotificationHandlersFactory()
context = create_context()
mock_get_connection.return_value = create_slack_connection()
mock_operator = MagicMock()
mock_operator_init.return_value = mock_operator
# when
factory.create_failure_handler(notifications_config)(context)
# then
mock_operator_init.assert_called_once_with(
SlackWebhookOperator,
task_id="slack_failure_notification",
message=":red_circle: Task Failed.\n"
"*Task*: task_id\n"
"*Dag*: dag_id\n"
"*Execution Time*: some date\n"
"*Log Url*: log_url",
http_conn_id="slack_failure",
webhook_token="test_password",
username="test_login",
)
mock_operator.execute.assert_called_once_with(context=context)
@patch(
"airflow.hooks.base.BaseHook.get_connection"
if IS_AIRFLOW_NEWER_THAN_2_4
else "airflow.hooks.base_hook.BaseHook.get_connection"
)
@patch("dbt_airflow_factory.notifications.ms_teams_webhook_hook.MSTeamsWebhookHook.run")
def test_notification_send_for_teams(mock_hook_run, mock_get_connection):
# given
notifications_config = AirflowDagFactory(
path.dirname(path.abspath(__file__)), "notifications_teams"
).airflow_config["failure_handlers"]
factory = NotificationHandlersFactory()
context = create_context()
mock_get_connection.return_value = create_teams_connection()
expected_payload_path = pathlib.Path(__file__).parent / "teams_webhook_expected_paylaod.json"
with open(expected_payload_path, "rt") as f:
webhook_expected_payload = json.load(f)
# when
factory.create_failure_handler(notifications_config)(context)
# then
request = mock_hook_run.call_args_list[0].kwargs
webhook_post_data = json.loads(request["data"].replace("\n", "").replace(" ", ""))
mock_hook_run.assert_called_once()
assert webhook_post_data == webhook_expected_payload
def create_slack_connection():
connection = MagicMock()
connection.configure_mock(**{"login": "test_login", "password": "test_password"})
return connection
def create_teams_connection():
connection = Connection(
**{
"login": None,
"password": None,
"conn_type": "http",
"host": "teams.com/webhook_endpoint",
"schema": "https",
}
)
return connection
def create_google_chat_connection():
connection = Connection(
**{
"login": None,
"password": None,
"conn_type": "http",
"host": "google.com/webhook_endpoint",
"schema": "https",
}
)
return connection
def create_context():
task_instance = MagicMock()
task_instance.configure_mock(**{"task_id": "task_id", "dag_id": "dag_id", "log_url": "log_url"})
return {"task_instance": task_instance, "execution_date": "some date", "ts": "ts"}
@patch(
"airflow.hooks.base.BaseHook.get_connection"
if IS_AIRFLOW_NEWER_THAN_2_4
else "airflow.hooks.base_hook.BaseHook.get_connection"
)
@patch("dbt_airflow_factory.notifications.handler.HttpHook")
def test_notification_send_for_google_chat(mock_http_hook, mock_get_connection):
# given
notifications_config = AirflowDagFactory(
path.dirname(path.abspath(__file__)), "notifications_google_chat"
).airflow_config["failure_handlers"]
factory = NotificationHandlersFactory()
context = create_context()
mock_get_connection.return_value = create_google_chat_connection()
# when
factory.create_failure_handler(notifications_config)(context)
# then
mock_http_hook.assert_called_once()