2323
2424
2525class TestMySQLClientIntegration (TestBase ):
26+ # pylint: disable=invalid-name
2627 def tearDown (self ):
2728 super ().tearDown ()
2829 with self .disable_logging ():
@@ -117,21 +118,18 @@ def test_instrument_connection_enable_commenter_dbapi_kwargs(
117118 self .assertEqual (kwargs ["commenter_options" ], {"foo" : True })
118119
119120 def test_instrument_connection_with_dbapi_sqlcomment_enabled (self ):
120- mock_cursor = mock .MagicMock ()
121+ mock_connect_module = mock .MagicMock (
122+ __name__ = "MySQLdb" ,
123+ threadsafety = "123" ,
124+ apilevel = "123" ,
125+ paramstyle = "test" ,
126+ )
127+ mock_connect_module ._mysql .get_client_info .return_value = "foobaz"
128+ mock_cursor = mock_connect_module .connect ().cursor ()
121129 mock_cursor .execute = mock .MagicMock ()
122130 mock_connection = mock .MagicMock ()
123131 mock_connection .cursor .return_value = mock_cursor
124132
125- mock_connect_module = mock .MagicMock ()
126- mock_connect_module .__name__ = "MySQLdb"
127- mock_connect_module .threadsafety = "123"
128- mock_connect_module .apilevel = "123"
129- mock_connect_module .paramstyle = "test"
130- mock_connect_module ._mysql .get_client_info = mock .Mock (
131- return_value = "foobaz"
132- )
133- mock_connect_module .connect = mock .Mock (return_value = mock_connection )
134-
135133 with mock .patch (
136134 "opentelemetry.instrumentation.mysqlclient.MySQLdb" ,
137135 mock_connect_module ,
@@ -144,29 +142,31 @@ def test_instrument_connection_with_dbapi_sqlcomment_enabled(self):
144142 enable_commenter = True ,
145143 )
146144 cnx_proxy .cursor ().execute ("Select 1;" )
147- self .assertRegex (
145+
146+ spans_list = self .memory_exporter .get_finished_spans ()
147+ span = spans_list [0 ]
148+ span_id = format (span .get_span_context ().span_id , "016x" )
149+ trace_id = format (span .get_span_context ().trace_id , "032x" )
150+ self .assertEqual (
148151 mock_cursor .execute .call_args [0 ][0 ],
149- 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}'\ */;" ,
152+ f "Select 1 /*db_driver='MySQLdb%%3Afoobar',dbapi_level='123',dbapi_threadsafety='123',driver_paramstyle='test',mysql_client_version='foobaz',traceparent='00- { trace_id } - { span_id } -01' */;" ,
150153 )
151154
152155 def test_instrument_connection_with_dbapi_sqlcomment_enabled_with_options (
153156 self ,
154157 ):
155- mock_cursor = mock .MagicMock ()
158+ mock_connect_module = mock .MagicMock (
159+ __name__ = "MySQLdb" ,
160+ threadsafety = "123" ,
161+ apilevel = "123" ,
162+ paramstyle = "test" ,
163+ )
164+ mock_connect_module ._mysql .get_client_info .return_value = "foobaz"
165+ mock_cursor = mock_connect_module .connect ().cursor ()
156166 mock_cursor .execute = mock .MagicMock ()
157167 mock_connection = mock .MagicMock ()
158168 mock_connection .cursor .return_value = mock_cursor
159169
160- mock_connect_module = mock .MagicMock ()
161- mock_connect_module .__name__ = "MySQLdb"
162- mock_connect_module .threadsafety = "123"
163- mock_connect_module .apilevel = "123"
164- mock_connect_module .paramstyle = "test"
165- mock_connect_module ._mysql .get_client_info = mock .Mock (
166- return_value = "foobaz"
167- )
168- mock_connect_module .connect = mock .Mock (return_value = mock_connection )
169-
170170 with mock .patch (
171171 "opentelemetry.instrumentation.mysqlclient.MySQLdb" ,
172172 mock_connect_module ,
@@ -184,30 +184,31 @@ def test_instrument_connection_with_dbapi_sqlcomment_enabled_with_options(
184184 },
185185 )
186186 cnx_proxy .cursor ().execute ("Select 1;" )
187- self .assertRegex (
187+
188+ spans_list = self .memory_exporter .get_finished_spans ()
189+ span = spans_list [0 ]
190+ span_id = format (span .get_span_context ().span_id , "016x" )
191+ trace_id = format (span .get_span_context ().trace_id , "032x" )
192+ self .assertEqual (
188193 mock_cursor .execute .call_args [0 ][0 ],
189- 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}'\ */;" ,
194+ f "Select 1 /*db_driver='MySQLdb%%3Afoobar',dbapi_threadsafety='123',mysql_client_version='foobaz',traceparent='00- { trace_id } - { span_id } -01' */;" ,
190195 )
191196
192197 def test_instrument_connection_with_dbapi_sqlcomment_not_enabled_default (
193198 self ,
194199 ):
195- mock_cursor = mock .MagicMock ()
200+ mock_connect_module = mock .MagicMock (
201+ __name__ = "MySQLdb" ,
202+ threadsafety = "123" ,
203+ apilevel = "123" ,
204+ paramstyle = "test" ,
205+ )
206+ mock_connect_module ._mysql .get_client_info .return_value = "foobaz"
207+ mock_cursor = mock_connect_module .connect ().cursor ()
196208 mock_cursor .execute = mock .MagicMock ()
197209 mock_connection = mock .MagicMock ()
198210 mock_connection .cursor .return_value = mock_cursor
199211
200- mock_connect_module = mock .MagicMock ()
201- mock_connect_module .__name__ = "MySQLdb"
202- mock_connect_module .threadsafety = "123"
203- mock_connect_module .apilevel = "123"
204- mock_connect_module .paramstyle = "test"
205- mock_connect_module ._mysql .get_client_info = mock .Mock (
206- return_value = "foobaz"
207- )
208-
209- mock_connect_module .connect = mock .Mock (return_value = mock_connection )
210-
211212 with mock .patch (
212213 "opentelemetry.instrumentation.mysqlclient.MySQLdb" ,
213214 mock_connect_module ,
@@ -219,15 +220,15 @@ def test_instrument_connection_with_dbapi_sqlcomment_not_enabled_default(
219220 mock_connection ,
220221 )
221222 cnx_proxy .cursor ().execute ("Select 1;" )
222- self .assertRegex (
223+ self .assertEqual (
223224 mock_cursor .execute .call_args [0 ][0 ],
224- r "Select 1;" ,
225+ "Select 1;" ,
225226 )
226227
227228 @mock .patch ("opentelemetry.instrumentation.dbapi.wrap_connect" )
228229 @mock .patch ("MySQLdb.connect" )
229230 # pylint: disable=unused-argument
230- def test__instrument_enable_commenter_dbapi_kwargs (
231+ def test_instrument_enable_commenter_dbapi_kwargs (
231232 self ,
232233 mock_connect ,
233234 mock_wrap_connect ,
@@ -240,25 +241,21 @@ def test__instrument_enable_commenter_dbapi_kwargs(
240241 self .assertEqual (kwargs ["enable_commenter" ], True )
241242 self .assertEqual (kwargs ["commenter_options" ], {"foo" : True })
242243
243- def test__instrument_with_dbapi_sqlcomment_enabled (
244+ def test_instrument_with_dbapi_sqlcomment_enabled (
244245 self ,
245246 ):
246- mock_cursor = mock .MagicMock ()
247+ mock_connect_module = mock .MagicMock (
248+ __name__ = "MySQLdb" ,
249+ threadsafety = "123" ,
250+ apilevel = "123" ,
251+ paramstyle = "test" ,
252+ )
253+ mock_connect_module ._mysql .get_client_info .return_value = "foobaz"
254+ mock_cursor = mock_connect_module .connect ().cursor ()
247255 mock_cursor .execute = mock .MagicMock ()
248256 mock_connection = mock .MagicMock ()
249257 mock_connection .cursor .return_value = mock_cursor
250258
251- mock_connect_module = mock .Mock ()
252- mock_connect_module .__name__ = "MySQLdb"
253- mock_connect_module .threadsafety = "123"
254- mock_connect_module .apilevel = "123"
255- mock_connect_module .paramstyle = "test"
256- mock_connect_module ._mysql .get_client_info = mock .Mock (
257- return_value = "foobaz"
258- )
259-
260- mock_connect_module .connect = mock .Mock (return_value = mock_connection )
261-
262259 with mock .patch (
263260 "opentelemetry.instrumentation.mysqlclient.MySQLdb" ,
264261 mock_connect_module ,
@@ -272,30 +269,31 @@ def test__instrument_with_dbapi_sqlcomment_enabled(
272269 cnx = mock_connect_module .connect (database = "test" )
273270 cursor = cnx .cursor ()
274271 cursor .execute ("Select 1;" )
275- self .assertRegex (
272+
273+ spans_list = self .memory_exporter .get_finished_spans ()
274+ span = spans_list [0 ]
275+ span_id = format (span .get_span_context ().span_id , "016x" )
276+ trace_id = format (span .get_span_context ().trace_id , "032x" )
277+ self .assertEqual (
276278 mock_cursor .execute .call_args [0 ][0 ],
277- 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}'\ */;" ,
279+ f "Select 1 /*db_driver='MySQLdb%%3Afoobar',dbapi_level='123',dbapi_threadsafety='123',driver_paramstyle='test',mysql_client_version='foobaz',traceparent='00- { trace_id } - { span_id } -01' */;" ,
278280 )
279281
280- def test__instrument_with_dbapi_sqlcomment_enabled_with_options (
282+ def test_instrument_with_dbapi_sqlcomment_enabled_with_options (
281283 self ,
282284 ):
283- mock_cursor = mock .MagicMock ()
285+ mock_connect_module = mock .MagicMock (
286+ __name__ = "MySQLdb" ,
287+ threadsafety = "123" ,
288+ apilevel = "123" ,
289+ paramstyle = "test" ,
290+ )
291+ mock_connect_module ._mysql .get_client_info .return_value = "foobaz"
292+ mock_cursor = mock_connect_module .connect ().cursor ()
284293 mock_cursor .execute = mock .MagicMock ()
285294 mock_connection = mock .MagicMock ()
286295 mock_connection .cursor .return_value = mock_cursor
287296
288- mock_connect_module = mock .Mock ()
289- mock_connect_module .__name__ = "MySQLdb"
290- mock_connect_module .threadsafety = "123"
291- mock_connect_module .apilevel = "123"
292- mock_connect_module .paramstyle = "test"
293- mock_connect_module ._mysql .get_client_info = mock .Mock (
294- return_value = "foobaz"
295- )
296-
297- mock_connect_module .connect = mock .Mock (return_value = mock_connection )
298-
299297 with mock .patch (
300298 "opentelemetry.instrumentation.mysqlclient.MySQLdb" ,
301299 mock_connect_module ,
@@ -314,30 +312,31 @@ def test__instrument_with_dbapi_sqlcomment_enabled_with_options(
314312 cnx = mock_connect_module .connect (database = "test" )
315313 cursor = cnx .cursor ()
316314 cursor .execute ("Select 1;" )
317- self .assertRegex (
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 (
318321 mock_cursor .execute .call_args [0 ][0 ],
319- 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}'\ */;" ,
322+ f "Select 1 /*db_driver='MySQLdb%%3Afoobar',dbapi_threadsafety='123',mysql_client_version='foobaz',traceparent='00- { trace_id } - { span_id } -01' */;" ,
320323 )
321324
322- def test__instrument_with_dbapi_sqlcomment_not_enabled_default (
325+ def test_instrument_with_dbapi_sqlcomment_not_enabled_default (
323326 self ,
324327 ):
325- mock_cursor = mock .MagicMock ()
328+ mock_connect_module = mock .MagicMock (
329+ __name__ = "MySQLdb" ,
330+ threadsafety = "123" ,
331+ apilevel = "123" ,
332+ paramstyle = "test" ,
333+ )
334+ mock_connect_module ._mysql .get_client_info .return_value = "foobaz"
335+ mock_cursor = mock_connect_module .connect ().cursor ()
326336 mock_cursor .execute = mock .MagicMock ()
327337 mock_connection = mock .MagicMock ()
328338 mock_connection .cursor .return_value = mock_cursor
329339
330- mock_connect_module = mock .Mock ()
331- mock_connect_module .__name__ = "MySQLdb"
332- mock_connect_module .threadsafety = "123"
333- mock_connect_module .apilevel = "123"
334- mock_connect_module .paramstyle = "test"
335- mock_connect_module ._mysql .get_client_info = mock .Mock (
336- return_value = "foobaz"
337- )
338-
339- mock_connect_module .connect = mock .Mock (return_value = mock_connection )
340-
341340 with mock .patch (
342341 "opentelemetry.instrumentation.mysqlclient.MySQLdb" ,
343342 mock_connect_module ,
@@ -349,9 +348,9 @@ def test__instrument_with_dbapi_sqlcomment_not_enabled_default(
349348 cnx = mock_connect_module .connect (database = "test" )
350349 cursor = cnx .cursor ()
351350 cursor .execute ("Select 1;" )
352- self .assertRegex (
351+ self .assertEqual (
353352 mock_cursor .execute .call_args [0 ][0 ],
354- r "Select 1;" ,
353+ "Select 1;" ,
355354 )
356355
357356 @mock .patch ("MySQLdb.connect" )
0 commit comments