11
11
12
12
@pytest .mark .inside_docker_check
13
13
def test_docker_run_mysql ():
14
- config = MySqlContainer ("mysql:8.3.0" )
14
+ config = MySqlContainer ("mysql:8.3.0" , dialect = "pymysql" )
15
15
with config as mysql :
16
- engine = sqlalchemy .create_engine (mysql .get_connection_url ())
16
+ connection_url = mysql .get_connection_url ()
17
+
18
+ assert mysql .dialect == "pymysql"
19
+ assert connection_url .startswith ("mysql+pymysql://" )
20
+
21
+ engine = sqlalchemy .create_engine (connection_url )
17
22
with engine .begin () as connection :
18
23
result = connection .execute (sqlalchemy .text ("select version()" ))
19
24
for row in result :
@@ -22,7 +27,7 @@ def test_docker_run_mysql():
22
27
23
28
@pytest .mark .skipif (is_arm (), reason = "mysql container not available for ARM" )
24
29
def test_docker_run_legacy_mysql ():
25
- config = MySqlContainer ("mysql:5.7.44" )
30
+ config = MySqlContainer ("mysql:5.7.44" , dialect = "pymysql" )
26
31
with config as mysql :
27
32
engine = sqlalchemy .create_engine (mysql .get_connection_url ())
28
33
with engine .begin () as connection :
@@ -35,7 +40,7 @@ def test_docker_run_legacy_mysql():
35
40
def test_docker_run_mysql_8_seed ():
36
41
# Avoid pytest CWD path issues
37
42
SEEDS_PATH = (Path (__file__ ).parent / "seeds" ).absolute ()
38
- config = MySqlContainer ("mysql:8" , seed = SEEDS_PATH )
43
+ config = MySqlContainer ("mysql:8" , dialect = "pymysql" , seed = str ( SEEDS_PATH ) )
39
44
with config as mysql :
40
45
engine = sqlalchemy .create_engine (mysql .get_connection_url ())
41
46
with engine .begin () as connection :
@@ -45,7 +50,7 @@ def test_docker_run_mysql_8_seed():
45
50
46
51
@pytest .mark .parametrize ("version" , ["11.3.2" , "10.11.7" ])
47
52
def test_docker_run_mariadb (version : str ):
48
- with MySqlContainer (f"mariadb:{ version } " ) as mariadb :
53
+ with MySqlContainer (f"mariadb:{ version } " , dialect = "pymysql" ) as mariadb :
49
54
engine = sqlalchemy .create_engine (mariadb .get_connection_url ())
50
55
with engine .begin () as connection :
51
56
result = connection .execute (sqlalchemy .text ("select version()" ))
@@ -55,14 +60,29 @@ def test_docker_run_mariadb(version: str):
55
60
56
61
def test_docker_env_variables ():
57
62
with (
58
- mock .patch .dict ("os.environ" , MYSQL_USER = "demo" , MYSQL_DATABASE = "custom_db" ),
63
+ mock .patch .dict ("os.environ" , MYSQL_DIALECT = "pymysql" , MYSQL_USER = "demo" , MYSQL_DATABASE = "custom_db" ),
59
64
MySqlContainer ("mariadb:10.6.5" ).with_bind_ports (3306 , 32785 ) as container ,
60
65
):
61
66
url = container .get_connection_url ()
62
67
pattern = r"mysql\+pymysql:\/\/demo:test@[\w,.]+:(3306|32785)\/custom_db"
63
68
assert re .match (pattern , url )
64
69
65
70
71
+ @pytest .mark .parametrize (
72
+ "dialect" ,
73
+ [
74
+ "mysql+pymysql" ,
75
+ "mysql+mariadb" ,
76
+ "mysql+mysqldb" ,
77
+ ],
78
+ )
79
+ def test_mysql_dialect_expecting_error_on_mysql_prefix (dialect : str ):
80
+ match = f"Please remove *.* prefix from dialect parameter"
81
+
82
+ with pytest .raises (ValueError , match = match ):
83
+ _ = MySqlContainer ("mariadb:10.6.5" , dialect = dialect )
84
+
85
+
66
86
# This is a feature in the generic DbContainer class
67
87
# but it can't be tested on its own
68
88
# so is tested in various database modules:
@@ -75,18 +95,18 @@ def test_quoted_password():
75
95
user = "root"
76
96
password = "p@$%25+0&%rd :/!=?"
77
97
quoted_password = "p%40%24%2525+0%26%25rd %3A%2F%21%3D%3F"
78
- driver = "pymysql"
79
- with MySqlContainer ("mariadb:10.6.5" , username = user , password = password ) as container :
98
+ dialect = "pymysql"
99
+ with MySqlContainer ("mariadb:10.6.5" , dialect = dialect , username = user , password = password ) as container :
80
100
host = container .get_container_host_ip ()
81
101
port = container .get_exposed_port (3306 )
82
- expected_url = f"mysql+{ driver } ://{ user } :{ quoted_password } @{ host } :{ port } /test"
102
+ expected_url = f"mysql+{ dialect } ://{ user } :{ quoted_password } @{ host } :{ port } /test"
83
103
url = container .get_connection_url ()
84
104
assert url == expected_url
85
105
86
106
with sqlalchemy .create_engine (expected_url ).begin () as connection :
87
107
connection .execute (sqlalchemy .text ("select version()" ))
88
108
89
- raw_pass_url = f"mysql+{ driver } ://{ user } :{ password } @{ host } :{ port } /test"
109
+ raw_pass_url = f"mysql+{ dialect } ://{ user } :{ password } @{ host } :{ port } /test"
90
110
with pytest .raises (Exception ):
91
111
with sqlalchemy .create_engine (raw_pass_url ).begin () as connection :
92
112
connection .execute (sqlalchemy .text ("select version()" ))
0 commit comments