Skip to content

Commit d3add81

Browse files
Change to patch
1 parent 5d89ace commit d3add81

File tree

1 file changed

+5
-18
lines changed

1 file changed

+5
-18
lines changed

tests/opentelemetry-docker-tests/tests/mysql/test_mysql_sqlcommenter.py

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# limitations under the License.
1414

1515
import os
16+
from unittest.mock import patch
1617

1718
import mysql.connector
1819

@@ -91,31 +92,17 @@ def test_commenter_enabled_unknown_mysql_client_version(self):
9192
)
9293
cursor = cnx.cursor()
9394

94-
# Temporarily remove _cmysql
95-
original_cmysql = None
96-
try:
97-
if hasattr(cursor._cnx, "_cmysql"):
98-
original_cmysql = cursor._cnx._cmysql
99-
delattr(cursor._cnx, "_cmysql")
100-
except AttributeError:
101-
pass
102-
103-
cursor.execute("SELECT 1;")
104-
cursor.fetchall()
95+
# Mock get_client_info to raise AttributeError
96+
with patch.object(cursor._cnx._cmysql, 'get_client_info', side_effect=AttributeError("Mocked error")):
97+
cursor.execute("SELECT 1;")
98+
cursor.fetchall()
10599

106100
self.assertRegex(
107101
cursor.statement,
108102
r"SELECT 1 /\*db_driver='mysql\.connector[^']*',dbapi_level='\d\.\d',dbapi_threadsafety=\d,driver_paramstyle='[^']*',mysql_client_version='unknown',traceparent='[^']*'\*/;",
109103
)
110104
self.assertIn("mysql_client_version='unknown'", cursor.statement)
111105

112-
# Restore _cmysql
113-
if original_cmysql is not None:
114-
try:
115-
cursor._cnx._cmysql = original_cmysql
116-
except AttributeError:
117-
pass
118-
119106
cursor.close()
120107
cnx.close()
121108
MySQLInstrumentor().uninstrument()

0 commit comments

Comments
 (0)