forked from open-telemetry/opentelemetry-python-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_sqlcommenter.py
117 lines (99 loc) · 3.9 KB
/
test_sqlcommenter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# Copyright The OpenTelemetry Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# pylint: disable=no-name-in-module
from unittest.mock import MagicMock, patch
import pytest
from django import VERSION, conf
from django.http import HttpResponse
from django.test.utils import setup_test_environment, teardown_test_environment
from opentelemetry.instrumentation.django import DjangoInstrumentor
from opentelemetry.instrumentation.django.middleware.sqlcommenter_middleware import (
SqlCommenter,
_QueryWrapper,
)
from opentelemetry.test.wsgitestutil import WsgiTestBase
DJANGO_2_0 = VERSION >= (2, 0)
_django_instrumentor = DjangoInstrumentor()
class TestMiddleware(WsgiTestBase):
@classmethod
def setUpClass(cls):
conf.settings.configure(
SQLCOMMENTER_WITH_FRAMEWORK=False,
SQLCOMMENTER_WITH_DB_DRIVER=False,
)
super().setUpClass()
def setUp(self):
super().setUp()
setup_test_environment()
_django_instrumentor.instrument(is_sql_commentor_enabled=True)
def tearDown(self):
super().tearDown()
teardown_test_environment()
_django_instrumentor.uninstrument()
@classmethod
def tearDownClass(cls):
super().tearDownClass()
conf.settings = conf.LazySettings()
@patch(
"opentelemetry.instrumentation.django.middleware.sqlcommenter_middleware.SqlCommenter"
)
def test_middleware_added(self, sqlcommenter_middleware):
instance = sqlcommenter_middleware.return_value
instance.get_response = HttpResponse()
if DJANGO_2_0:
middleware = conf.settings.MIDDLEWARE
else:
middleware = conf.settings.MIDDLEWARE_CLASSES
self.assertTrue(
"opentelemetry.instrumentation.django.middleware.sqlcommenter_middleware.SqlCommenter"
in middleware
)
@patch(
"opentelemetry.instrumentation.django.middleware.sqlcommenter_middleware.get_opentelemetry_values"
)
def test_query_wrapper(self, trace_capture):
requests_mock = MagicMock()
requests_mock.resolver_match.view_name = "view"
requests_mock.resolver_match.route = "route"
requests_mock.resolver_match.app_name = "app"
trace_capture.return_value = {
"traceparent": "*traceparent='00-000000000000000000000000deadbeef-000000000000beef-00"
}
qw_instance = _QueryWrapper(requests_mock)
execute_mock_obj = MagicMock()
qw_instance(
execute_mock_obj,
"Select 1",
MagicMock("test"),
MagicMock("test1"),
MagicMock(),
)
output_sql = execute_mock_obj.call_args[0][0]
self.assertEqual(
output_sql,
"Select 1 /*app_name='app',controller='view',route='route',traceparent='%%2Atraceparent%%3D%%2700-0000000"
"00000000000000000deadbeef-000000000000beef-00'*/",
)
@patch(
"opentelemetry.instrumentation.django.middleware.sqlcommenter_middleware._QueryWrapper"
)
def test_multiple_connection_support(self, query_wrapper):
if not DJANGO_2_0:
pytest.skip()
requests_mock = MagicMock()
get_response = MagicMock()
sql_instance = SqlCommenter(get_response)
sql_instance(requests_mock)
# check if query_wrapper is added to the context for 2 databases
self.assertEqual(query_wrapper.call_count, 2)