12
12
# See the License for the specific language governing permissions and
13
13
# limitations under the License.
14
14
15
+ from unittest import mock
16
+
15
17
from opentelemetry import trace as trace_api
16
- from opentelemetry .ext . dbapi import DatabaseApiIntegration
18
+ from opentelemetry .ext import dbapi
17
19
from opentelemetry .test .test_base import TestBase
18
20
19
21
@@ -35,7 +37,7 @@ def test_span_succeeded(self):
35
37
"host" : "server_host" ,
36
38
"user" : "user" ,
37
39
}
38
- db_integration = DatabaseApiIntegration (
40
+ db_integration = dbapi . DatabaseApiIntegration (
39
41
self .tracer , "testcomponent" , "testtype" , connection_attributes
40
42
)
41
43
mock_connection = db_integration .wrapped_connection (
@@ -66,7 +68,9 @@ def test_span_succeeded(self):
66
68
)
67
69
68
70
def test_span_failed (self ):
69
- db_integration = DatabaseApiIntegration (self .tracer , "testcomponent" )
71
+ db_integration = dbapi .DatabaseApiIntegration (
72
+ self .tracer , "testcomponent"
73
+ )
70
74
mock_connection = db_integration .wrapped_connection (
71
75
mock_connect , {}, {}
72
76
)
@@ -85,7 +89,9 @@ def test_span_failed(self):
85
89
self .assertEqual (span .status .description , "Test Exception" )
86
90
87
91
def test_executemany (self ):
88
- db_integration = DatabaseApiIntegration (self .tracer , "testcomponent" )
92
+ db_integration = dbapi .DatabaseApiIntegration (
93
+ self .tracer , "testcomponent"
94
+ )
89
95
mock_connection = db_integration .wrapped_connection (
90
96
mock_connect , {}, {}
91
97
)
@@ -97,7 +103,9 @@ def test_executemany(self):
97
103
self .assertEqual (span .attributes ["db.statement" ], "Test query" )
98
104
99
105
def test_callproc (self ):
100
- db_integration = DatabaseApiIntegration (self .tracer , "testcomponent" )
106
+ db_integration = dbapi .DatabaseApiIntegration (
107
+ self .tracer , "testcomponent"
108
+ )
101
109
mock_connection = db_integration .wrapped_connection (
102
110
mock_connect , {}, {}
103
111
)
@@ -110,6 +118,26 @@ def test_callproc(self):
110
118
span .attributes ["db.statement" ], "Test stored procedure"
111
119
)
112
120
121
+ @mock .patch ("opentelemetry.ext.dbapi" )
122
+ def test_wrap_connect (self , mock_dbapi ):
123
+ dbapi .wrap_connect (self .tracer , mock_dbapi , "connect" , "-" )
124
+ connection = mock_dbapi .connect ()
125
+ self .assertEqual (mock_dbapi .connect .call_count , 1 )
126
+ self .assertIsInstance (connection , dbapi .TracedConnectionProxy )
127
+ self .assertIsInstance (connection .__wrapped__ , mock .Mock )
128
+
129
+ @mock .patch ("opentelemetry.ext.dbapi" )
130
+ def test_unwrap_connect (self , mock_dbapi ):
131
+ dbapi .wrap_connect (self .tracer , mock_dbapi , "connect" , "-" )
132
+ connection = mock_dbapi .connect ()
133
+ self .assertEqual (mock_dbapi .connect .call_count , 1 )
134
+ self .assertIsInstance (connection , dbapi .TracedConnectionProxy )
135
+
136
+ dbapi .unwrap_connect (mock_dbapi , "connect" )
137
+ connection = mock_dbapi .connect ()
138
+ self .assertEqual (mock_dbapi .connect .call_count , 2 )
139
+ self .assertIsInstance (connection , mock .Mock )
140
+
113
141
114
142
# pylint: disable=unused-argument
115
143
def mock_connect (* args , ** kwargs ):
0 commit comments