|
1 | 1 | import os |
2 | 2 |
|
3 | 3 | import pytest |
| 4 | +from contextlib import contextmanager |
4 | 5 | from datetime import datetime |
5 | 6 | from unittest import mock |
6 | 7 |
|
|
15 | 16 | from freezegun import freeze_time |
16 | 17 | from werkzeug.test import Client |
17 | 18 |
|
18 | | -from sentry_sdk import start_transaction |
| 19 | +from sentry_sdk import start_transaction, start_span |
19 | 20 | from sentry_sdk.consts import SPANDATA |
20 | 21 | from sentry_sdk.integrations.django import DjangoIntegration |
21 | 22 | from sentry_sdk.tracing_utils import record_sql_queries |
@@ -347,29 +348,24 @@ def test_no_query_source_if_duration_too_short(sentry_init, client, capture_even |
347 | 348 |
|
348 | 349 | events = capture_events() |
349 | 350 |
|
350 | | - class fake_record_sql_queries: # noqa: N801 |
351 | | - def __init__(self, *args, **kwargs): |
352 | | - with freeze_time(datetime(2024, 1, 1, microsecond=0)): |
353 | | - with record_sql_queries(*args, **kwargs) as span: |
354 | | - self.span = span |
355 | | - freezer = freeze_time(datetime(2024, 1, 1, microsecond=99999)) |
356 | | - freezer.start() |
357 | | - |
358 | | - freezer.stop() |
359 | | - |
360 | | - def __enter__(self): |
361 | | - return self.span |
362 | | - |
363 | | - def __exit__(self, type, value, traceback): |
364 | | - pass |
365 | | - |
366 | | - with mock.patch( |
367 | | - "sentry_sdk.integrations.django.record_sql_queries", |
368 | | - fake_record_sql_queries, |
369 | | - ): |
370 | | - _, status, _ = unpack_werkzeug_response( |
371 | | - client.get(reverse("postgres_select_orm")) |
372 | | - ) |
| 351 | + def fake_start_span(*args, **kwargs): # noqa: N801 |
| 352 | + with freeze_time(datetime(2024, 1, 1, microsecond=0)): |
| 353 | + return start_span(*args, **kwargs) |
| 354 | + |
| 355 | + @contextmanager |
| 356 | + def fake_record_sql_queries(*args, **kwargs): # noqa: N801 |
| 357 | + with freeze_time(datetime(2024, 1, 1, microsecond=99999)): |
| 358 | + with record_sql_queries(*args, **kwargs) as span: |
| 359 | + yield span |
| 360 | + |
| 361 | + with mock.patch("sentry_sdk.start_span", fake_start_span): |
| 362 | + with mock.patch( |
| 363 | + "sentry_sdk.integrations.django.record_sql_queries", |
| 364 | + fake_record_sql_queries, |
| 365 | + ): |
| 366 | + _, status, _ = unpack_werkzeug_response( |
| 367 | + client.get(reverse("postgres_select_orm")) |
| 368 | + ) |
373 | 369 |
|
374 | 370 | assert status == "200 OK" |
375 | 371 |
|
@@ -407,29 +403,24 @@ def test_query_source_if_duration_over_threshold(sentry_init, client, capture_ev |
407 | 403 |
|
408 | 404 | events = capture_events() |
409 | 405 |
|
410 | | - class fake_record_sql_queries: # noqa: N801 |
411 | | - def __init__(self, *args, **kwargs): |
412 | | - with freeze_time(datetime(2024, 1, 1, microsecond=0)): |
413 | | - with record_sql_queries(*args, **kwargs) as span: |
414 | | - self.span = span |
415 | | - freezer = freeze_time(datetime(2024, 1, 1, microsecond=99999)) |
416 | | - freezer.start() |
417 | | - |
418 | | - freezer.stop() |
419 | | - |
420 | | - def __enter__(self): |
421 | | - return self.span |
422 | | - |
423 | | - def __exit__(self, type, value, traceback): |
424 | | - pass |
425 | | - |
426 | | - with mock.patch( |
427 | | - "sentry_sdk.integrations.django.record_sql_queries", |
428 | | - fake_record_sql_queries, |
429 | | - ): |
430 | | - _, status, _ = unpack_werkzeug_response( |
431 | | - client.get(reverse("postgres_select_orm")) |
432 | | - ) |
| 406 | + def fake_start_span(*args, **kwargs): # noqa: N801 |
| 407 | + with freeze_time(datetime(2024, 1, 1, microsecond=0)): |
| 408 | + return start_span(*args, **kwargs) |
| 409 | + |
| 410 | + @contextmanager |
| 411 | + def fake_record_sql_queries(*args, **kwargs): # noqa: N801 |
| 412 | + with freeze_time(datetime(2024, 1, 1, microsecond=100001)): |
| 413 | + with record_sql_queries(*args, **kwargs) as span: |
| 414 | + yield span |
| 415 | + |
| 416 | + with mock.patch("sentry_sdk.start_span", fake_start_span): |
| 417 | + with mock.patch( |
| 418 | + "sentry_sdk.integrations.django.record_sql_queries", |
| 419 | + fake_record_sql_queries, |
| 420 | + ): |
| 421 | + _, status, _ = unpack_werkzeug_response( |
| 422 | + client.get(reverse("postgres_select_orm")) |
| 423 | + ) |
433 | 424 |
|
434 | 425 | assert status == "200 OK" |
435 | 426 |
|
|
0 commit comments