1- import mock
2-
31import pytest
42
53pytest .importorskip ("celery" )
1614from sentry_sdk .crons import MonitorStatus
1715from celery .schedules import crontab , schedule
1816
17+ try :
18+ from unittest import mock # python 3.3 and above
19+ from unittest .mock import MagicMock
20+ except ImportError :
21+ import mock # python < 3.3
22+ from mock import MagicMock
23+
1924
2025def test_get_headers ():
21- fake_task = mock . MagicMock ()
26+ fake_task = MagicMock ()
2227 fake_task .request = {
2328 "bla" : "blub" ,
2429 "foo" : "bar" ,
@@ -69,7 +74,7 @@ def test_get_humanized_interval(seconds, expected_tuple):
6974
7075
7176def test_crons_task_success ():
72- fake_task = mock . MagicMock ()
77+ fake_task = MagicMock ()
7378 fake_task .request = {
7479 "headers" : {
7580 "sentry-monitor-slug" : "test123" ,
@@ -113,7 +118,7 @@ def test_crons_task_success():
113118
114119
115120def test_crons_task_failure ():
116- fake_task = mock . MagicMock ()
121+ fake_task = MagicMock ()
117122 fake_task .request = {
118123 "headers" : {
119124 "sentry-monitor-slug" : "test123" ,
@@ -157,7 +162,7 @@ def test_crons_task_failure():
157162
158163
159164def test_crons_task_retry ():
160- fake_task = mock . MagicMock ()
165+ fake_task = MagicMock ()
161166 fake_task .request = {
162167 "headers" : {
163168 "sentry-monitor-slug" : "test123" ,
@@ -201,8 +206,8 @@ def test_crons_task_retry():
201206
202207
203208def test_get_monitor_config ():
204- app = mock . MagicMock ()
205- app .conf = mock . MagicMock ()
209+ app = MagicMock ()
210+ app .conf = MagicMock ()
206211 app .conf .timezone = "Europe/Vienna"
207212
208213 celery_schedule = crontab (day_of_month = "3" , hour = "12" , minute = "*/10" )
@@ -229,14 +234,14 @@ def test_get_monitor_config():
229234 "timezone" : "Europe/Vienna" ,
230235 }
231236
232- unknown_celery_schedule = mock . MagicMock ()
237+ unknown_celery_schedule = MagicMock ()
233238 monitor_config = _get_monitor_config (unknown_celery_schedule , app )
234239 assert monitor_config == {}
235240
236241
237242def test_get_monitor_config_default_timezone ():
238- app = mock . MagicMock ()
239- app .conf = mock . MagicMock ()
243+ app = MagicMock ()
244+ app .conf = MagicMock ()
240245 app .conf .timezone = None
241246
242247 celery_schedule = crontab (day_of_month = "3" , hour = "12" , minute = "*/10" )
@@ -259,18 +264,18 @@ def test_exclude_beat_tasks_option(
259264 """
260265 Test excluding Celery Beat tasks from automatic instrumentation.
261266 """
262- fake_apply_entry = mock . MagicMock ()
267+ fake_apply_entry = MagicMock ()
263268
264- fake_scheduler = mock . MagicMock ()
269+ fake_scheduler = MagicMock ()
265270 fake_scheduler .apply_entry = fake_apply_entry
266271
267- fake_integration = mock . MagicMock ()
272+ fake_integration = MagicMock ()
268273 fake_integration .exclude_beat_tasks = exclude_beat_tasks
269274
270- fake_schedule_entry = mock . MagicMock ()
275+ fake_schedule_entry = MagicMock ()
271276 fake_schedule_entry .name = task_name
272277
273- fake_get_monitor_config = mock . MagicMock ()
278+ fake_get_monitor_config = MagicMock ()
274279
275280 with mock .patch (
276281 "sentry_sdk.integrations.celery.Scheduler" , fake_scheduler
@@ -290,10 +295,10 @@ def test_exclude_beat_tasks_option(
290295
291296 if task_in_excluded_beat_tasks :
292297 # Only the original Scheduler.apply_entry() is called, _get_monitor_config is NOT called.
293- fake_apply_entry .assert_called_once ()
298+ assert fake_apply_entry .call_count == 1
294299 _get_monitor_config .assert_not_called ()
295300
296301 else :
297302 # The original Scheduler.apply_entry() is called, AND _get_monitor_config is called.
298- fake_apply_entry .assert_called_once ()
299- _get_monitor_config .assert_called_once ()
303+ assert fake_apply_entry .call_count == 1
304+ assert _get_monitor_config .call_count == 1
0 commit comments