Skip to content

Commit 4dd9f73

Browse files
Add test
1 parent b0a9efd commit 4dd9f73

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

instrumentation/opentelemetry-instrumentation-dbapi/tests/test_dbapi_integration.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,32 @@ def test_compatible_build_version_psycopg_psycopg2_libpq(self):
303303
r"Select 1 /\*dbapi_threadsafety=123,driver_paramstyle='test',libpq_version=123,traceparent='\d{1,2}-[a-zA-Z0-9_]{32}-[a-zA-Z0-9_]{16}-\d{1,2}'\*/;",
304304
)
305305

306+
def test_executemany_mysqlconnector_integration_comment(self):
307+
connect_module = mock.MagicMock()
308+
connect_module.__name__ = "mysql.connector"
309+
connect_module.__version__ = "1.2.3"
310+
connect_module.apilevel = 123
311+
connect_module.threadsafety = 123
312+
connect_module.paramstyle = "test"
313+
314+
db_integration = dbapi.DatabaseApiIntegration(
315+
"testname",
316+
"mysql",
317+
enable_commenter=True,
318+
commenter_options={"db_driver": True, "dbapi_level": False},
319+
connect_module=connect_module,
320+
)
321+
322+
mock_connection = db_integration.wrapped_connection(
323+
mock_connect, {}, {}
324+
)
325+
cursor = mock_connection.cursor()
326+
cursor.executemany("Select 1;")
327+
self.assertRegex(
328+
cursor.query,
329+
r"Select 1 /\*db_driver='mysql.connector%%3A1.2.3',dbapi_threadsafety=123,driver_paramstyle='test',mysql_client_version='1.2.3',traceparent='\d{1,2}-[a-zA-Z0-9_]{32}-[a-zA-Z0-9_]{16}-\d{1,2}'\*/;",
330+
)
331+
306332
def test_executemany_flask_integration_comment(self):
307333
connect_module = mock.MagicMock()
308334
connect_module.__name__ = "test"
@@ -335,6 +361,11 @@ def test_executemany_flask_integration_comment(self):
335361
r"Select 1 /\*dbapi_threadsafety=123,driver_paramstyle='test',flask=1,libpq_version=123,traceparent='\d{1,2}-[a-zA-Z0-9_]{32}-[a-zA-Z0-9_]{16}-\d{1,2}'\*/;",
336362
)
337363

364+
clear_context = context.set_value(
365+
"SQLCOMMENTER_ORM_TAGS_AND_VALUES", {}, current_context
366+
)
367+
context.attach(clear_context)
368+
338369
def test_callproc(self):
339370
db_integration = dbapi.DatabaseApiIntegration(
340371
"testname", "testcomponent"
@@ -418,6 +449,12 @@ class MockCursor:
418449
def __init__(self) -> None:
419450
self.query = ""
420451
self.params = None
452+
# Mock mysql.connector modules and method
453+
self._cnx = mock.MagicMock()
454+
self._cnx._cmysql = mock.MagicMock()
455+
self._cnx._cmysql.get_client_info = mock.MagicMock(
456+
return_value="1.2.3"
457+
)
421458

422459
# pylint: disable=unused-argument, no-self-use
423460
def execute(self, query, params=None, throw_exception=False):

0 commit comments

Comments
 (0)