Skip to content

Commit 410751a

Browse files
tests: Remove forked from Django tests
1 parent f99a17b commit 410751a

File tree

6 files changed

+152
-66
lines changed

6 files changed

+152
-66
lines changed

tests/conftest.py

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,109 @@ def render_span(span):
389389
return inner
390390

391391

392+
@pytest.fixture(name="EmittedSpanMetadataEqual")
393+
def span_metadata_matcher():
394+
class EmittedSpanMetadataEqual:
395+
def __init__(
396+
self,
397+
span,
398+
check_trace_id=True,
399+
check_op=True,
400+
check_status=True,
401+
check_origin=True,
402+
check_name=True,
403+
):
404+
self.span = span
405+
406+
self.check_trace_id = check_trace_id
407+
self.check_op = check_op
408+
self.check_status = check_status
409+
self.check_origin = check_origin
410+
self.check_name = check_name
411+
412+
def __eq__(self, span):
413+
print("OP", self.span["op"], span.op)
414+
return (
415+
(not self.check_trace_id or self.span["trace_id"] == span.trace_id)
416+
and (not self.check_op or self.span["op"] == span.op)
417+
and (not self.check_status or self.span["status"] == span.status)
418+
and (not self.check_origin or self.span["origin"] == span.origin)
419+
and (
420+
not self.check_name or self.span["description"] == span.description
421+
)
422+
)
423+
424+
def __ne__(self, test_string):
425+
return not self.__eq__(test_string)
426+
427+
return EmittedSpanMetadataEqual
428+
429+
430+
@pytest.fixture(name="SpanTreeEqualUnorderedSiblings")
431+
def unordered_siblings_span_tree_matcher(EmittedSpanMetadataEqual):
432+
class SpanTreeEqualUnorderedSiblings:
433+
def __init__(self, root_span, span_tree, **kwargs):
434+
self.root_span = root_span
435+
self.span_tree = span_tree
436+
437+
self.span_matcher_kwargs = kwargs
438+
439+
def _construct_parent_to_spans_mapping(self, event):
440+
by_parent = {}
441+
for span in event["spans"]:
442+
by_parent.setdefault(span["parent_span_id"], []).append(span)
443+
444+
return by_parent
445+
446+
def _subtree_eq(
447+
self, actual_subtree_root_span, expected_subtree_root_span, by_parent
448+
):
449+
if actual_subtree_root_span["span_id"] not in by_parent:
450+
return expected_subtree_root_span == EmittedSpanMetadataEqual(
451+
actual_subtree_root_span, **self.span_matcher_kwargs
452+
)
453+
454+
actual_span_children = by_parent[actual_subtree_root_span["span_id"]]
455+
expected_span_children = self.span_tree[expected_subtree_root_span]
456+
457+
if len(actual_span_children) != len(expected_span_children):
458+
return False
459+
460+
for expected_child in expected_span_children:
461+
found = False
462+
for actual_child in actual_span_children:
463+
if expected_child == EmittedSpanMetadataEqual(
464+
actual_child, **self.span_matcher_kwargs
465+
):
466+
found = True
467+
if not self._subtree_eq(
468+
actual_child, expected_child, by_parent
469+
):
470+
return False
471+
continue
472+
473+
if not found:
474+
return False
475+
476+
return True
477+
478+
def __eq__(self, event):
479+
by_parent = self._construct_parent_to_spans_mapping(event)
480+
root_span = event["contexts"]["trace"]
481+
482+
if self.root_span != EmittedSpanMetadataEqual(
483+
root_span, **self.span_matcher_kwargs
484+
):
485+
return False
486+
487+
return self._subtree_eq(root_span, self.root_span, by_parent)
488+
489+
def __ne__(self, test_string):
490+
return not self.__eq__(test_string)
491+
492+
return SpanTreeEqualUnorderedSiblings
493+
494+
392495
@pytest.fixture(name="StringContaining")
393496
def string_containing_matcher():
394497
"""

tests/integrations/django/asgi/test_asgi.py

Lines changed: 49 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import pytest
1111
from channels.testing import HttpCommunicator
1212
from sentry_sdk import capture_message
13+
from sentry_sdk.tracing import Span
1314
from sentry_sdk.integrations.django import DjangoIntegration
1415
from sentry_sdk.integrations.django.asgi import _asgi_middleware_mixin_factory
1516
from tests.integrations.django.myapp.asgi import channels_application
@@ -29,7 +30,6 @@
2930

3031
@pytest.mark.parametrize("application", APPS)
3132
@pytest.mark.asyncio
32-
@pytest.mark.forked
3333
@pytest.mark.skipif(
3434
django.VERSION < (3, 0), reason="Django ASGI support shipped in 3.0"
3535
)
@@ -86,7 +86,6 @@ async def test_basic(sentry_init, capture_events, application):
8686

8787
@pytest.mark.parametrize("application", APPS)
8888
@pytest.mark.asyncio
89-
@pytest.mark.forked
9089
@pytest.mark.skipif(
9190
django.VERSION < (3, 1), reason="async views have been introduced in Django 3.1"
9291
)
@@ -119,7 +118,6 @@ async def test_async_views(sentry_init, capture_events, application):
119118
@pytest.mark.parametrize("application", APPS)
120119
@pytest.mark.parametrize("endpoint", ["/sync/thread_ids", "/async/thread_ids"])
121120
@pytest.mark.asyncio
122-
@pytest.mark.forked
123121
@pytest.mark.skipif(
124122
django.VERSION < (3, 1), reason="async views have been introduced in Django 3.1"
125123
)
@@ -165,7 +163,6 @@ async def test_active_thread_id(
165163

166164

167165
@pytest.mark.asyncio
168-
@pytest.mark.forked
169166
@pytest.mark.skipif(
170167
django.VERSION < (3, 1), reason="async views have been introduced in Django 3.1"
171168
)
@@ -208,7 +205,6 @@ async def test_async_views_concurrent_execution(sentry_init, settings):
208205

209206

210207
@pytest.mark.asyncio
211-
@pytest.mark.forked
212208
@pytest.mark.skipif(
213209
django.VERSION < (3, 1), reason="async views have been introduced in Django 3.1"
214210
)
@@ -255,12 +251,11 @@ async def test_async_middleware_that_is_function_concurrent_execution(
255251

256252

257253
@pytest.mark.asyncio
258-
@pytest.mark.forked
259254
@pytest.mark.skipif(
260255
django.VERSION < (3, 1), reason="async views have been introduced in Django 3.1"
261256
)
262257
async def test_async_middleware_spans(
263-
sentry_init, render_span_tree, capture_events, settings
258+
sentry_init, SpanTreeEqualUnorderedSiblings, capture_events, settings
264259
):
265260
settings.MIDDLEWARE = [
266261
"django.contrib.sessions.middleware.SessionMiddleware",
@@ -286,26 +281,57 @@ async def test_async_middleware_spans(
286281

287282
(transaction,) = events
288283

289-
assert (
290-
render_span_tree(transaction)
291-
== """\
292-
- op="http.server": description=null
293-
- op="event.django": description="django.db.reset_queries"
294-
- op="event.django": description="django.db.close_old_connections"
295-
- op="middleware.django": description="django.contrib.sessions.middleware.SessionMiddleware.__acall__"
296-
- op="middleware.django": description="django.contrib.auth.middleware.AuthenticationMiddleware.__acall__"
297-
- op="middleware.django": description="django.middleware.csrf.CsrfViewMiddleware.__acall__"
298-
- op="middleware.django": description="tests.integrations.django.myapp.settings.TestMiddleware.__acall__"
299-
- op="middleware.django": description="django.middleware.csrf.CsrfViewMiddleware.process_view"
300-
- op="view.render": description="simple_async_view"
301-
- op="event.django": description="django.db.close_old_connections"
302-
- op="event.django": description="django.core.cache.close_caches"
303-
- op="event.django": description="django.core.handlers.base.reset_urlconf\""""
284+
http_server_span = Span(op="http.server", name=None)
285+
session_middleware_span = Span(
286+
op="middleware.django",
287+
name="django.contrib.sessions.middleware.SessionMiddleware.__acall__",
288+
)
289+
authentication_middleware_span = Span(
290+
op="middleware.django",
291+
name="django.contrib.auth.middleware.AuthenticationMiddleware.__acall__",
292+
)
293+
csrf_view_middleware_span = Span(
294+
op="middleware.django",
295+
name="django.middleware.csrf.CsrfViewMiddleware.__acall__",
296+
)
297+
test_middleware_span = Span(
298+
op="middleware.django",
299+
name="tests.integrations.django.myapp.settings.TestMiddleware.__acall__",
300+
)
301+
302+
span_tree = {
303+
http_server_span: {
304+
session_middleware_span,
305+
Span(op="event.django", name="django.db.reset_queries"),
306+
Span(op="event.django", name="django.db.close_old_connections"),
307+
Span(op="event.django", name="django.db.close_old_connections"),
308+
Span(op="event.django", name="django.core.cache.close_caches"),
309+
Span(op="event.django", name="django.core.handlers.base.reset_urlconf"),
310+
},
311+
session_middleware_span: {authentication_middleware_span},
312+
authentication_middleware_span: {csrf_view_middleware_span},
313+
csrf_view_middleware_span: {test_middleware_span},
314+
test_middleware_span: {
315+
Span(
316+
op="middleware.django",
317+
name="django.middleware.csrf.CsrfViewMiddleware.process_view",
318+
),
319+
Span(op="view.render", name="simple_async_view"),
320+
},
321+
}
322+
323+
assert transaction == SpanTreeEqualUnorderedSiblings(
324+
http_server_span,
325+
span_tree,
326+
check_trace_id=False,
327+
check_op=True,
328+
check_status=False,
329+
check_origin=False,
330+
check_name=True,
304331
)
305332

306333

307334
@pytest.mark.asyncio
308-
@pytest.mark.forked
309335
@pytest.mark.skipif(
310336
django.VERSION < (3, 1), reason="async views have been introduced in Django 3.1"
311337
)
@@ -333,7 +359,6 @@ async def test_has_trace_if_performance_enabled(sentry_init, capture_events):
333359

334360

335361
@pytest.mark.asyncio
336-
@pytest.mark.forked
337362
@pytest.mark.skipif(
338363
django.VERSION < (3, 1), reason="async views have been introduced in Django 3.1"
339364
)
@@ -364,7 +389,6 @@ async def test_has_trace_if_performance_disabled(sentry_init, capture_events):
364389

365390

366391
@pytest.mark.asyncio
367-
@pytest.mark.forked
368392
@pytest.mark.skipif(
369393
django.VERSION < (3, 1), reason="async views have been introduced in Django 3.1"
370394
)
@@ -398,7 +422,6 @@ async def test_trace_from_headers_if_performance_enabled(sentry_init, capture_ev
398422

399423

400424
@pytest.mark.asyncio
401-
@pytest.mark.forked
402425
@pytest.mark.skipif(
403426
django.VERSION < (3, 1), reason="async views have been introduced in Django 3.1"
404427
)
@@ -529,7 +552,6 @@ async def test_trace_from_headers_if_performance_disabled(sentry_init, capture_e
529552
],
530553
)
531554
@pytest.mark.asyncio
532-
@pytest.mark.forked
533555
@pytest.mark.skipif(
534556
django.VERSION < (3, 1), reason="async views have been introduced in Django 3.1"
535557
)

tests/integrations/django/test_basic.py

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,6 @@ def test_trace_from_headers_if_performance_disabled(
268268
assert error_event["contexts"]["trace"]["trace_id"] == trace_id
269269

270270

271-
@pytest.mark.forked
272271
@pytest_mark_django_db_decorator()
273272
def test_user_captured(sentry_init, client, capture_events):
274273
sentry_init(integrations=[DjangoIntegration()], send_default_pii=True)
@@ -290,7 +289,6 @@ def test_user_captured(sentry_init, client, capture_events):
290289
}
291290

292291

293-
@pytest.mark.forked
294292
@pytest_mark_django_db_decorator()
295293
def test_queryset_repr(sentry_init, capture_events):
296294
sentry_init(integrations=[DjangoIntegration()])
@@ -313,7 +311,6 @@ def test_queryset_repr(sentry_init, capture_events):
313311
)
314312

315313

316-
@pytest.mark.forked
317314
@pytest_mark_django_db_decorator()
318315
def test_context_nested_queryset_repr(sentry_init, capture_events):
319316
sentry_init(integrations=[DjangoIntegration()])
@@ -363,7 +360,6 @@ def test_500(sentry_init, client):
363360
assert content == "Sentry error."
364361

365362

366-
@pytest.mark.forked
367363
def test_management_command_raises():
368364
# This just checks for our assumption that Django passes through all
369365
# exceptions by default, so our excepthook can be used for management
@@ -372,7 +368,6 @@ def test_management_command_raises():
372368
execute_from_command_line(["manage.py", "mycrash"])
373369

374370

375-
@pytest.mark.forked
376371
@pytest_mark_django_db_decorator()
377372
@pytest.mark.parametrize("with_integration", [True, False])
378373
def test_sql_queries(sentry_init, capture_events, with_integration):
@@ -405,7 +400,6 @@ def test_sql_queries(sentry_init, capture_events, with_integration):
405400
assert crumb["data"]["db.params"] == [123]
406401

407402

408-
@pytest.mark.forked
409403
@pytest_mark_django_db_decorator()
410404
def test_sql_dict_query_params(sentry_init, capture_events):
411405
sentry_init(
@@ -440,7 +434,6 @@ def test_sql_dict_query_params(sentry_init, capture_events):
440434
assert crumb["data"]["db.params"] == {"my_foo": 10}
441435

442436

443-
@pytest.mark.forked
444437
@pytest_mark_django_db_decorator()
445438
def test_response_trace(sentry_init, client, capture_events, render_span_tree):
446439
pytest.importorskip("rest_framework")
@@ -470,7 +463,6 @@ def test_response_trace(sentry_init, client, capture_events, render_span_tree):
470463
lambda sql: sql.SQL('SELECT %(my_param)s FROM "foobar"'),
471464
],
472465
)
473-
@pytest.mark.forked
474466
@pytest_mark_django_db_decorator()
475467
def test_sql_psycopg2_string_composition(sentry_init, capture_events, query):
476468
sentry_init(
@@ -502,7 +494,6 @@ def test_sql_psycopg2_string_composition(sentry_init, capture_events, query):
502494
assert crumb["data"]["db.params"] == {"my_param": 10}
503495

504496

505-
@pytest.mark.forked
506497
@pytest_mark_django_db_decorator()
507498
def test_sql_psycopg2_placeholders(sentry_init, capture_events):
508499
sentry_init(
@@ -562,7 +553,6 @@ def test_sql_psycopg2_placeholders(sentry_init, capture_events):
562553
]
563554

564555

565-
@pytest.mark.forked
566556
@pytest_mark_django_db_decorator(transaction=True)
567557
def test_django_connect_trace(sentry_init, client, capture_events, render_span_tree):
568558
"""
@@ -599,7 +589,6 @@ def test_django_connect_trace(sentry_init, client, capture_events, render_span_t
599589
assert '- op="db": description="connect"' in render_span_tree(event)
600590

601591

602-
@pytest.mark.forked
603592
@pytest_mark_django_db_decorator(transaction=True)
604593
def test_django_connect_breadcrumbs(sentry_init, capture_events):
605594
"""
@@ -635,7 +624,6 @@ def test_django_connect_breadcrumbs(sentry_init, capture_events):
635624
]
636625

637626

638-
@pytest.mark.forked
639627
@pytest_mark_django_db_decorator(transaction=True)
640628
def test_db_connection_span_data(sentry_init, client, capture_events):
641629
sentry_init(
@@ -958,7 +946,6 @@ def test_render_spans(sentry_init, client, capture_events, render_span_tree):
958946

959947

960948
@pytest.mark.skipif(DJANGO_VERSION < (1, 9), reason="Requires Django >= 1.9")
961-
@pytest.mark.forked
962949
@pytest_mark_django_db_decorator()
963950
def test_render_spans_queryset_in_data(sentry_init, client, capture_events):
964951
sentry_init(

0 commit comments

Comments
 (0)