Skip to content

Commit 755326a

Browse files
committed
feat(mysql): move to pymysql
1 parent 7b21f8e commit 755326a

File tree

6 files changed

+911
-775
lines changed

6 files changed

+911
-775
lines changed

.pre-commit-config.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ exclude: "^docs/conf.py"
22

33
repos:
44
- repo: https://github.com/pre-commit/pre-commit-hooks
5-
rev: v5.0.0
5+
rev: v6.0.0
66
hooks:
77
- id: trailing-whitespace
88
- id: check-added-large-files
@@ -19,7 +19,7 @@ repos:
1919

2020
# Ruff replaces black, flake8, autoflake and isort
2121
- repo: https://github.com/charliermarsh/ruff-pre-commit
22-
rev: "v0.11.11" # make sure this is always consistent with hatch configs
22+
rev: "v0.12.9" # make sure this is always consistent with hatch configs
2323
hooks:
2424
- id: ruff
2525
args: [--config, ./pyproject.toml]

docs/supported-databases/mysql.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ Usage Example
1616
.. code-block:: python
1717
1818
import pytest
19-
import mysql.connector
19+
import pymysql
2020
from pytest_databases.docker.mysql import MySQLService
2121
2222
pytest_plugins = ["pytest_databases.docker.mysql"]
2323
2424
def test(mysql_service: MySQLService) -> None:
25-
with mysql.connector.connect(
25+
with pymysql.connect(
2626
host=mysql_service.host,
2727
port=mysql_service.port,
2828
user=mysql_service.user,
@@ -33,7 +33,7 @@ Usage Example
3333
resp = cursor.fetchone()
3434
assert resp is not None and resp[0] == 1
3535
36-
def test(mysql_connection: mysql.connector.MySQLConnection) -> None:
36+
def test(mysql_connection: pymysql.Connection) -> None:
3737
with mysql_connection.cursor() as cursor:
3838
cursor.execute("CREATE TABLE if not exists simple_table as SELECT 1 as the_value")
3939
cursor.execute("select * from simple_table")

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ keydb = ["redis"]
6565
mariadb = ["mariadb"]
6666
minio = ["minio"]
6767
mssql = ["pymssql"]
68-
mysql = ["mysql-connector-python"]
68+
mysql = ["pymysql"]
6969
oracle = ["oracledb"]
7070
postgres = ["psycopg>=3"]
7171
redis = ["redis"]

src/pytest_databases/docker/mysql.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from dataclasses import dataclass
55
from typing import TYPE_CHECKING
66

7-
import mysql.connector
7+
import pymysql
88
import pytest
99

1010
from pytest_databases._service import DockerService, ServiceContainer
@@ -13,7 +13,7 @@
1313
if TYPE_CHECKING:
1414
from collections.abc import Generator
1515

16-
from mysql.connector.abstracts import MySQLConnectionAbstract
16+
from pymysql import Connection
1717

1818
from pytest_databases.types import XdistIsolationLevel
1919

@@ -50,23 +50,23 @@ def _provide_mysql_service(
5050

5151
def check(_service: ServiceContainer) -> bool:
5252
try:
53-
conn = mysql.connector.connect(
53+
conn = pymysql.connect(
5454
host=_service.host,
5555
port=_service.port,
5656
user=user,
5757
database=database,
5858
password=password,
5959
)
60-
except mysql.connector.errors.OperationalError as exc:
61-
if "Lost connection" in exc.msg: # type: ignore
60+
except pymysql.OperationalError as exc:
61+
if "Lost connection" in str(exc):
6262
return False
6363
raise
6464

6565
try:
6666
with conn.cursor() as cursor:
6767
cursor.execute("select 1 as is_available")
6868
resp = cursor.fetchone()
69-
return resp is not None and resp[0] == 1 # type: ignore
69+
return resp is not None and resp[0] == 1
7070
finally:
7171
with contextlib.suppress(Exception):
7272
conn.close()
@@ -166,41 +166,41 @@ def mysql_8_service(
166166

167167

168168
@pytest.fixture(scope="session")
169-
def mysql_56_connection(mysql_56_service: MySQLService) -> Generator[MySQLConnectionAbstract, None, None]:
170-
with mysql.connector.connect(
169+
def mysql_56_connection(mysql_56_service: MySQLService) -> Generator[Connection, None, None]:
170+
with pymysql.connect(
171171
host=mysql_56_service.host,
172172
port=mysql_56_service.port,
173173
user=mysql_56_service.user,
174174
database=mysql_56_service.db,
175175
password=mysql_56_service.password,
176176
) as conn:
177-
yield conn # type: ignore
177+
yield conn
178178

179179

180180
@pytest.fixture(scope="session")
181-
def mysql_57_connection(mysql_57_service: MySQLService) -> Generator[MySQLConnectionAbstract, None, None]:
182-
with mysql.connector.connect(
181+
def mysql_57_connection(mysql_57_service: MySQLService) -> Generator[Connection, None, None]:
182+
with pymysql.connect(
183183
host=mysql_57_service.host,
184184
port=mysql_57_service.port,
185185
user=mysql_57_service.user,
186186
database=mysql_57_service.db,
187187
password=mysql_57_service.password,
188188
) as conn:
189-
yield conn # type: ignore
189+
yield conn
190190

191191

192192
@pytest.fixture(scope="session")
193-
def mysql_connection(mysql_8_connection: MySQLConnectionAbstract) -> MySQLConnectionAbstract:
193+
def mysql_connection(mysql_8_connection: Connection) -> Connection:
194194
return mysql_8_connection
195195

196196

197197
@pytest.fixture(scope="session")
198-
def mysql_8_connection(mysql_8_service: MySQLService) -> Generator[MySQLConnectionAbstract, None, None]:
199-
with mysql.connector.connect(
198+
def mysql_8_connection(mysql_8_service: MySQLService) -> Generator[Connection, None, None]:
199+
with pymysql.connect(
200200
host=mysql_8_service.host,
201201
port=mysql_8_service.port,
202202
user=mysql_8_service.user,
203203
database=mysql_8_service.db,
204204
password=mysql_8_service.password,
205205
) as conn:
206-
yield conn # type: ignore
206+
yield conn

tests/test_mysql.py

Lines changed: 2 additions & 2 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 mysql.connector
16+
import pymysql
1717
pytest_plugins = ["pytest_databases.docker.mysql"]
1818
1919
def test({service_fixture}):
20-
with mysql.connector.connect(
20+
with pymysql.connect(
2121
host={service_fixture}.host,
2222
port={service_fixture}.port,
2323
user={service_fixture}.user,

0 commit comments

Comments
 (0)