From 7c9d299966f18349d08a4e9c22f53160e647dfb0 Mon Sep 17 00:00:00 2001 From: tammy-baylis-swi Date: Tue, 19 Nov 2024 20:29:58 -0800 Subject: [PATCH] more tests 2 --- .../tests/test_mysqlclient_integration.py | 84 ++++++++++++++++++- 1 file changed, 82 insertions(+), 2 deletions(-) diff --git a/instrumentation/opentelemetry-instrumentation-mysqlclient/tests/test_mysqlclient_integration.py b/instrumentation/opentelemetry-instrumentation-mysqlclient/tests/test_mysqlclient_integration.py index c5f91aa046..600ddbb910 100644 --- a/instrumentation/opentelemetry-instrumentation-mysqlclient/tests/test_mysqlclient_integration.py +++ b/instrumentation/opentelemetry-instrumentation-mysqlclient/tests/test_mysqlclient_integration.py @@ -142,7 +142,6 @@ 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( @@ -150,6 +149,46 @@ def test_instrument_connection_with_dbapi_sqlcomment_enabled(self): 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, ): @@ -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() @@ -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, ):