Skip to content

Commit 8110496

Browse files
authored
ref(integrations): Use new scopes API in web framework tests (#2898)
1 parent 581e23b commit 8110496

File tree

5 files changed

+25
-47
lines changed

5 files changed

+25
-47
lines changed

tests/integrations/django/test_basic.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,13 @@
1818
from django.core.urlresolvers import reverse
1919

2020
from sentry_sdk._compat import PY310
21-
from sentry_sdk import capture_message, capture_exception, configure_scope
21+
from sentry_sdk import capture_message, capture_exception
2222
from sentry_sdk.consts import SPANDATA
2323
from sentry_sdk.integrations.django import DjangoIntegration, _set_db_data
2424
from sentry_sdk.integrations.django.signals_handlers import _get_receiver_name
2525
from sentry_sdk.integrations.django.caching import _get_span_description
2626
from sentry_sdk.integrations.executing import ExecutingIntegration
27+
from sentry_sdk.scope import Scope
2728
from sentry_sdk.tracing import Span
2829
from tests.conftest import unpack_werkzeug_response
2930
from tests.integrations.django.myapp.wsgi import application
@@ -372,8 +373,7 @@ def test_sql_queries(sentry_init, capture_events, with_integration):
372373

373374
sql = connection.cursor()
374375

375-
with configure_scope() as scope:
376-
scope.clear_breadcrumbs()
376+
Scope.get_isolation_scope().clear_breadcrumbs()
377377

378378
with pytest.raises(OperationalError):
379379
# table doesn't even exist
@@ -407,8 +407,7 @@ def test_sql_dict_query_params(sentry_init, capture_events):
407407
sql = connections["postgres"].cursor()
408408

409409
events = capture_events()
410-
with configure_scope() as scope:
411-
scope.clear_breadcrumbs()
410+
Scope.get_isolation_scope().clear_breadcrumbs()
412411

413412
with pytest.raises(ProgrammingError):
414413
sql.execute(
@@ -473,8 +472,7 @@ def test_sql_psycopg2_string_composition(sentry_init, capture_events, query):
473472

474473
sql = connections["postgres"].cursor()
475474

476-
with configure_scope() as scope:
477-
scope.clear_breadcrumbs()
475+
Scope.get_isolation_scope().clear_breadcrumbs()
478476

479477
events = capture_events()
480478

@@ -507,8 +505,7 @@ def test_sql_psycopg2_placeholders(sentry_init, capture_events):
507505
sql = connections["postgres"].cursor()
508506

509507
events = capture_events()
510-
with configure_scope() as scope:
511-
scope.clear_breadcrumbs()
508+
Scope.get_isolation_scope().clear_breadcrumbs()
512509

513510
with pytest.raises(DataError):
514511
names = ["foo", "bar"]

tests/integrations/falcon/test_falcon.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -404,8 +404,7 @@ def generator():
404404
expected_response = "".join(str(row) + "\n" for row in range(1000))
405405
assert response.text == expected_response
406406
assert not events
407-
408-
not Scope.get_isolation_scope()._tags["request_data"]
407+
assert not Scope.get_isolation_scope()._tags["request_data"]
409408

410409

411410
@pytest.mark.skipif(

tests/integrations/flask/test_flask.py

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import sentry_sdk.integrations.flask as flask_sentry
2525
from sentry_sdk import (
2626
set_tag,
27-
configure_scope,
2827
capture_message,
2928
capture_exception,
3029
)
@@ -279,8 +278,7 @@ def test_flask_session_tracking(sentry_init, capture_envelopes, app):
279278

280279
@app.route("/")
281280
def index():
282-
with configure_scope() as scope:
283-
scope.set_user({"ip_address": "1.2.3.4", "id": "42"})
281+
Scope.get_isolation_scope().set_user({"ip_address": "1.2.3.4", "id": "42"})
284282
try:
285283
raise ValueError("stuff")
286284
except Exception:
@@ -668,18 +666,15 @@ def test_does_not_leak_scope(sentry_init, capture_events, app):
668666
sentry_init(integrations=[flask_sentry.FlaskIntegration()])
669667
events = capture_events()
670668

671-
with configure_scope() as scope:
672-
scope.set_tag("request_data", False)
669+
Scope.get_isolation_scope().set_tag("request_data", False)
673670

674671
@app.route("/")
675672
def index():
676-
with configure_scope() as scope:
677-
scope.set_tag("request_data", True)
673+
Scope.get_isolation_scope().set_tag("request_data", True)
678674

679675
def generate():
680676
for row in range(1000):
681-
with configure_scope() as scope:
682-
assert scope._tags["request_data"]
677+
assert Scope.get_isolation_scope()._tags["request_data"]
683678

684679
yield str(row) + "\n"
685680

@@ -690,8 +685,7 @@ def generate():
690685
assert response.data.decode() == "".join(str(row) + "\n" for row in range(1000))
691686
assert not events
692687

693-
with configure_scope() as scope:
694-
assert not scope._tags["request_data"]
688+
assert not Scope.get_isolation_scope()._tags["request_data"]
695689

696690

697691
def test_scoped_test_client(sentry_init, app):
@@ -839,10 +833,7 @@ def test_template_tracing_meta(sentry_init, app, capture_events, template_string
839833

840834
@app.route("/")
841835
def index():
842-
scope = Scope.get_isolation_scope()
843-
capture_message(
844-
scope.get_traceparent() + "\n" + scope.get_baggage().serialize()
845-
)
836+
capture_message(sentry_sdk.get_traceparent() + "\n" + sentry_sdk.get_baggage())
846837
return render_template_string(template_string)
847838

848839
with app.test_client() as client:

tests/integrations/quart/test_quart.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66

77
from sentry_sdk import (
88
set_tag,
9-
configure_scope,
109
capture_message,
1110
capture_exception,
1211
)
1312
from sentry_sdk.integrations.logging import LoggingIntegration
1413
import sentry_sdk.integrations.quart as quart_sentry
14+
from sentry_sdk.scope import Scope
1515

1616
from quart import Quart, Response, abort, stream_with_context
1717
from quart.views import View
@@ -378,18 +378,15 @@ async def test_does_not_leak_scope(sentry_init, capture_events, app):
378378
sentry_init(integrations=[quart_sentry.QuartIntegration()])
379379
events = capture_events()
380380

381-
with configure_scope() as scope:
382-
scope.set_tag("request_data", False)
381+
Scope.get_isolation_scope().set_tag("request_data", False)
383382

384383
@app.route("/")
385384
async def index():
386-
with configure_scope() as scope:
387-
scope.set_tag("request_data", True)
385+
Scope.get_isolation_scope().set_tag("request_data", True)
388386

389387
async def generate():
390388
for row in range(1000):
391-
with configure_scope() as scope:
392-
assert scope._tags["request_data"]
389+
assert Scope.get_isolation_scope()._tags["request_data"]
393390

394391
yield str(row) + "\n"
395392

@@ -401,9 +398,7 @@ async def generate():
401398
str(row) + "\n" for row in range(1000)
402399
)
403400
assert not events
404-
405-
with configure_scope() as scope:
406-
assert not scope._tags["request_data"]
401+
assert not Scope.get_isolation_scope()._tags["request_data"]
407402

408403

409404
@pytest.mark.asyncio

tests/integrations/tornado/test_tornado.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
import pytest
44

5-
from sentry_sdk import configure_scope, start_transaction, capture_message
5+
from sentry_sdk import start_transaction, capture_message
66
from sentry_sdk.integrations.tornado import TornadoIntegration
7+
from sentry_sdk.scope import Scope
78

89
from tornado.web import RequestHandler, Application, HTTPError
910
from tornado.testing import AsyncHTTPTestCase
@@ -36,13 +37,11 @@ def bogustest(self):
3637

3738
class CrashingHandler(RequestHandler):
3839
def get(self):
39-
with configure_scope() as scope:
40-
scope.set_tag("foo", "42")
40+
Scope.get_isolation_scope().set_tag("foo", "42")
4141
1 / 0
4242

4343
def post(self):
44-
with configure_scope() as scope:
45-
scope.set_tag("foo", "43")
44+
Scope.get_isolation_scope().set_tag("foo", "43")
4645
1 / 0
4746

4847

@@ -54,14 +53,12 @@ def get(self):
5453

5554
class HelloHandler(RequestHandler):
5655
async def get(self):
57-
with configure_scope() as scope:
58-
scope.set_tag("foo", "42")
56+
Scope.get_isolation_scope().set_tag("foo", "42")
5957

6058
return b"hello"
6159

6260
async def post(self):
63-
with configure_scope() as scope:
64-
scope.set_tag("foo", "43")
61+
Scope.get_isolation_scope().set_tag("foo", "43")
6562

6663
return b"hello"
6764

@@ -104,8 +101,7 @@ def test_basic(tornado_testcase, sentry_init, capture_events):
104101
)
105102
assert event["transaction_info"] == {"source": "component"}
106103

107-
with configure_scope() as scope:
108-
assert not scope._tags
104+
assert not Scope.get_isolation_scope()._tags
109105

110106

111107
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)