From 916bf5e883cb296b9d3a9378a69cdc07bf3d753e Mon Sep 17 00:00:00 2001 From: tammy-baylis-swi Date: Wed, 20 Nov 2024 09:39:36 -0800 Subject: [PATCH] Add test --- .../tests/test_dbapi_integration.py | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/instrumentation/opentelemetry-instrumentation-dbapi/tests/test_dbapi_integration.py b/instrumentation/opentelemetry-instrumentation-dbapi/tests/test_dbapi_integration.py index ae595fb430..1236750ec7 100644 --- a/instrumentation/opentelemetry-instrumentation-dbapi/tests/test_dbapi_integration.py +++ b/instrumentation/opentelemetry-instrumentation-dbapi/tests/test_dbapi_integration.py @@ -592,6 +592,49 @@ def test_instrument_connection(self): connection2 = dbapi.instrument_connection(self.tracer, connection, "-") self.assertIs(connection2.__wrapped__, connection) + @mock.patch("opentelemetry.instrumentation.dbapi.DatabaseApiIntegration") + def test_instrument_connection_kwargs_defaults(self, mock_dbapiint): + connection = mock.Mock() + # Avoid get_attributes failing because can't concatenate mock + connection.database = "-" + dbapi.instrument_connection(self.tracer, connection, "-") + kwargs = mock_dbapiint.call_args[1] + self.assertEqual(kwargs["connection_attributes"], None) + self.assertEqual(kwargs["version"], "") + self.assertEqual(kwargs["tracer_provider"], None) + self.assertEqual(kwargs["capture_parameters"], False) + self.assertEqual(kwargs["enable_commenter"], False) + self.assertEqual(kwargs["commenter_options"], None) + self.assertEqual(kwargs["connect_module"], None) + + @mock.patch("opentelemetry.instrumentation.dbapi.DatabaseApiIntegration") + def test_instrument_connection_kwargs_provided(self, mock_dbapiint): + mock_tracer_provider = mock.MagicMock() + mock_connect_module = mock.MagicMock() + connection = mock.Mock() + # Avoid get_attributes failing because can't concatenate mock + connection.database = "-" + dbapi.instrument_connection( + self.tracer, + connection, + "-", + connection_attributes={"foo": "bar"}, + version="test", + tracer_provider=mock_tracer_provider, + capture_parameters=True, + enable_commenter=True, + commenter_options={"foo": "bar"}, + connect_module=mock_connect_module, + ) + kwargs = mock_dbapiint.call_args[1] + self.assertEqual(kwargs["connection_attributes"], {"foo": "bar"}) + self.assertEqual(kwargs["version"], "test") + self.assertEqual(kwargs["tracer_provider"], mock_tracer_provider) + self.assertEqual(kwargs["capture_parameters"], True) + self.assertEqual(kwargs["enable_commenter"], True) + self.assertEqual(kwargs["commenter_options"], {"foo": "bar"}) + self.assertEqual(kwargs["connect_module"], mock_connect_module) + def test_uninstrument_connection(self): connection = mock.Mock() # Set connection.database to avoid a failure because mock can't