From d9dc816512ff6ad747b463f5789c7afbad69c372 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20G=C3=B3rny?= Date: Mon, 27 Feb 2023 20:40:29 +0100 Subject: [PATCH 1/4] test: Fix using unittest.mock whenever available 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. --- test-requirements.txt | 2 +- .../test_cloud_resource_context.py | 9 +++++++-- tests/integrations/opentelemetry/test_propagator.py | 8 ++++++-- .../integrations/opentelemetry/test_span_processor.py | 10 ++++++++-- tests/tracing/test_misc.py | 3 ++- 5 files changed, 24 insertions(+), 8 deletions(-) diff --git a/test-requirements.txt b/test-requirements.txt index 5d449df716..0fd5593d7a 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/cloud_resource_context/test_cloud_resource_context.py b/tests/integrations/cloud_resource_context/test_cloud_resource_context.py index b1efd97f3f..524b79fe66 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, 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 0467da7673..7d242d62a2 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/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): From fc41cf736ad4ec5654ec2a23d0beb86b11412266 Mon Sep 17 00:00:00 2001 From: Ivana Kellyerova Date: Wed, 7 Jun 2023 11:19:21 +0200 Subject: [PATCH 2/4] Prefer unittest.mock in more places --- tests/integrations/celery/test_celery_beat_crons.py | 7 +++++-- 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_py3.py | 2 +- 6 files changed, 26 insertions(+), 9 deletions(-) diff --git a/tests/integrations/celery/test_celery_beat_crons.py b/tests/integrations/celery/test_celery_beat_crons.py index a74214a9ee..3c127f91e6 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,6 +14,11 @@ from sentry_sdk.crons import MonitorStatus from celery.schedules import crontab, schedule +try: + from unittest import mock # python 3.3 and above +except ImportError: + import mock # python < 3.3 + def test_get_headers(): fake_task = mock.MagicMock() diff --git a/tests/integrations/redis/test_redis.py b/tests/integrations/redis/test_redis.py index a596319c8b..b700206451 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_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 From e8c1bb1e88c704633b57e9d1c06e1f09e3855a35 Mon Sep 17 00:00:00 2001 From: Ivana Kellyerova Date: Wed, 7 Jun 2023 11:30:45 +0200 Subject: [PATCH 3/4] fixes --- .../celery/test_celery_beat_crons.py | 30 ++++++++++--------- tests/tracing/test_decorator_py2.py | 7 +++-- 2 files changed, 21 insertions(+), 16 deletions(-) diff --git a/tests/integrations/celery/test_celery_beat_crons.py b/tests/integrations/celery/test_celery_beat_crons.py index 3c127f91e6..738850750c 100644 --- a/tests/integrations/celery/test_celery_beat_crons.py +++ b/tests/integrations/celery/test_celery_beat_crons.py @@ -16,12 +16,14 @@ 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", @@ -72,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", @@ -116,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", @@ -160,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", @@ -204,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") @@ -232,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") @@ -262,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 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" From c256ff67a8823224ecc217b7b72348fa960a47cd Mon Sep 17 00:00:00 2001 From: Ivana Kellyerova Date: Wed, 7 Jun 2023 11:43:03 +0200 Subject: [PATCH 4/4] compat --- tests/integrations/celery/test_celery_beat_crons.py | 6 +++--- .../cloud_resource_context/test_cloud_resource_context.py | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/integrations/celery/test_celery_beat_crons.py b/tests/integrations/celery/test_celery_beat_crons.py index 738850750c..1b0c82ba8d 100644 --- a/tests/integrations/celery/test_celery_beat_crons.py +++ b/tests/integrations/celery/test_celery_beat_crons.py @@ -295,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 ae2d320f9b..b36f795a2b 100644 --- a/tests/integrations/cloud_resource_context/test_cloud_resource_context.py +++ b/tests/integrations/cloud_resource_context/test_cloud_resource_context.py @@ -405,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()