Skip to content
18 changes: 2 additions & 16 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import socket
from threading import Thread
from contextlib import contextmanager
from http.server import BaseHTTPRequestHandler, HTTPServer
from unittest import mock

import pytest
import jsonschema
Expand All @@ -17,22 +19,6 @@
except ImportError:
eventlet = None

try:
# Python 2
import BaseHTTPServer

HTTPServer = BaseHTTPServer.HTTPServer
BaseHTTPRequestHandler = BaseHTTPServer.BaseHTTPRequestHandler
except Exception:
# Python 3
from http.server import BaseHTTPRequestHandler, HTTPServer


try:
from unittest import mock
except ImportError:
import mock

import sentry_sdk
from sentry_sdk.envelope import Envelope
from sentry_sdk.integrations import _processed_integrations # noqa: F401
Expand Down
6 changes: 1 addition & 5 deletions tests/integrations/aiohttp/test_aiohttp.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import asyncio
import json
from contextlib import suppress
from unittest import mock

import pytest
from aiohttp import web
Expand All @@ -10,11 +11,6 @@
from sentry_sdk import capture_message, start_transaction
from sentry_sdk.integrations.aiohttp import AioHttpIntegration

try:
from unittest import mock # python 3.3 and above
except ImportError:
import mock # python < 3.3


@pytest.mark.asyncio
async def test_basic(sentry_init, aiohttp_client, capture_events):
Expand Down
24 changes: 0 additions & 24 deletions tests/integrations/asgi/test_asgi.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import sys

from collections import Counter

import pytest
Expand All @@ -11,11 +9,6 @@
from async_asgi_testclient import TestClient


minimum_python_36 = pytest.mark.skipif(
sys.version_info < (3, 6), reason="ASGI is only supported in Python >= 3.6"
)


@pytest.fixture
def asgi3_app():
async def app(scope, receive, send):
Expand Down Expand Up @@ -133,7 +126,6 @@ async def app(scope, receive, send):
return app


@minimum_python_36
def test_invalid_transaction_style(asgi3_app):
with pytest.raises(ValueError) as exp:
SentryAsgiMiddleware(asgi3_app, transaction_style="URL")
Expand All @@ -144,7 +136,6 @@ def test_invalid_transaction_style(asgi3_app):
)


@minimum_python_36
@pytest.mark.asyncio
async def test_capture_transaction(
sentry_init,
Expand Down Expand Up @@ -176,7 +167,6 @@ async def test_capture_transaction(
}


@minimum_python_36
@pytest.mark.asyncio
async def test_capture_transaction_with_error(
sentry_init,
Expand Down Expand Up @@ -214,7 +204,6 @@ async def test_capture_transaction_with_error(
assert transaction_event["request"] == error_event["request"]


@minimum_python_36
@pytest.mark.asyncio
async def test_has_trace_if_performance_enabled(
sentry_init,
Expand Down Expand Up @@ -247,7 +236,6 @@ async def test_has_trace_if_performance_enabled(
)


@minimum_python_36
@pytest.mark.asyncio
async def test_has_trace_if_performance_disabled(
sentry_init,
Expand All @@ -271,7 +259,6 @@ async def test_has_trace_if_performance_disabled(
assert "trace_id" in error_event["contexts"]["trace"]


@minimum_python_36
@pytest.mark.asyncio
async def test_trace_from_headers_if_performance_enabled(
sentry_init,
Expand Down Expand Up @@ -305,7 +292,6 @@ async def test_trace_from_headers_if_performance_enabled(
assert transaction_event["contexts"]["trace"]["trace_id"] == trace_id


@minimum_python_36
@pytest.mark.asyncio
async def test_trace_from_headers_if_performance_disabled(
sentry_init,
Expand Down Expand Up @@ -334,7 +320,6 @@ async def test_trace_from_headers_if_performance_disabled(
assert error_event["contexts"]["trace"]["trace_id"] == trace_id


@minimum_python_36
@pytest.mark.asyncio
async def test_websocket(sentry_init, asgi3_ws_app, capture_events, request):
sentry_init(debug=True, send_default_pii=True)
Expand Down Expand Up @@ -367,7 +352,6 @@ async def test_websocket(sentry_init, asgi3_ws_app, capture_events, request):
assert exc["value"] == "Oh no"


@minimum_python_36
@pytest.mark.asyncio
async def test_auto_session_tracking_with_aggregates(
sentry_init, asgi3_app, capture_envelopes
Expand Down Expand Up @@ -406,7 +390,6 @@ async def test_auto_session_tracking_with_aggregates(
assert len(session_aggregates) == 1


@minimum_python_36
@pytest.mark.parametrize(
"url,transaction_style,expected_transaction,expected_source",
[
Expand Down Expand Up @@ -470,7 +453,6 @@ async def __call__():
pass


@minimum_python_36
def test_looks_like_asgi3(asgi3_app):
# branch: inspect.isclass(app)
assert _looks_like_asgi3(MockAsgi3App)
Expand All @@ -487,7 +469,6 @@ def test_looks_like_asgi3(asgi3_app):
assert not _looks_like_asgi3(asgi2)


@minimum_python_36
def test_get_ip_x_forwarded_for():
headers = [
(b"x-forwarded-for", b"8.8.8.8"),
Expand Down Expand Up @@ -525,7 +506,6 @@ def test_get_ip_x_forwarded_for():
assert ip == "5.5.5.5"


@minimum_python_36
def test_get_ip_x_real_ip():
headers = [
(b"x-real-ip", b"10.10.10.10"),
Expand All @@ -550,7 +530,6 @@ def test_get_ip_x_real_ip():
assert ip == "8.8.8.8"


@minimum_python_36
def test_get_ip():
# if now headers are provided the ip is taken from the client.
headers = []
Expand Down Expand Up @@ -584,7 +563,6 @@ def test_get_ip():
assert ip == "10.10.10.10"


@minimum_python_36
def test_get_headers():
headers = [
(b"x-real-ip", b"10.10.10.10"),
Expand All @@ -602,7 +580,6 @@ def test_get_headers():
}


@minimum_python_36
@pytest.mark.asyncio
@pytest.mark.parametrize(
"request_url,transaction_style,expected_transaction_name,expected_transaction_source",
Expand Down Expand Up @@ -654,7 +631,6 @@ async def test_transaction_name(
)


@minimum_python_36
@pytest.mark.asyncio
@pytest.mark.parametrize(
"request_url, transaction_style,expected_transaction_name,expected_transaction_source",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
import asyncio
import inspect
import sys
from unittest.mock import MagicMock, patch

import pytest

import sentry_sdk
from sentry_sdk.consts import OP
from sentry_sdk.integrations.asyncio import AsyncioIntegration, patch_asyncio

try:
from unittest.mock import MagicMock, patch
except ImportError:
from mock import MagicMock, patch

try:
from contextvars import Context, ContextVar
except ImportError:
Expand Down
5 changes: 1 addition & 4 deletions tests/integrations/aws_lambda/test_aws.py
Original file line number Diff line number Diff line change
Expand Up @@ -600,10 +600,7 @@ def test_traces_sampler_gets_correct_values_in_sampling_context(
+ dedent(inspect.getsource(ObjectDescribedBy))
+ dedent(
"""
try:
from unittest import mock # python 3.3 and above
except ImportError:
import mock # python < 3.3
from unittest import mock

def _safe_is_equal(x, y):
# copied from conftest.py - see docstring and comments there
Expand Down
10 changes: 3 additions & 7 deletions tests/integrations/boto3/test_s3.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
import pytest
from unittest import mock

import boto3
import pytest

from sentry_sdk import Hub
from sentry_sdk.integrations.boto3 import Boto3Integration
from tests.integrations.boto3.aws_mock import MockResponse
from tests.integrations.boto3 import read_fixture

try:
from unittest import mock # python 3.3 and above
except ImportError:
import mock # python < 3.3
from tests.integrations.boto3.aws_mock import MockResponse


session = boto3.Session(
Expand Down
11 changes: 3 additions & 8 deletions tests/integrations/celery/test_celery.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import threading
from unittest import mock

import pytest
from celery import Celery, VERSION
from celery.bin import worker

from sentry_sdk import Hub, configure_scope, start_transaction, get_current_span
from sentry_sdk.integrations.celery import (
Expand All @@ -9,14 +12,6 @@
_wrap_apply_async,
)

from celery import Celery, VERSION
from celery.bin import worker

try:
from unittest import mock # python 3.3 and above
except ImportError:
import mock # python < 3.3


@pytest.fixture
def connect_signal(request):
Expand Down
18 changes: 4 additions & 14 deletions tests/integrations/celery/test_celery_beat_crons.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import datetime
import sys
from unittest import mock
from unittest.mock import MagicMock

import pytest
from celery.schedules import crontab, schedule

from sentry_sdk.crons import MonitorStatus
from sentry_sdk.integrations.celery import (
_get_headers,
_get_humanized_interval,
Expand All @@ -12,15 +15,6 @@
crons_task_failure,
crons_task_retry,
)
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():
Expand Down Expand Up @@ -378,10 +372,6 @@ def test_get_monitor_config_timezone_in_app_conf():
assert monitor_config["timezone"] == "Asia/Karachi"


@pytest.mark.skipif(
sys.version_info < (3, 0),
reason="no datetime.timezone for Python 2, so skipping this test.",
)
def test_get_monitor_config_timezone_in_celery_schedule():
app = MagicMock()
app.timezone = "Asia/Karachi"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
import json
from unittest import mock
from unittest.mock import MagicMock

import pytest

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,
CLOUD_PROVIDER,
Expand All @@ -32,16 +27,11 @@
"version": "2017-09-30",
}

try:
# Python 3
AWS_EC2_EXAMPLE_IMDSv2_PAYLOAD_BYTES = bytes(
json.dumps(AWS_EC2_EXAMPLE_IMDSv2_PAYLOAD), "utf-8"
)
except TypeError:
# Python 2
AWS_EC2_EXAMPLE_IMDSv2_PAYLOAD_BYTES = bytes(
json.dumps(AWS_EC2_EXAMPLE_IMDSv2_PAYLOAD)
).encode("utf-8")

AWS_EC2_EXAMPLE_IMDSv2_PAYLOAD_BYTES = bytes(
json.dumps(AWS_EC2_EXAMPLE_IMDSv2_PAYLOAD), "utf-8"
)


GCP_GCE_EXAMPLE_METADATA_PLAYLOAD = {
"instance": {
Expand Down
5 changes: 1 addition & 4 deletions tests/integrations/django/asgi/test_asgi.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import base64
import json
import os
from unittest import mock

import django
import pytest
Expand All @@ -14,10 +15,6 @@
except ImportError:
from django.core.urlresolvers import reverse

try:
from unittest import mock # python 3.3 and above
except ImportError:
import mock # python < 3.3

APPS = [channels_application]
if django.VERSION >= (3, 0):
Expand Down
7 changes: 2 additions & 5 deletions tests/integrations/django/test_transactions.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
from unittest import mock

import pytest
import django

try:
from unittest import mock # python 3.3 and above
except ImportError:
import mock # python < 3.3


# django<2.0 has only `url` with regex based patterns.
# django>=2.0 renames `url` to `re_path`, and additionally introduces `path`
Expand Down
Loading