11import asyncio
22import gc
33import os
4- import random
5- import time
64import uuid
75from concurrent .futures import ThreadPoolExecutor
8- from contextlib import asynccontextmanager
96
10- import pyodbc
117import pytest
128import pytest_asyncio
139import uvloop
14- from aiodocker import Docker
1510
1611import aioodbc
1712
@@ -43,163 +38,6 @@ def loop(event_loop):
4338 return event_loop
4439
4540
46- @pytest .fixture (scope = "session" )
47- async def docker ():
48- client = Docker ()
49-
50- try :
51- yield client
52- finally :
53- await client .close ()
54-
55-
56- @pytest .fixture (scope = "session" )
57- def host ():
58- # Alternative: host.docker.internal, however not working on travis
59- return os .environ .get ("DOCKER_MACHINE_IP" , "127.0.0.1" )
60-
61-
62- @pytest_asyncio .fixture
63- async def pg_params (pg_server ):
64- server_info = pg_server ["pg_params" ]
65- return dict (** server_info )
66-
67-
68- @asynccontextmanager
69- async def _pg_server_helper (host , docker , session_id ):
70- pg_tag = "9.5"
71-
72- await docker .pull (f"postgres:{ pg_tag } " )
73- container = await docker .containers .create_or_replace (
74- name = f"aioodbc-test-server-{ pg_tag } -{ session_id } " ,
75- config = {
76- "Image" : f"postgres:{ pg_tag } " ,
77- "AttachStdout" : False ,
78- "AttachStderr" : False ,
79- "HostConfig" : {
80- "PublishAllPorts" : True ,
81- },
82- },
83- )
84- await container .start ()
85- container_port = await container .port (5432 )
86- port = container_port [0 ]["HostPort" ]
87-
88- pg_params = {
89- "database" : "postgres" ,
90- "user" : "postgres" ,
91- "password" : "mysecretpassword" ,
92- "host" : host ,
93- "port" : port ,
94- }
95-
96- start = time .time ()
97- dsn = create_pg_dsn (pg_params )
98- last_error = None
99- container_info = {
100- "port" : port ,
101- "pg_params" : pg_params ,
102- "container" : container ,
103- "dsn" : dsn ,
104- }
105- try :
106- while (time .time () - start ) < 40 :
107- try :
108- conn = pyodbc .connect (dsn )
109- cur = conn .execute ("SELECT 1;" )
110- cur .close ()
111- conn .close ()
112- break
113- except pyodbc .Error as e :
114- last_error = e
115- await asyncio .sleep (random .uniform (0.1 , 1 ))
116- else :
117- pytest .fail (f"Cannot start postgres server: { last_error } " )
118-
119- yield container_info
120- finally :
121- container = container_info ["container" ]
122- if container :
123- await container .kill ()
124- await container .delete (v = True , force = True )
125-
126-
127- @pytest .fixture (scope = "session" )
128- async def pg_server (host , docker , session_id ):
129- async with _pg_server_helper (host , docker , session_id ) as helper :
130- yield helper
131-
132-
133- @pytest .fixture
134- async def pg_server_local (host , docker ):
135- async with _pg_server_helper (host , docker , None ) as helper :
136- yield helper
137-
138-
139- @pytest .fixture
140- async def mysql_params (mysql_server ):
141- server_info = (mysql_server )["mysql_params" ]
142- return dict (** server_info )
143-
144-
145- @pytest .fixture (scope = "session" )
146- async def mysql_server (host , docker , session_id ):
147- mysql_tag = "5.7"
148- await docker .pull (f"mysql:{ mysql_tag } " )
149- container = await docker .containers .create_or_replace (
150- name = f"aioodbc-test-server-{ mysql_tag } -{ session_id } " ,
151- config = {
152- "Image" : f"mysql:{ mysql_tag } " ,
153- "AttachStdout" : False ,
154- "AttachStderr" : False ,
155- "Env" : [
156- "MYSQL_USER=aioodbc" ,
157- "MYSQL_PASSWORD=mysecretpassword" ,
158- "MYSQL_DATABASE=aioodbc" ,
159- "MYSQL_ROOT_PASSWORD=mysecretpassword" ,
160- ],
161- "HostConfig" : {
162- "PublishAllPorts" : True ,
163- },
164- },
165- )
166- await container .start ()
167- port = (await container .port (3306 ))[0 ]["HostPort" ]
168- mysql_params = {
169- "database" : "aioodbc" ,
170- "user" : "aioodbc" ,
171- "password" : "mysecretpassword" ,
172- "host" : host ,
173- "port" : port ,
174- }
175- dsn = create_mysql_dsn (mysql_params )
176- start = time .time ()
177- try :
178- last_error = None
179- while (time .time () - start ) < 30 :
180- try :
181- conn = pyodbc .connect (dsn )
182- cur = conn .execute ("SELECT 1;" )
183- cur .close ()
184- conn .close ()
185- break
186- except pyodbc .Error as e :
187- last_error = e
188- await asyncio .sleep (random .uniform (0.1 , 1 ))
189- else :
190- pytest .fail (f"Cannot start mysql server: { last_error } " )
191-
192- container_info = {
193- "port" : port ,
194- "mysql_params" : mysql_params ,
195- }
196-
197- yield container_info
198- finally :
199- await container .kill ()
200- await container .delete (v = True , force = True )
201-
202-
20341@pytest .fixture
20442def executor ():
20543 executor = ThreadPoolExecutor (max_workers = 1 )
@@ -215,7 +53,7 @@ def pytest_configure():
21553
21654
21755@pytest .fixture
218- def db (request ):
56+ def db ():
21957 return "sqlite"
22058
22159
0 commit comments