11from __future__ import annotations
22
33import contextlib
4- import traceback
54from dataclasses import dataclass
65from typing import TYPE_CHECKING
76
8- import pymysql
7+ import mysql .connector
8+ from mysql .connector .abstracts import MySQLConnectionAbstract
99import pytest
1010
1111from 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" )
154155def 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" )
168169def 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 ,
0 commit comments