11import django
2+ from django .views .generic import TemplateView
23from django .test import modify_settings , override_settings
34import os
45import pytest
56
67from ddtrace .constants import ANALYTICS_SAMPLE_RATE_KEY , SAMPLING_PRIORITY_KEY
8+ from ddtrace .contrib .django .patch import instrument_view
79from ddtrace .ext import http , errors
810from ddtrace .ext .priority import USER_KEEP
911from ddtrace .propagation .http import HTTP_HEADER_TRACE_ID , HTTP_HEADER_PARENT_ID , HTTP_HEADER_SAMPLING_PRIORITY
@@ -492,7 +494,7 @@ def test_simple_view_options(client, test_spans):
492494 assert len (spans ) == 1
493495 span = spans [0 ]
494496 span .assert_matches (
495- resource = "tests.contrib. django.views.BasicView .options" , error = 0 ,
497+ resource = "django.views.generic.base.View .options" , error = 0 ,
496498 )
497499
498500
@@ -1239,7 +1241,9 @@ def test_custom_dispatch_template_view(client, test_spans):
12391241
12401242 spans = test_spans .get_spans ()
12411243 assert [s .resource for s in spans if s .resource .endswith ("dispatch" )] == [
1242- "tests.contrib.django.views.ComposedTemplateView.dispatch" ,
1244+ "tests.contrib.django.views.CustomDispatchMixin.dispatch" ,
1245+ "tests.contrib.django.views.AnotherCustomDispatchMixin.dispatch" ,
1246+ "django.views.generic.base.View.dispatch" ,
12431247 ]
12441248
12451249
@@ -1254,8 +1258,40 @@ def test_custom_dispatch_get_view(client, test_spans):
12541258
12551259 spans = test_spans .get_spans ()
12561260 assert [s .resource for s in spans if s .resource .endswith ("dispatch" )] == [
1257- "tests.contrib.django.views.ComposedGetView.dispatch" ,
1261+ "tests.contrib.django.views.CustomDispatchMixin.dispatch" ,
1262+ "django.views.generic.base.View.dispatch" ,
12581263 ]
12591264 assert [s .resource for s in spans if s .resource .endswith ("get" )] == [
12601265 "tests.contrib.django.views.ComposedGetView.get" ,
1266+ "tests.contrib.django.views.CustomGetView.get" ,
12611267 ]
1268+
1269+
1270+ def test_view_mixin (client , test_spans ):
1271+ from tests .contrib .django import views
1272+
1273+ assert views .DISPATCH_CALLED is False
1274+ resp = client .get ("/composed-view/" )
1275+ assert views .DISPATCH_CALLED is True
1276+
1277+ assert resp .status_code == 200
1278+ assert resp .content .strip () == b"custom dispatch"
1279+
1280+
1281+ def test_template_view_patching ():
1282+ """
1283+ Test to ensure that patching a view does not give it properties it did not have before
1284+ """
1285+ # DEV: `vars(cls)` will give you only the properties defined on that class,
1286+ # it will not traverse through __mro__ to find the property from a parent class
1287+
1288+ # We are not starting with a "dispatch" property
1289+ assert "dispatch" not in vars (TemplateView )
1290+
1291+ # Manually call internal method for patching
1292+ instrument_view (django , TemplateView )
1293+ assert "dispatch" not in vars (TemplateView )
1294+
1295+ # Patch via `as_view()`
1296+ TemplateView .as_view ()
1297+ assert "dispatch" not in vars (TemplateView )
0 commit comments