Skip to content

Commit 7c9d299

Browse files
more tests 2
1 parent f33e219 commit 7c9d299

File tree

1 file changed

+82
-2
lines changed

1 file changed

+82
-2
lines changed

instrumentation/opentelemetry-instrumentation-mysqlclient/tests/test_mysqlclient_integration.py

Lines changed: 82 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,14 +142,53 @@ def test_instrument_connection_with_dbapi_sqlcomment_enabled(self):
142142
cnx_proxy = MySQLClientInstrumentor().instrument_connection(
143143
mock_connection,
144144
enable_commenter=True,
145-
commenter_options={"foo": True},
146145
)
147146
cnx_proxy.cursor().execute("Select 1;")
148147
self.assertRegex(
149148
mock_cursor.execute.call_args[0][0],
150149
r"Select 1 /\*db_driver='MySQLdb%%3Afoobar',dbapi_level='123',dbapi_threadsafety='123',driver_paramstyle='test',mysql_client_version='foobaz',traceparent='\d{1,2}-[a-zA-Z0-9_]{32}-[a-zA-Z0-9_]{16}-\d{1,2}'\*/;",
151150
)
152151

152+
def test_instrument_connection_with_dbapi_sqlcomment_enabled_with_options(
153+
self,
154+
):
155+
mock_cursor = mock.MagicMock()
156+
mock_cursor.execute = mock.MagicMock()
157+
mock_connection = mock.MagicMock()
158+
mock_connection.cursor.return_value = mock_cursor
159+
160+
mock_connect_module = mock.MagicMock()
161+
mock_connect_module.__name__ = "MySQLdb"
162+
mock_connect_module.threadsafety = "123"
163+
mock_connect_module.apilevel = "123"
164+
mock_connect_module.paramstyle = "test"
165+
mock_connect_module._mysql.get_client_info = mock.Mock(
166+
return_value="foobaz"
167+
)
168+
mock_connect_module.connect = mock.Mock(return_value=mock_connection)
169+
170+
with mock.patch(
171+
"opentelemetry.instrumentation.mysqlclient.MySQLdb",
172+
mock_connect_module,
173+
), mock.patch(
174+
"opentelemetry.instrumentation.dbapi.util_version",
175+
return_value="foobar",
176+
):
177+
cnx_proxy = MySQLClientInstrumentor().instrument_connection(
178+
mock_connection,
179+
enable_commenter=True,
180+
commenter_options={
181+
"dbapi_level": False,
182+
"dbapi_threadsafety": True,
183+
"driver_paramstyle": False,
184+
},
185+
)
186+
cnx_proxy.cursor().execute("Select 1;")
187+
self.assertRegex(
188+
mock_cursor.execute.call_args[0][0],
189+
r"Select 1 /\*db_driver='MySQLdb%%3Afoobar',dbapi_threadsafety='123',mysql_client_version='foobaz',traceparent='\d{1,2}-[a-zA-Z0-9_]{32}-[a-zA-Z0-9_]{16}-\d{1,2}'\*/;",
190+
)
191+
153192
def test_instrument_connection_with_dbapi_sqlcomment_not_enabled_default(
154193
self,
155194
):
@@ -229,7 +268,6 @@ def test__instrument_with_dbapi_sqlcomment_enabled(
229268
):
230269
MySQLClientInstrumentor()._instrument(
231270
enable_commenter=True,
232-
commenter_options={"foo": True},
233271
)
234272
cnx = mock_connect_module.connect(database="test")
235273
cursor = cnx.cursor()
@@ -239,6 +277,48 @@ def test__instrument_with_dbapi_sqlcomment_enabled(
239277
r"Select 1 /\*db_driver='MySQLdb%%3Afoobar',dbapi_level='123',dbapi_threadsafety='123',driver_paramstyle='test',mysql_client_version='foobaz',traceparent='\d{1,2}-[a-zA-Z0-9_]{32}-[a-zA-Z0-9_]{16}-\d{1,2}'\*/;",
240278
)
241279

280+
def test__instrument_with_dbapi_sqlcomment_enabled_with_options(
281+
self,
282+
):
283+
mock_cursor = mock.MagicMock()
284+
mock_cursor.execute = mock.MagicMock()
285+
mock_connection = mock.MagicMock()
286+
mock_connection.cursor.return_value = mock_cursor
287+
288+
mock_connect_module = mock.Mock()
289+
mock_connect_module.__name__ = "MySQLdb"
290+
mock_connect_module.threadsafety = "123"
291+
mock_connect_module.apilevel = "123"
292+
mock_connect_module.paramstyle = "test"
293+
mock_connect_module._mysql.get_client_info = mock.Mock(
294+
return_value="foobaz"
295+
)
296+
297+
mock_connect_module.connect = mock.Mock(return_value=mock_connection)
298+
299+
with mock.patch(
300+
"opentelemetry.instrumentation.mysqlclient.MySQLdb",
301+
mock_connect_module,
302+
), mock.patch(
303+
"opentelemetry.instrumentation.dbapi.util_version",
304+
return_value="foobar",
305+
):
306+
MySQLClientInstrumentor()._instrument(
307+
enable_commenter=True,
308+
commenter_options={
309+
"dbapi_level": False,
310+
"dbapi_threadsafety": True,
311+
"driver_paramstyle": False,
312+
},
313+
)
314+
cnx = mock_connect_module.connect(database="test")
315+
cursor = cnx.cursor()
316+
cursor.execute("Select 1;")
317+
self.assertRegex(
318+
mock_cursor.execute.call_args[0][0],
319+
r"Select 1 /\*db_driver='MySQLdb%%3Afoobar',dbapi_threadsafety='123',mysql_client_version='foobaz',traceparent='\d{1,2}-[a-zA-Z0-9_]{32}-[a-zA-Z0-9_]{16}-\d{1,2}'\*/;",
320+
)
321+
242322
def test__instrument_with_dbapi_sqlcomment_not_enabled_default(
243323
self,
244324
):

0 commit comments

Comments
 (0)