Skip to content

Commit 59bf4d4

Browse files
mgornyantonpirkersentrivana
authored
test: Fix using unittest.mock whenever available (#1926)
Fix some of the newly-added `mock` imports to prefer `unittest.mock` when it is available. Update `test-requirements.txt` to install `mock` only in Python < 3.3; hopefully this will suffice for CI to catch these regressions in the future. --------- Co-authored-by: Anton Pirker <[email protected]> Co-authored-by: Ivana Kellyerova <[email protected]>
1 parent 28b21ed commit 59bf4d4

12 files changed

+75
-37
lines changed

test-requirements.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
pip # always use newest pip
2-
mock # for testing under python < 3.3
2+
mock ; python_version<'3.3'
33
pytest<7
44
pytest-cov==2.8.1
55
pytest-forked<=1.4.0

tests/integrations/celery/test_celery_beat_crons.py

+24-19
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import mock
2-
31
import pytest
42

53
pytest.importorskip("celery")
@@ -16,9 +14,16 @@
1614
from sentry_sdk.crons import MonitorStatus
1715
from 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

2025
def 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

7176
def 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

115120
def 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

159164
def 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

203208
def 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

237242
def 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

tests/integrations/cloud_resource_context/test_cloud_resource_context.py

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
import json
22

33
import pytest
4-
import mock
5-
from mock import MagicMock
4+
5+
try:
6+
from unittest import mock # python 3.3 and above
7+
from unittest.mock import MagicMock
8+
except ImportError:
9+
import mock # python < 3.3
10+
from mock import MagicMock
611

712
from sentry_sdk.integrations.cloud_resource_context import (
813
CLOUD_PLATFORM,
@@ -400,6 +405,6 @@ def test_setup_once(
400405
fake_set_context.assert_not_called()
401406

402407
if warning_called:
403-
fake_warning.assert_called_once()
408+
assert fake_warning.call_count == 1
404409
else:
405410
fake_warning.assert_not_called()

tests/integrations/opentelemetry/test_propagator.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1-
from mock import MagicMock
2-
import mock
1+
try:
2+
from unittest import mock # python 3.3 and above
3+
from unittest.mock import MagicMock
4+
except ImportError:
5+
import mock # python < 3.3
6+
from mock import MagicMock
37

48
from opentelemetry.context import get_current
59
from opentelemetry.trace.propagation import get_current_span

tests/integrations/opentelemetry/test_span_processor.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
from datetime import datetime
2-
from mock import MagicMock
3-
import mock
42
import time
3+
4+
try:
5+
from unittest import mock # python 3.3 and above
6+
from unittest.mock import MagicMock
7+
except ImportError:
8+
import mock
9+
from mock import MagicMock # python < 3.3
10+
511
from sentry_sdk.integrations.opentelemetry.span_processor import (
612
SentrySpanProcessor,
713
link_trace_context_to_error_event,

tests/integrations/redis/test_redis.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1-
import mock
2-
31
from sentry_sdk import capture_message, start_transaction
42
from sentry_sdk.consts import SPANDATA
53
from sentry_sdk.integrations.redis import RedisIntegration
64

75
from fakeredis import FakeStrictRedis
86
import pytest
97

8+
try:
9+
from unittest import mock # python 3.3 and above
10+
except ImportError:
11+
import mock # python < 3.3
12+
1013

1114
def test_basic(sentry_init, capture_events):
1215
sentry_init(integrations=[RedisIntegration()])

tests/test_api.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1-
import mock
2-
31
from sentry_sdk import (
42
configure_scope,
53
get_current_span,
64
start_transaction,
75
)
86

7+
try:
8+
from unittest import mock # python 3.3 and above
9+
except ImportError:
10+
import mock # python < 3.3
11+
912

1013
def test_get_current_span():
1114
fake_hub = mock.MagicMock()

tests/test_client.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# coding: utf-8
22
import os
33
import json
4-
import mock
54
import pytest
65
import subprocess
76
import sys
@@ -27,6 +26,11 @@
2726
from sentry_sdk.serializer import MAX_DATABAG_BREADTH
2827
from sentry_sdk.consts import DEFAULT_MAX_BREADCRUMBS
2928

29+
try:
30+
from unittest import mock # python 3.3 and above
31+
except ImportError:
32+
import mock # python < 3.3
33+
3034
if PY2:
3135
# Importing ABCs from collections is deprecated, and will stop working in 3.8
3236
# https://github.com/python/cpython/blob/master/Lib/collections/__init__.py#L49

tests/test_crons.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1-
import mock
21
import pytest
32
import uuid
43

54
import sentry_sdk
65
from sentry_sdk.crons import capture_checkin
76

7+
try:
8+
from unittest import mock # python 3.3 and above
9+
except ImportError:
10+
import mock # python < 3.3
11+
812

913
@sentry_sdk.monitor(monitor_slug="abc123")
1014
def _hello_world(name):

tests/tracing/test_decorator_py2.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1-
import mock
2-
31
from sentry_sdk.tracing_utils_py2 import (
42
start_child_span_decorator as start_child_span_decorator_py2,
53
)
64
from sentry_sdk.utils import logger
75

6+
try:
7+
from unittest import mock # python 3.3 and above
8+
except ImportError:
9+
import mock # python < 3.3
10+
811

912
def my_example_function():
1013
return "return_of_sync_function"

tests/tracing/test_decorator_py3.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import mock
1+
from unittest import mock
22
import pytest
33
import sys
44

tests/tracing/test_misc.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from mock import MagicMock
21
import pytest
32
import gc
43
import uuid
@@ -12,8 +11,10 @@
1211

1312
try:
1413
from unittest import mock # python 3.3 and above
14+
from unittest.mock import MagicMock
1515
except ImportError:
1616
import mock # python < 3.3
17+
from mock import MagicMock
1718

1819

1920
def test_span_trimming(sentry_init, capture_events):

0 commit comments

Comments
 (0)