From 59bf4d45a4cdca2cb19e6f75851a01cbe06d0b2c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20G=C3=B3rny?= Date: Wed, 7 Jun 2023 11:57:39 +0200 Subject: [PATCH] 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 Co-authored-by: Ivana Kellyerova --- test-requirements.txt | 2 +- .../celery/test_celery_beat_crons.py | 43 +++++++++++-------- .../test_cloud_resource_context.py | 11 +++-- .../opentelemetry/test_propagator.py | 8 +++- .../opentelemetry/test_span_processor.py | 10 ++++- tests/integrations/redis/test_redis.py | 7 ++- tests/test_api.py | 7 ++- tests/test_client.py | 6 ++- tests/test_crons.py | 6 ++- tests/tracing/test_decorator_py2.py | 7 ++- tests/tracing/test_decorator_py3.py | 2 +- tests/tracing/test_misc.py | 3 +- 12 files changed, 75 insertions(+), 37 deletions(-) diff --git a/test-requirements.txt b/test-requirements.txt index a70bd769d1..662ac4bd53 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -1,5 +1,5 @@ pip # always use newest pip -mock # for testing under python < 3.3 +mock ; python_version<'3.3' pytest<7 pytest-cov==2.8.1 pytest-forked<=1.4.0 diff --git a/tests/integrations/celery/test_celery_beat_crons.py b/tests/integrations/celery/test_celery_beat_crons.py index a74214a9ee..1b0c82ba8d 100644 --- a/tests/integrations/celery/test_celery_beat_crons.py +++ b/tests/integrations/celery/test_celery_beat_crons.py @@ -1,5 +1,3 @@ -import mock - import pytest pytest.importorskip("celery") @@ -16,9 +14,16 @@ from sentry_sdk.crons import MonitorStatus from celery.schedules import crontab, schedule +try: + from unittest import mock # python 3.3 and above + from unittest.mock import MagicMock +except ImportError: + import mock # python < 3.3 + from mock import MagicMock + def test_get_headers(): - fake_task = mock.MagicMock() + fake_task = MagicMock() fake_task.request = { "bla": "blub", "foo": "bar", @@ -69,7 +74,7 @@ def test_get_humanized_interval(seconds, expected_tuple): def test_crons_task_success(): - fake_task = mock.MagicMock() + fake_task = MagicMock() fake_task.request = { "headers": { "sentry-monitor-slug": "test123", @@ -113,7 +118,7 @@ def test_crons_task_success(): def test_crons_task_failure(): - fake_task = mock.MagicMock() + fake_task = MagicMock() fake_task.request = { "headers": { "sentry-monitor-slug": "test123", @@ -157,7 +162,7 @@ def test_crons_task_failure(): def test_crons_task_retry(): - fake_task = mock.MagicMock() + fake_task = MagicMock() fake_task.request = { "headers": { "sentry-monitor-slug": "test123", @@ -201,8 +206,8 @@ def test_crons_task_retry(): def test_get_monitor_config(): - app = mock.MagicMock() - app.conf = mock.MagicMock() + app = MagicMock() + app.conf = MagicMock() app.conf.timezone = "Europe/Vienna" celery_schedule = crontab(day_of_month="3", hour="12", minute="*/10") @@ -229,14 +234,14 @@ def test_get_monitor_config(): "timezone": "Europe/Vienna", } - unknown_celery_schedule = mock.MagicMock() + unknown_celery_schedule = MagicMock() monitor_config = _get_monitor_config(unknown_celery_schedule, app) assert monitor_config == {} def test_get_monitor_config_default_timezone(): - app = mock.MagicMock() - app.conf = mock.MagicMock() + app = MagicMock() + app.conf = MagicMock() app.conf.timezone = None celery_schedule = crontab(day_of_month="3", hour="12", minute="*/10") @@ -259,18 +264,18 @@ def test_exclude_beat_tasks_option( """ Test excluding Celery Beat tasks from automatic instrumentation. """ - fake_apply_entry = mock.MagicMock() + fake_apply_entry = MagicMock() - fake_scheduler = mock.MagicMock() + fake_scheduler = MagicMock() fake_scheduler.apply_entry = fake_apply_entry - fake_integration = mock.MagicMock() + fake_integration = MagicMock() fake_integration.exclude_beat_tasks = exclude_beat_tasks - fake_schedule_entry = mock.MagicMock() + fake_schedule_entry = MagicMock() fake_schedule_entry.name = task_name - fake_get_monitor_config = mock.MagicMock() + fake_get_monitor_config = MagicMock() with mock.patch( "sentry_sdk.integrations.celery.Scheduler", fake_scheduler @@ -290,10 +295,10 @@ def test_exclude_beat_tasks_option( if task_in_excluded_beat_tasks: # Only the original Scheduler.apply_entry() is called, _get_monitor_config is NOT called. - fake_apply_entry.assert_called_once() + assert fake_apply_entry.call_count == 1 _get_monitor_config.assert_not_called() else: # The original Scheduler.apply_entry() is called, AND _get_monitor_config is called. - fake_apply_entry.assert_called_once() - _get_monitor_config.assert_called_once() + assert fake_apply_entry.call_count == 1 + assert _get_monitor_config.call_count == 1 diff --git a/tests/integrations/cloud_resource_context/test_cloud_resource_context.py b/tests/integrations/cloud_resource_context/test_cloud_resource_context.py index 07e627d5d7..b36f795a2b 100644 --- a/tests/integrations/cloud_resource_context/test_cloud_resource_context.py +++ b/tests/integrations/cloud_resource_context/test_cloud_resource_context.py @@ -1,8 +1,13 @@ import json import pytest -import mock -from mock import MagicMock + +try: + from unittest import mock # python 3.3 and above + from unittest.mock import MagicMock +except ImportError: + import mock # python < 3.3 + from mock import MagicMock from sentry_sdk.integrations.cloud_resource_context import ( CLOUD_PLATFORM, @@ -400,6 +405,6 @@ def test_setup_once( fake_set_context.assert_not_called() if warning_called: - fake_warning.assert_called_once() + assert fake_warning.call_count == 1 else: fake_warning.assert_not_called() diff --git a/tests/integrations/opentelemetry/test_propagator.py b/tests/integrations/opentelemetry/test_propagator.py index 529aa99c09..d3e29707e5 100644 --- a/tests/integrations/opentelemetry/test_propagator.py +++ b/tests/integrations/opentelemetry/test_propagator.py @@ -1,5 +1,9 @@ -from mock import MagicMock -import mock +try: + from unittest import mock # python 3.3 and above + from unittest.mock import MagicMock +except ImportError: + import mock # python < 3.3 + from mock import MagicMock from opentelemetry.context import get_current from opentelemetry.trace.propagation import get_current_span diff --git a/tests/integrations/opentelemetry/test_span_processor.py b/tests/integrations/opentelemetry/test_span_processor.py index 8659e548a1..0db2a942a5 100644 --- a/tests/integrations/opentelemetry/test_span_processor.py +++ b/tests/integrations/opentelemetry/test_span_processor.py @@ -1,7 +1,13 @@ from datetime import datetime -from mock import MagicMock -import mock import time + +try: + from unittest import mock # python 3.3 and above + from unittest.mock import MagicMock +except ImportError: + import mock + from mock import MagicMock # python < 3.3 + from sentry_sdk.integrations.opentelemetry.span_processor import ( SentrySpanProcessor, link_trace_context_to_error_event, diff --git a/tests/integrations/redis/test_redis.py b/tests/integrations/redis/test_redis.py index ad23967873..37a886c224 100644 --- a/tests/integrations/redis/test_redis.py +++ b/tests/integrations/redis/test_redis.py @@ -1,5 +1,3 @@ -import mock - from sentry_sdk import capture_message, start_transaction from sentry_sdk.consts import SPANDATA from sentry_sdk.integrations.redis import RedisIntegration @@ -7,6 +5,11 @@ from fakeredis import FakeStrictRedis import pytest +try: + from unittest import mock # python 3.3 and above +except ImportError: + import mock # python < 3.3 + def test_basic(sentry_init, capture_events): sentry_init(integrations=[RedisIntegration()]) diff --git a/tests/test_api.py b/tests/test_api.py index ce4315df19..dc969404d0 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -1,11 +1,14 @@ -import mock - from sentry_sdk import ( configure_scope, get_current_span, start_transaction, ) +try: + from unittest import mock # python 3.3 and above +except ImportError: + import mock # python < 3.3 + def test_get_current_span(): fake_hub = mock.MagicMock() diff --git a/tests/test_client.py b/tests/test_client.py index 1a932c65f2..835a75e6fa 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -1,7 +1,6 @@ # coding: utf-8 import os import json -import mock import pytest import subprocess import sys @@ -27,6 +26,11 @@ from sentry_sdk.serializer import MAX_DATABAG_BREADTH from sentry_sdk.consts import DEFAULT_MAX_BREADCRUMBS +try: + from unittest import mock # python 3.3 and above +except ImportError: + import mock # python < 3.3 + if PY2: # Importing ABCs from collections is deprecated, and will stop working in 3.8 # https://github.com/python/cpython/blob/master/Lib/collections/__init__.py#L49 diff --git a/tests/test_crons.py b/tests/test_crons.py index 0a940c52ad..7688ac8a72 100644 --- a/tests/test_crons.py +++ b/tests/test_crons.py @@ -1,10 +1,14 @@ -import mock import pytest import uuid import sentry_sdk from sentry_sdk.crons import capture_checkin +try: + from unittest import mock # python 3.3 and above +except ImportError: + import mock # python < 3.3 + @sentry_sdk.monitor(monitor_slug="abc123") def _hello_world(name): diff --git a/tests/tracing/test_decorator_py2.py b/tests/tracing/test_decorator_py2.py index c7c503cb1a..9969786623 100644 --- a/tests/tracing/test_decorator_py2.py +++ b/tests/tracing/test_decorator_py2.py @@ -1,10 +1,13 @@ -import mock - from sentry_sdk.tracing_utils_py2 import ( start_child_span_decorator as start_child_span_decorator_py2, ) from sentry_sdk.utils import logger +try: + from unittest import mock # python 3.3 and above +except ImportError: + import mock # python < 3.3 + def my_example_function(): return "return_of_sync_function" diff --git a/tests/tracing/test_decorator_py3.py b/tests/tracing/test_decorator_py3.py index bc3ea29316..c458e8add4 100644 --- a/tests/tracing/test_decorator_py3.py +++ b/tests/tracing/test_decorator_py3.py @@ -1,4 +1,4 @@ -import mock +from unittest import mock import pytest import sys diff --git a/tests/tracing/test_misc.py b/tests/tracing/test_misc.py index 007dcb9151..0c9d114793 100644 --- a/tests/tracing/test_misc.py +++ b/tests/tracing/test_misc.py @@ -1,4 +1,3 @@ -from mock import MagicMock import pytest import gc import uuid @@ -12,8 +11,10 @@ try: from unittest import mock # python 3.3 and above + from unittest.mock import MagicMock except ImportError: import mock # python < 3.3 + from mock import MagicMock def test_span_trimming(sentry_init, capture_events):