Skip to content

Commit

Permalink
more tests 2
Browse files Browse the repository at this point in the history
  • Loading branch information
tammy-baylis-swi committed Nov 20, 2024
1 parent f33e219 commit 7c9d299
Showing 1 changed file with 82 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -142,14 +142,53 @@ def test_instrument_connection_with_dbapi_sqlcomment_enabled(self):
cnx_proxy = MySQLClientInstrumentor().instrument_connection(
mock_connection,
enable_commenter=True,
commenter_options={"foo": True},
)
cnx_proxy.cursor().execute("Select 1;")
self.assertRegex(
mock_cursor.execute.call_args[0][0],
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}'\*/;",
)

def test_instrument_connection_with_dbapi_sqlcomment_enabled_with_options(
self,
):
mock_cursor = mock.MagicMock()
mock_cursor.execute = mock.MagicMock()
mock_connection = mock.MagicMock()
mock_connection.cursor.return_value = mock_cursor

mock_connect_module = mock.MagicMock()
mock_connect_module.__name__ = "MySQLdb"
mock_connect_module.threadsafety = "123"
mock_connect_module.apilevel = "123"
mock_connect_module.paramstyle = "test"
mock_connect_module._mysql.get_client_info = mock.Mock(
return_value="foobaz"
)
mock_connect_module.connect = mock.Mock(return_value=mock_connection)

with mock.patch(
"opentelemetry.instrumentation.mysqlclient.MySQLdb",
mock_connect_module,
), mock.patch(
"opentelemetry.instrumentation.dbapi.util_version",
return_value="foobar",
):
cnx_proxy = MySQLClientInstrumentor().instrument_connection(
mock_connection,
enable_commenter=True,
commenter_options={
"dbapi_level": False,
"dbapi_threadsafety": True,
"driver_paramstyle": False,
},
)
cnx_proxy.cursor().execute("Select 1;")
self.assertRegex(
mock_cursor.execute.call_args[0][0],
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}'\*/;",
)

def test_instrument_connection_with_dbapi_sqlcomment_not_enabled_default(
self,
):
Expand Down Expand Up @@ -229,7 +268,6 @@ def test__instrument_with_dbapi_sqlcomment_enabled(
):
MySQLClientInstrumentor()._instrument(
enable_commenter=True,
commenter_options={"foo": True},
)
cnx = mock_connect_module.connect(database="test")
cursor = cnx.cursor()
Expand All @@ -239,6 +277,48 @@ def test__instrument_with_dbapi_sqlcomment_enabled(
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}'\*/;",
)

def test__instrument_with_dbapi_sqlcomment_enabled_with_options(
self,
):
mock_cursor = mock.MagicMock()
mock_cursor.execute = mock.MagicMock()
mock_connection = mock.MagicMock()
mock_connection.cursor.return_value = mock_cursor

mock_connect_module = mock.Mock()
mock_connect_module.__name__ = "MySQLdb"
mock_connect_module.threadsafety = "123"
mock_connect_module.apilevel = "123"
mock_connect_module.paramstyle = "test"
mock_connect_module._mysql.get_client_info = mock.Mock(
return_value="foobaz"
)

mock_connect_module.connect = mock.Mock(return_value=mock_connection)

with mock.patch(
"opentelemetry.instrumentation.mysqlclient.MySQLdb",
mock_connect_module,
), mock.patch(
"opentelemetry.instrumentation.dbapi.util_version",
return_value="foobar",
):
MySQLClientInstrumentor()._instrument(
enable_commenter=True,
commenter_options={
"dbapi_level": False,
"dbapi_threadsafety": True,
"driver_paramstyle": False,
},
)
cnx = mock_connect_module.connect(database="test")
cursor = cnx.cursor()
cursor.execute("Select 1;")
self.assertRegex(
mock_cursor.execute.call_args[0][0],
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}'\*/;",
)

def test__instrument_with_dbapi_sqlcomment_not_enabled_default(
self,
):
Expand Down

0 comments on commit 7c9d299

Please sign in to comment.