7
7
import logging
8
8
import os
9
9
import sys
10
- import time
11
10
from typing import Optional
12
11
13
12
# Add project root to path
22
21
)
23
22
logger = logging .getLogger ("slack-notifier-test" )
24
23
25
- from src .utils .config_loader import load_config
26
24
from src .utils .slack_notifier import SlackNotifier , create_slack_notifier
27
25
28
26
29
27
def load_slack_configuration () -> Optional [str ]:
30
28
"""
31
29
Load Slack webhook URL from configuration.
32
-
30
+
33
31
Returns:
34
32
Slack webhook URL if found, None otherwise
35
33
"""
@@ -43,42 +41,39 @@ def load_slack_configuration() -> Optional[str]:
43
41
def create_and_validate_slack_notifier (webhook_url : str ) -> Optional [SlackNotifier ]:
44
42
"""
45
43
Create and validate Slack notifier instance.
46
-
44
+
47
45
Args:
48
46
webhook_url: The Slack webhook URL
49
-
47
+
50
48
Returns:
51
49
SlackNotifier instance if successful, None otherwise
52
50
"""
53
51
# Create notifier instance using factory function
54
52
notifier = create_slack_notifier (webhook_url )
55
-
53
+
56
54
if not notifier :
57
55
logger .error ("Failed to create Slack notifier instance" )
58
56
return None
59
-
57
+
60
58
logger .info ("Slack notifier created successfully" )
61
59
return notifier
62
60
63
61
64
62
def test_info_notification (notifier : SlackNotifier ) -> bool :
65
63
"""
66
64
Test sending an informational notification to Slack.
67
-
65
+
68
66
Args:
69
67
notifier: Configured SlackNotifier instance
70
-
68
+
71
69
Returns:
72
70
True if test passes, False otherwise
73
71
"""
74
72
# Send test info notification with sample message
75
73
logger .info ("Testing info notification..." )
76
-
77
- success = notifier .send_info_notification (
78
- message = "Test send info notification" ,
79
- title = "Test Notification"
80
- )
81
-
74
+
75
+ success = notifier .send_info_notification (message = "Test send info notification" , title = "Test Notification" )
76
+
82
77
if success :
83
78
logger .info ("Info notification TEST PASSED" )
84
79
return True
@@ -90,28 +85,26 @@ def test_info_notification(notifier: SlackNotifier) -> bool:
90
85
def test_success_notification (notifier : SlackNotifier ) -> bool :
91
86
"""
92
87
Test sending a success notification to Slack.
93
-
88
+
94
89
Args:
95
90
notifier: Configured SlackNotifier instance
96
-
91
+
97
92
Returns:
98
93
True if test passes, False otherwise
99
94
"""
100
95
# Send test success notification with sample indexer data
101
96
logger .info ("Testing success notification..." )
102
-
97
+
103
98
test_indexers = [
104
99
"0x1234567890abcdef1234567890abcdef12345678" ,
105
100
"0xabcdef1234567890abcdef1234567890abcdef12" ,
106
- "0x9876543210fedcba9876543210fedcba98765432"
101
+ "0x9876543210fedcba9876543210fedcba98765432" ,
107
102
]
108
-
103
+
109
104
success = notifier .send_success_notification (
110
- eligible_indexers = test_indexers ,
111
- total_processed = len (test_indexers ),
112
- execution_time = 1.0
105
+ eligible_indexers = test_indexers , total_processed = len (test_indexers ), execution_time = 1.0
113
106
)
114
-
107
+
115
108
if success :
116
109
logger .info ("Success notification TEST PASSED" )
117
110
return True
@@ -123,22 +116,22 @@ def test_success_notification(notifier: SlackNotifier) -> bool:
123
116
def test_failure_notification (notifier : SlackNotifier ) -> bool :
124
117
"""
125
118
Test sending a failure notification to Slack.
126
-
119
+
127
120
Args:
128
121
notifier: Configured SlackNotifier instance
129
-
122
+
130
123
Returns:
131
124
True if test passes, False otherwise
132
125
"""
133
126
# Send test failure notification with sample error
134
127
logger .info ("Testing failure notification..." )
135
-
128
+
136
129
success = notifier .send_failure_notification (
137
130
error_message = "Test error message to verify failure notifications work correctly." ,
138
131
stage = "Test Stage" ,
139
- execution_time = 1
132
+ execution_time = 1 ,
140
133
)
141
-
134
+
142
135
if success :
143
136
logger .info ("Failure notification TEST PASSED" )
144
137
return True
@@ -155,7 +148,7 @@ def execute_all_slack_tests() -> bool:
155
148
- Info notification
156
149
- Success notification
157
150
- Failure notification
158
-
151
+
159
152
Returns:
160
153
True if all tests pass, False otherwise
161
154
"""
@@ -164,28 +157,28 @@ def execute_all_slack_tests() -> bool:
164
157
if not webhook_url :
165
158
logger .error ("Failed to load webhook_url" )
166
159
return False
167
-
160
+
168
161
# Create and validate Slack notifier instance
169
162
notifier = create_and_validate_slack_notifier (webhook_url )
170
163
if not notifier :
171
164
logger .error ("Failed to create Slack notifier instance" )
172
165
return False
173
-
166
+
174
167
# Execute info notification test
175
168
if not test_info_notification (notifier ):
176
169
logger .error ("Failed to send info notification" )
177
170
return False
178
-
171
+
179
172
# Execute success notification test
180
173
if not test_success_notification (notifier ):
181
174
logger .error ("Failed to send success notification" )
182
175
return False
183
-
176
+
184
177
# Execute failure notification test
185
178
if not test_failure_notification (notifier ):
186
179
logger .error ("Failed to send failure notification" )
187
180
return False
188
-
181
+
189
182
# All tests completed successfully
190
183
logger .info ("All Slack notification tests passed!" )
191
184
return True
@@ -213,11 +206,11 @@ def main():
213
206
"""
214
207
# Display test header information
215
208
logger .info ("Service Quality Oracle - Slack Notification Test" )
216
-
209
+
217
210
# Check environment variable configuration
218
211
if not check_environment_variable_configuration ():
219
212
sys .exit (1 )
220
-
213
+
221
214
# Execute all tests and handle results
222
215
if execute_all_slack_tests ():
223
216
logger .info ("All tests completed successfully!" )
@@ -231,4 +224,4 @@ def main():
231
224
232
225
233
226
if __name__ == "__main__" :
234
- main ()
227
+ main ()
0 commit comments