Skip to content

Commit 0987233

Browse files
Add tests
1 parent 501b7bd commit 0987233

File tree

1 file changed

+204
-2
lines changed

1 file changed

+204
-2
lines changed

instrumentation/opentelemetry-instrumentation-pymysql/tests/test_pymysql_integration.py

Lines changed: 204 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ def test_instrument_connection(self, mock_connect):
115115
@mock.patch("opentelemetry.instrumentation.dbapi.instrument_connection")
116116
@mock.patch("pymysql.connect")
117117
# pylint: disable=unused-argument
118-
def test_instrument_connection_enable_commenter(
118+
def test_instrument_connection_enable_commenter_dbapi_kwargs(
119119
self,
120120
mock_connect,
121121
mock_instrument_connection,
@@ -132,10 +132,109 @@ def test_instrument_connection_enable_commenter(
132132
self.assertEqual(kwargs["enable_commenter"], True)
133133
self.assertEqual(kwargs["commenter_options"], {"foo": True})
134134

135+
def test_instrument_connection_with_dbapi_sqlcomment_enabled(self):
136+
mock_connect_module = mock.MagicMock(
137+
__name__="pymysql",
138+
__version__="foobar",
139+
threadsafety="123",
140+
apilevel="123",
141+
paramstyle="test",
142+
)
143+
mock_connect_module.get_client_info.return_value = "foobaz"
144+
mock_cursor = mock_connect_module.connect().cursor()
145+
mock_connection = mock.MagicMock()
146+
mock_connection.cursor.return_value = mock_cursor
147+
148+
with mock.patch(
149+
"opentelemetry.instrumentation.pymysql.pymysql",
150+
mock_connect_module,
151+
):
152+
cnx_proxy = PyMySQLInstrumentor().instrument_connection(
153+
mock_connection,
154+
enable_commenter=True,
155+
)
156+
cnx_proxy.cursor().execute("Select 1;")
157+
158+
spans_list = self.memory_exporter.get_finished_spans()
159+
span = spans_list[0]
160+
span_id = format(span.get_span_context().span_id, "016x")
161+
trace_id = format(span.get_span_context().trace_id, "032x")
162+
self.assertEqual(
163+
mock_cursor.execute.call_args[0][0],
164+
f"Select 1 /*db_driver='pymysql%%3Afoobar',dbapi_level='123',dbapi_threadsafety='123',driver_paramstyle='test',mysql_client_version='foobaz',traceparent='00-{trace_id}-{span_id}-01'*/;",
165+
)
166+
167+
def test_instrument_connection_with_dbapi_sqlcomment_enabled_with_options(
168+
self,
169+
):
170+
mock_connect_module = mock.MagicMock(
171+
__name__="pymysql",
172+
__version__="foobar",
173+
threadsafety="123",
174+
apilevel="123",
175+
paramstyle="test",
176+
)
177+
mock_connect_module.get_client_info.return_value = "foobaz"
178+
mock_cursor = mock_connect_module.connect().cursor()
179+
mock_connection = mock.MagicMock()
180+
mock_connection.cursor.return_value = mock_cursor
181+
182+
with mock.patch(
183+
"opentelemetry.instrumentation.pymysql.pymysql",
184+
mock_connect_module,
185+
):
186+
cnx_proxy = PyMySQLInstrumentor().instrument_connection(
187+
mock_connection,
188+
enable_commenter=True,
189+
commenter_options={
190+
"dbapi_level": False,
191+
"dbapi_threadsafety": True,
192+
"driver_paramstyle": False,
193+
},
194+
)
195+
cnx_proxy.cursor().execute("Select 1;")
196+
197+
spans_list = self.memory_exporter.get_finished_spans()
198+
span = spans_list[0]
199+
span_id = format(span.get_span_context().span_id, "016x")
200+
trace_id = format(span.get_span_context().trace_id, "032x")
201+
self.assertEqual(
202+
mock_cursor.execute.call_args[0][0],
203+
f"Select 1 /*db_driver='pymysql%%3Afoobar',dbapi_threadsafety='123',mysql_client_version='foobaz',traceparent='00-{trace_id}-{span_id}-01'*/;",
204+
)
205+
206+
def test_instrument_connection_with_dbapi_sqlcomment_not_enabled_default(
207+
self,
208+
):
209+
mock_connect_module = mock.MagicMock(
210+
__name__="pymysql",
211+
__version__="foobar",
212+
threadsafety="123",
213+
apilevel="123",
214+
paramstyle="test",
215+
)
216+
mock_connect_module.get_client_info.return_value = "foobaz"
217+
mock_cursor = mock_connect_module.connect().cursor()
218+
mock_connection = mock.MagicMock()
219+
mock_connection.cursor.return_value = mock_cursor
220+
221+
with mock.patch(
222+
"opentelemetry.instrumentation.pymysql.pymysql",
223+
mock_connect_module,
224+
):
225+
cnx_proxy = PyMySQLInstrumentor().instrument_connection(
226+
mock_connection,
227+
)
228+
cnx_proxy.cursor().execute("Select 1;")
229+
self.assertEqual(
230+
mock_cursor.execute.call_args[0][0],
231+
"Select 1;",
232+
)
233+
135234
@mock.patch("opentelemetry.instrumentation.dbapi.wrap_connect")
136235
@mock.patch("pymysql.connect")
137236
# pylint: disable=unused-argument
138-
def test__instrument_enable_commenter(
237+
def test_instrument_enable_commenter_dbapi_kwargs(
139238
self,
140239
mock_connect,
141240
mock_wrap_connect,
@@ -148,6 +247,109 @@ def test__instrument_enable_commenter(
148247
self.assertEqual(kwargs["enable_commenter"], True)
149248
self.assertEqual(kwargs["commenter_options"], {"foo": True})
150249

250+
def test_instrument_with_dbapi_sqlcomment_enabled(
251+
self,
252+
):
253+
mock_connect_module = mock.MagicMock(
254+
__name__="pymysql",
255+
__version__="foobar",
256+
threadsafety="123",
257+
apilevel="123",
258+
paramstyle="test",
259+
)
260+
mock_connect_module.get_client_info.return_value = "foobaz"
261+
mock_cursor = mock_connect_module.connect().cursor()
262+
mock_connection = mock.MagicMock()
263+
mock_connection.cursor.return_value = mock_cursor
264+
265+
with mock.patch(
266+
"opentelemetry.instrumentation.pymysql.pymysql",
267+
mock_connect_module,
268+
):
269+
PyMySQLInstrumentor()._instrument(
270+
enable_commenter=True,
271+
)
272+
cnx = mock_connect_module.connect(database="test")
273+
cursor = cnx.cursor()
274+
cursor.execute("Select 1;")
275+
276+
spans_list = self.memory_exporter.get_finished_spans()
277+
span = spans_list[0]
278+
span_id = format(span.get_span_context().span_id, "016x")
279+
trace_id = format(span.get_span_context().trace_id, "032x")
280+
self.assertEqual(
281+
mock_cursor.execute.call_args[0][0],
282+
f"Select 1 /*db_driver='pymysql%%3Afoobar',dbapi_level='123',dbapi_threadsafety='123',driver_paramstyle='test',mysql_client_version='foobaz',traceparent='00-{trace_id}-{span_id}-01'*/;",
283+
)
284+
285+
def test_instrument_with_dbapi_sqlcomment_enabled_with_options(
286+
self,
287+
):
288+
mock_connect_module = mock.MagicMock(
289+
__name__="pymysql",
290+
__version__="foobar",
291+
threadsafety="123",
292+
apilevel="123",
293+
paramstyle="test",
294+
)
295+
mock_connect_module.get_client_info.return_value = "foobaz"
296+
mock_cursor = mock_connect_module.connect().cursor()
297+
mock_connection = mock.MagicMock()
298+
mock_connection.cursor.return_value = mock_cursor
299+
300+
with mock.patch(
301+
"opentelemetry.instrumentation.pymysql.pymysql",
302+
mock_connect_module,
303+
):
304+
PyMySQLInstrumentor()._instrument(
305+
enable_commenter=True,
306+
commenter_options={
307+
"dbapi_level": False,
308+
"dbapi_threadsafety": True,
309+
"driver_paramstyle": False,
310+
},
311+
)
312+
cnx = mock_connect_module.connect(database="test")
313+
cursor = cnx.cursor()
314+
cursor.execute("Select 1;")
315+
316+
spans_list = self.memory_exporter.get_finished_spans()
317+
span = spans_list[0]
318+
span_id = format(span.get_span_context().span_id, "016x")
319+
trace_id = format(span.get_span_context().trace_id, "032x")
320+
self.assertEqual(
321+
mock_cursor.execute.call_args[0][0],
322+
f"Select 1 /*db_driver='pymysql%%3Afoobar',dbapi_threadsafety='123',mysql_client_version='foobaz',traceparent='00-{trace_id}-{span_id}-01'*/;",
323+
)
324+
325+
def test_instrument_with_dbapi_sqlcomment_not_enabled_default(
326+
self,
327+
):
328+
mock_connect_module = mock.MagicMock(
329+
__name__="pymysql",
330+
__version__="foobar",
331+
threadsafety="123",
332+
apilevel="123",
333+
paramstyle="test",
334+
)
335+
mock_connect_module.get_client_info.return_value = "foobaz"
336+
mock_cursor = mock_connect_module.connect().cursor()
337+
mock_connection = mock.MagicMock()
338+
mock_connection.cursor.return_value = mock_cursor
339+
340+
with mock.patch(
341+
"opentelemetry.instrumentation.pymysql.pymysql",
342+
mock_connect_module,
343+
):
344+
PyMySQLInstrumentor()._instrument()
345+
cnx = mock_connect_module.connect(database="test")
346+
cursor = cnx.cursor()
347+
cursor.execute("Select 1;")
348+
self.assertEqual(
349+
mock_cursor.execute.call_args[0][0],
350+
"Select 1;",
351+
)
352+
151353
@mock.patch("pymysql.connect")
152354
# pylint: disable=unused-argument
153355
def test_uninstrument_connection(self, mock_connect):

0 commit comments

Comments
 (0)