1
+ import contextlib
1
2
import os
3
+ import sys
2
4
3
5
import pytest
4
6
from datetime import datetime
7
+ from pathlib import Path
5
8
from unittest import mock
6
9
7
10
from django import VERSION as DJANGO_VERSION
17
20
from sentry_sdk import start_transaction
18
21
from sentry_sdk .consts import SPANDATA
19
22
from sentry_sdk .integrations .django import DjangoIntegration
20
- from sentry_sdk .tracing_utils import record_sql_queries
23
+ from sentry_sdk .tracing_utils import _get_frame_module_abs_path , record_sql_queries
24
+ from sentry_sdk .utils import _module_in_list
21
25
22
26
from tests .conftest import unpack_werkzeug_response
23
27
from tests .integrations .django .utils import pytest_mark_django_db_decorator
@@ -283,7 +287,10 @@ def test_query_source_with_in_app_exclude(sentry_init, client, capture_events):
283
287
284
288
@pytest .mark .forked
285
289
@pytest_mark_django_db_decorator (transaction = True )
286
- def test_query_source_with_in_app_include (sentry_init , client , capture_events ):
290
+ @pytest .mark .parametrize ("django_outside_of_project_root" , [False , True ])
291
+ def test_query_source_with_in_app_include (
292
+ sentry_init , client , capture_events , django_outside_of_project_root
293
+ ):
287
294
sentry_init (
288
295
integrations = [DjangoIntegration ()],
289
296
send_default_pii = True ,
@@ -301,8 +308,35 @@ def test_query_source_with_in_app_include(sentry_init, client, capture_events):
301
308
302
309
events = capture_events ()
303
310
304
- _ , status , _ = unpack_werkzeug_response (client .get (reverse ("postgres_select_orm" )))
305
- assert status == "200 OK"
311
+ # Simulate Django installation outside of the project root
312
+ original_get_frame_module_abs_path = _get_frame_module_abs_path
313
+
314
+ def patched_get_frame_module_abs_path_function (frame ):
315
+ result = original_get_frame_module_abs_path (frame )
316
+ namespace = frame .f_globals .get ("__name__" )
317
+ print (f"{ namespace = } " )
318
+ print (f"{ _module_in_list (namespace , ["django" ])= } " )
319
+ if _module_in_list (namespace , ["django" ]):
320
+ print (f"{ frame .f_code .co_filename = } " )
321
+ result = str (
322
+ Path ("/outside-of-project-root" ) / frame .f_code .co_filename [1 :]
323
+ )
324
+ return result
325
+
326
+ patched_get_frame_module_abs_path = (
327
+ mock .patch (
328
+ "sentry_sdk.tracing_utils._get_frame_module_abs_path" ,
329
+ patched_get_frame_module_abs_path_function ,
330
+ )
331
+ if django_outside_of_project_root
332
+ else contextlib .suppress ()
333
+ )
334
+
335
+ with patched_get_frame_module_abs_path :
336
+ _ , status , _ = unpack_werkzeug_response (
337
+ client .get (reverse ("postgres_select_orm" ))
338
+ )
339
+ assert status == "200 OK"
306
340
307
341
(event ,) = events
308
342
for span in event ["spans" ]:
0 commit comments