Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: Fix using unittest.mock whenever available #1926

Merged
merged 15 commits into from
Jun 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion test-requirements.txt
Original file line number Diff line number Diff line change
@@ -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
Expand Down
43 changes: 24 additions & 19 deletions tests/integrations/celery/test_celery_beat_crons.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import mock

import pytest

pytest.importorskip("celery")
Expand All @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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")
Expand All @@ -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")
Expand All @@ -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
Expand All @@ -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
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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()
8 changes: 6 additions & 2 deletions tests/integrations/opentelemetry/test_propagator.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
10 changes: 8 additions & 2 deletions tests/integrations/opentelemetry/test_span_processor.py
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
7 changes: 5 additions & 2 deletions tests/integrations/redis/test_redis.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import mock

from sentry_sdk import capture_message, start_transaction
from sentry_sdk.consts import SPANDATA
from sentry_sdk.integrations.redis import RedisIntegration

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()])
Expand Down
7 changes: 5 additions & 2 deletions tests/test_api.py
Original file line number Diff line number Diff line change
@@ -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()
Expand Down
6 changes: 5 additions & 1 deletion tests/test_client.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# coding: utf-8
import os
import json
import mock
import pytest
import subprocess
import sys
Expand All @@ -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
Expand Down
6 changes: 5 additions & 1 deletion tests/test_crons.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down
7 changes: 5 additions & 2 deletions tests/tracing/test_decorator_py2.py
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
2 changes: 1 addition & 1 deletion tests/tracing/test_decorator_py3.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import mock
from unittest import mock
import pytest
import sys

Expand Down
3 changes: 2 additions & 1 deletion tests/tracing/test_misc.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from mock import MagicMock
import pytest
import gc
import uuid
Expand All @@ -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):
Expand Down