Skip to content

Commit 920b0dd

Browse files
committed
use mysql-connector-python
1 parent 698d036 commit 920b0dd

File tree

4 files changed

+52
-35
lines changed

4 files changed

+52
-35
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ elasticsearch7 = ["elasticsearch7"]
6161
elasticsearch8 = ["elasticsearch8"]
6262
keydb = ["redis"]
6363
mssql = ["pymssql<=2.3.1"]
64-
mysql = ["pymysql[rsa]"]
64+
mysql = ["mysql-connector-python"]
6565
oracle = ["oracledb"]
6666
postgres = ["psycopg>=3"]
6767
redis = ["redis"]

src/pytest_databases/docker/mysql.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
from __future__ import annotations
22

33
import contextlib
4-
import traceback
54
from dataclasses import dataclass
65
from typing import TYPE_CHECKING
76

8-
import pymysql
7+
import mysql.connector
8+
from mysql.connector.abstracts import MySQLConnectionAbstract
99
import pytest
1010

1111
from pytest_databases._service import DockerService, ServiceContainer
@@ -43,16 +43,17 @@ def _provide_mysql_service(
4343

4444
def check(_service: ServiceContainer) -> bool:
4545
try:
46-
conn = pymysql.connect(
46+
conn = mysql.connector.connect(
4747
host=_service.host,
4848
port=_service.port,
4949
user=user,
5050
database=database,
5151
password=password,
5252
)
53-
except Exception: # noqa: BLE001
54-
traceback.print_exc()
55-
return False
53+
except mysql.connector.errors.OperationalError as exc:
54+
if "Lost connection" in exc.msg:
55+
return False
56+
raise
5657

5758
try:
5859
with conn.cursor() as cursor:
@@ -153,8 +154,8 @@ def mysql_8_service(
153154
@pytest.fixture(autouse=False, scope="session")
154155
def mysql_56_connection(
155156
mysql_56_service: MySQLService,
156-
) -> Generator[pymysql.Connection, None, None]:
157-
with pymysql.connect(
157+
) -> Generator[MySQLConnectionAbstract, None, None]:
158+
with mysql.connector.connect(
158159
host=mysql_56_service.host,
159160
port=mysql_56_service.port,
160161
user=mysql_56_service.user,
@@ -167,8 +168,8 @@ def mysql_56_connection(
167168
@pytest.fixture(autouse=False, scope="session")
168169
def mysql_57_connection(
169170
mysql_57_service: MySQLService,
170-
) -> Generator[pymysql.Connection, None, None]:
171-
with pymysql.connect(
171+
) -> Generator[MySQLConnectionAbstract, None, None]:
172+
with mysql.connector.connect(
172173
host=mysql_57_service.host,
173174
port=mysql_57_service.port,
174175
user=mysql_57_service.user,
@@ -179,13 +180,13 @@ def mysql_57_connection(
179180

180181

181182
@pytest.fixture(autouse=False, scope="session")
182-
def mysql_connection(mysql_8_connection: pymysql.Connection) -> pymysql.Connection:
183+
def mysql_connection(mysql_8_connection) -> MySQLConnectionAbstract:
183184
return mysql_8_connection
184185

185186

186187
@pytest.fixture(autouse=False, scope="session")
187-
def mysql_8_connection(mysql_8_service: MySQLService) -> Generator[pymysql.Connection, None, None]:
188-
with pymysql.connect(
188+
def mysql_8_connection(mysql_8_service) -> Generator[MySQLConnectionAbstract, None, None]:
189+
with mysql.connector.connect(
189190
host=mysql_8_service.host,
190191
port=mysql_8_service.port,
191192
user=mysql_8_service.user,

tests/test_mysql.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@
1313
)
1414
def test_service_fixture(pytester: pytest.Pytester, service_fixture: str) -> None:
1515
pytester.makepyfile(f"""
16-
import pymysql
16+
import mysql.connector
1717
pytest_plugins = ["pytest_databases.docker.mysql"]
1818
1919
def test({service_fixture}):
20-
with pymysql.connect(
20+
with mysql.connector.connect(
2121
host={service_fixture}.host,
2222
port={service_fixture}.port,
2323
user={service_fixture}.user,
@@ -42,7 +42,6 @@ def test({service_fixture}):
4242
)
4343
def test_connection_fixture(pytester: pytest.Pytester, connection_fixture: str) -> None:
4444
pytester.makepyfile(f"""
45-
import pymysql
4645
pytest_plugins = ["pytest_databases.docker.mysql"]
4746
4847
def test({connection_fixture}):
@@ -59,7 +58,6 @@ def test({connection_fixture}):
5958

6059
def test_xdist_isolate_database(pytester: pytest.Pytester) -> None:
6160
pytester.makepyfile("""
62-
import pymysql
6361
pytest_plugins = ["pytest_databases.docker.mysql"]
6462
6563
def test_1(mysql_56_connection):
@@ -77,7 +75,6 @@ def test_2(mysql_56_connection):
7775

7876
def test_xdist_isolate_server(pytester: pytest.Pytester) -> None:
7977
pytester.makepyfile("""
80-
import pymysql
8178
import pytest
8279
pytest_plugins = ["pytest_databases.docker.mysql"]
8380

uv.lock

Lines changed: 35 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)