Skip to content

Commit fe4e8c6

Browse files
authored
Close mysql connections and cursors in tests (#352)
1 parent 48d5182 commit fe4e8c6

File tree

1 file changed

+47
-38
lines changed

1 file changed

+47
-38
lines changed

tests/otel_integrations/test_mysql.py

Lines changed: 47 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from __future__ import annotations
22

3+
import sys
4+
35
import mysql.connector
46
import pytest
57
from dirty_equals import IsInt
@@ -10,6 +12,8 @@
1012
import logfire
1113
from logfire.testing import TestExporter
1214

15+
pytestmark = pytest.mark.skipif(sys.version_info < (3, 9), reason='MySQL testcontainers has problems in 3.8')
16+
1317

1418
@pytest.fixture(scope='module')
1519
def mysql_container():
@@ -26,10 +30,12 @@ def get_mysql_connection(mysql_container: MySqlContainer):
2630

2731
def test_mysql_instrumentation(exporter: TestExporter, mysql_container: MySqlContainer):
2832
logfire.instrument_mysql()
29-
conn = get_mysql_connection(mysql_container)
30-
cursor = conn.cursor()
31-
cursor.execute('DROP TABLE IF EXISTS test')
32-
cursor.execute('CREATE TABLE test (id INT PRIMARY KEY, name VARCHAR(255))')
33+
34+
with get_mysql_connection(mysql_container) as conn:
35+
with conn.cursor() as cursor:
36+
cursor.execute('DROP TABLE IF EXISTS test')
37+
cursor.execute('CREATE TABLE test (id INT PRIMARY KEY, name VARCHAR(255))')
38+
3339
assert exporter.exported_spans_as_dict() == snapshot(
3440
[
3541
{
@@ -72,38 +78,41 @@ def test_mysql_instrumentation(exporter: TestExporter, mysql_container: MySqlCon
7278

7379

7480
def test_instrument_mysql_connection(exporter: TestExporter, mysql_container: MySqlContainer):
75-
conn = get_mysql_connection(mysql_container)
76-
cursor = conn.cursor()
77-
cursor.execute('DROP TABLE IF EXISTS test')
78-
cursor.execute('CREATE TABLE test (id INT PRIMARY KEY, name VARCHAR(255))')
79-
assert exporter.exported_spans_as_dict() == []
80-
81-
conn = logfire.instrument_mysql(conn)
82-
cursor = conn.cursor()
83-
cursor.execute('INSERT INTO test (id, name) VALUES (1, "test")')
84-
assert exporter.exported_spans_as_dict() == snapshot(
85-
[
86-
{
87-
'name': 'INSERT',
88-
'context': {'trace_id': 1, 'span_id': 1, 'is_remote': False},
89-
'parent': None,
90-
'start_time': 1000000000,
91-
'end_time': 2000000000,
92-
'attributes': {
93-
'logfire.span_type': 'span',
94-
'logfire.msg': 'INSERT INTO test (id, name) VALUES (1, "test")',
95-
'db.system': 'mysql',
96-
'db.name': 'test',
97-
'db.statement': 'INSERT INTO test (id, name) VALUES (1, "test")',
98-
'db.user': 'test',
99-
'net.peer.name': 'localhost',
100-
'net.peer.port': IsInt(),
101-
},
102-
}
103-
]
104-
)
81+
with get_mysql_connection(mysql_container) as conn:
82+
with conn.cursor() as cursor:
83+
cursor.execute('DROP TABLE IF EXISTS test')
84+
cursor.execute('CREATE TABLE test (id INT PRIMARY KEY, name VARCHAR(255))')
85+
86+
assert exporter.exported_spans_as_dict() == []
87+
88+
conn = logfire.instrument_mysql(conn)
89+
with conn.cursor() as cursor:
90+
cursor.execute('INSERT INTO test (id, name) VALUES (1, "test")')
91+
92+
assert exporter.exported_spans_as_dict() == snapshot(
93+
[
94+
{
95+
'name': 'INSERT',
96+
'context': {'trace_id': 1, 'span_id': 1, 'is_remote': False},
97+
'parent': None,
98+
'start_time': 1000000000,
99+
'end_time': 2000000000,
100+
'attributes': {
101+
'logfire.span_type': 'span',
102+
'logfire.msg': 'INSERT INTO test (id, name) VALUES (1, "test")',
103+
'db.system': 'mysql',
104+
'db.name': 'test',
105+
'db.statement': 'INSERT INTO test (id, name) VALUES (1, "test")',
106+
'db.user': 'test',
107+
'net.peer.name': 'localhost',
108+
'net.peer.port': IsInt(),
109+
},
110+
}
111+
]
112+
)
113+
114+
conn = MySQLInstrumentor().uninstrument_connection(conn) # type: ignore
115+
with conn.cursor() as cursor: # type: ignore
116+
cursor.execute('INSERT INTO test (id, name) VALUES (2, "test-2")') # type: ignore
105117

106-
conn = MySQLInstrumentor().uninstrument_connection(conn) # type: ignore
107-
cursor = conn.cursor() # type: ignore
108-
cursor.execute('INSERT INTO test (id, name) VALUES (2, "test-2")') # type: ignore
109-
assert len(exporter.exported_spans_as_dict()) == 1
118+
assert len(exporter.exported_spans_as_dict()) == 1

0 commit comments

Comments
 (0)