Skip to content
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 pytest.ini
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[pytest]
DJANGO_SETTINGS_MODULE = tests.integrations.django.myapp.settings
addopts = --boxed --tb=short
addopts = --tb=short
markers = tests_internal_exceptions
2 changes: 1 addition & 1 deletion test-requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
hypothesis==3.69.9
pytest==3.7.3
pytest-xdist==1.23.0
git+https://github.com/untitaker/pytest-forked@forked-marker#egg=pytest-forked
tox==3.7.0
Werkzeug==0.15.3
pytest-localserver==0.4.1
Expand Down
17 changes: 14 additions & 3 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,15 +184,22 @@ def fast_serialize(request):


@pytest.fixture
def sentry_init(monkeypatch_test_transport, fast_serialize):
def sentry_init(monkeypatch_test_transport, fast_serialize, request):
def inner(*a, **kw):
hub = sentry_sdk.Hub.current
client = sentry_sdk.Client(*a, **kw)
client.options["_experiments"]["fast_serialize"] = fast_serialize
hub.bind_client(client)
monkeypatch_test_transport(sentry_sdk.Hub.current.client)

return inner
if request.node.get_closest_marker("forked"):
# Do not run isolation if the test is already running in
# ultimate isolation (seems to be required for celery tests that
# fork)
yield inner
else:
with sentry_sdk.Hub(None):
yield inner


class TestTransport(Transport):
Expand Down Expand Up @@ -258,7 +265,11 @@ def read_flush(self):


# scope=session ensures that fixture is run earlier
@pytest.fixture(scope="session", params=[None, "eventlet", "gevent"])
@pytest.fixture(
scope="session",
params=[None, "eventlet", "gevent"],
ids=("threads", "eventlet", "greenlet"),
)
def maybe_monkeypatched_threading(request):
if request.param == "eventlet":
try:
Expand Down
1 change: 1 addition & 0 deletions tests/integrations/celery/test_celery.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ def dummy_task(self):
assert e["type"] == "ZeroDivisionError"


@pytest.mark.forked
@pytest.mark.skipif(VERSION < (4,), reason="in-memory backend broken")
def test_transport_shutdown(request, celery, capture_events_forksafe, tmpdir):
events = capture_events_forksafe()
Expand Down
18 changes: 17 additions & 1 deletion tests/integrations/django/test_basic.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import absolute_import

import pytest
import json

Expand Down Expand Up @@ -81,6 +83,7 @@ def test_transaction_with_class_view(sentry_init, client, capture_events):
assert event["message"] == "hi"


@pytest.mark.forked
@pytest.mark.django_db
def test_user_captured(sentry_init, client, capture_events):
sentry_init(integrations=[DjangoIntegration()], send_default_pii=True)
Expand All @@ -102,6 +105,7 @@ def test_user_captured(sentry_init, client, capture_events):
}


@pytest.mark.forked
@pytest.mark.django_db
def test_queryset_repr(sentry_init, capture_events):
sentry_init(integrations=[DjangoIntegration()])
Expand Down Expand Up @@ -156,6 +160,7 @@ def test_500(sentry_init, client, capture_events):
assert content == "Sentry error: %s" % event_id


@pytest.mark.forked
def test_management_command_raises():
# This just checks for our assumption that Django passes through all
# exceptions by default, so our excepthook can be used for management
Expand All @@ -164,6 +169,7 @@ def test_management_command_raises():
execute_from_command_line(["manage.py", "mycrash"])


@pytest.mark.forked
@pytest.mark.django_db
@pytest.mark.parametrize("with_integration", [True, False])
def test_sql_queries(sentry_init, capture_events, with_integration):
Expand All @@ -175,9 +181,16 @@ def test_sql_queries(sentry_init, capture_events, with_integration):

from django.db import connection

sql = connection.cursor()
sentry_init(
integrations=[DjangoIntegration()],
send_default_pii=True,
_experiments={"record_sql_params": True},
)

events = capture_events()

sql = connection.cursor()

with pytest.raises(OperationalError):
# table doesn't even exist
sql.execute("""SELECT count(*) FROM people_person WHERE foo = %s""", [123])
Expand All @@ -193,6 +206,7 @@ def test_sql_queries(sentry_init, capture_events, with_integration):
assert crumb["data"]["db.params"] == [123]


@pytest.mark.forked
@pytest.mark.django_db
def test_sql_dict_query_params(sentry_init, capture_events):
sentry_init(
Expand Down Expand Up @@ -234,6 +248,7 @@ def test_sql_dict_query_params(sentry_init, capture_events):
lambda sql: sql.SQL('SELECT %(my_param)s FROM "foobar"'),
],
)
@pytest.mark.forked
@pytest.mark.django_db
def test_sql_psycopg2_string_composition(sentry_init, capture_events, query):
sentry_init(
Expand Down Expand Up @@ -262,6 +277,7 @@ def test_sql_psycopg2_string_composition(sentry_init, capture_events, query):
assert crumb["data"]["db.params"] == {"my_param": 10}


@pytest.mark.forked
@pytest.mark.django_db
def test_sql_psycopg2_placeholders(sentry_init, capture_events):
sentry_init(
Expand Down
3 changes: 3 additions & 0 deletions tests/integrations/threading/test_threading.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from sentry_sdk.integrations.threading import ThreadingIntegration


@pytest.mark.forked
@pytest.mark.parametrize("integrations", [[ThreadingIntegration()], []])
def test_handles_exceptions(sentry_init, capture_events, integrations):
sentry_init(default_integrations=False, integrations=integrations)
Expand All @@ -30,6 +31,7 @@ def crash():
assert not events


@pytest.mark.forked
@pytest.mark.parametrize("propagate_hub", (True, False))
def test_propagates_hub(sentry_init, capture_events, propagate_hub):
sentry_init(
Expand Down Expand Up @@ -85,6 +87,7 @@ def run(self):
assert not gc.collect()


@pytest.mark.forked
def test_double_patching(sentry_init, capture_events):
sentry_init(default_integrations=False, integrations=[ThreadingIntegration()])
events = capture_events()
Expand Down
23 changes: 9 additions & 14 deletions tests/test_basics.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,26 +205,21 @@ def test_breadcrumbs(sentry_init, capture_events):
assert len(event["breadcrumbs"]) == 0


def test_integration_scoping():
def test_integration_scoping(sentry_init, capture_events):
logger = logging.getLogger("test_basics")
events = []
logging_integration = LoggingIntegration(event_level=logging.WARNING)

# This client uses the logging integration
client_with_logging = Client(
transport=events.append,
default_integrations=False,
integrations=[logging_integration],
)
Hub.current.bind_client(client_with_logging)
logging_integration = LoggingIntegration(event_level=logging.WARNING)
sentry_init(default_integrations=False, integrations=[logging_integration])
events = capture_events()
logger.warning("This is a warning")
assert len(events) == 1

# This client does not
client_without_logging = Client(transport=events.append, default_integrations=False)
Hub.current.bind_client(client_without_logging)
sentry_init(default_integrations=False)
events = capture_events()
logger.warning("This is not a warning")

assert len(events) == 1
assert not events


def test_client_initialized_within_scope(sentry_init, caplog):
Expand All @@ -233,7 +228,7 @@ def test_client_initialized_within_scope(sentry_init, caplog):
sentry_init(debug=True)

with push_scope():
sentry_init()
Hub.current.bind_client(Client())

record, = (x for x in caplog.records if x.levelname == "WARNING")

Expand Down
4 changes: 4 additions & 0 deletions tests/test_client.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# coding: utf-8
import os
import json
import pytest
import subprocess
Expand Down Expand Up @@ -31,6 +32,9 @@ def capture_event(self, event):


def test_transport_option(monkeypatch):
if "SENTRY_DSN" in os.environ:
monkeypatch.delenv("SENTRY_DSN")

dsn = "https://[email protected]/123"
dsn2 = "https://[email protected]/124"
assert str(Client(dsn=dsn).dsn) == dsn
Expand Down
1 change: 1 addition & 0 deletions tests/test_transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def inner(*args, **kwargs):
return inner


@pytest.mark.forked
@pytest.mark.parametrize("debug", (True, False))
@pytest.mark.parametrize("client_flush_method", ["close", "flush"])
def test_transport_works(
Expand Down
3 changes: 3 additions & 0 deletions tests/utils/test_contextvars.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
import pytest
import random
import time


from sentry_sdk.utils import _is_threading_local_monkey_patched


@pytest.mark.forked
def test_thread_local_is_patched(maybe_monkeypatched_threading):
if maybe_monkeypatched_threading is None:
assert not _is_threading_local_monkey_patched()
else:
assert _is_threading_local_monkey_patched()


@pytest.mark.forked
def test_leaks(maybe_monkeypatched_threading):
import threading

Expand Down