@@ -134,69 +134,76 @@ async def create_default_data_for_tests(
134134 table_name : str ,
135135 number_database_records : int ,
136136) -> AsyncGenerator [None , None ]:
137- connection = await psql_pool .connection ()
138- await connection .execute (
139- f"CREATE TABLE { table_name } (id SERIAL, name VARCHAR(255))" ,
140- )
141-
142- for table_id in range (1 , number_database_records + 1 ):
143- new_name = random_string ()
137+ async with psql_pool .acquire () as connection :
144138 await connection .execute (
145- querystring = f"INSERT INTO { table_name } VALUES ($1, $2)" ,
146- parameters = [table_id , new_name ],
139+ f"CREATE TABLE { table_name } (id SERIAL, name VARCHAR(255))" ,
147140 )
141+
142+ for table_id in range (1 , number_database_records + 1 ):
143+ new_name = random_string ()
144+ await connection .execute (
145+ querystring = f"INSERT INTO { table_name } VALUES ($1, $2)" ,
146+ parameters = [table_id , new_name ],
147+ )
148+
148149 yield
149- await connection .execute (
150- f"DROP TABLE { table_name } " ,
151- )
150+
151+ async with psql_pool .acquire () as connection :
152+ await connection .execute (
153+ f"DROP TABLE { table_name } " ,
154+ )
152155
153156
154157@pytest .fixture
155158async def create_table_for_listener_tests (
156159 psql_pool : ConnectionPool ,
157160 listener_table_name : str ,
158161) -> AsyncGenerator [None , None ]:
159- connection = await psql_pool .connection ()
160- await connection .execute (
161- f"CREATE TABLE { listener_table_name } "
162- f"(id SERIAL, payload VARCHAR(255),"
163- f"channel VARCHAR(255), process_id INT)" ,
164- )
162+ async with psql_pool .acquire () as connection :
163+ await connection .execute (
164+ f"CREATE TABLE { listener_table_name } "
165+ f"(id SERIAL, payload VARCHAR(255),"
166+ f"channel VARCHAR(255), process_id INT)" ,
167+ )
165168
166169 yield
167- await connection .execute (
168- f"DROP TABLE { listener_table_name } " ,
169- )
170+
171+ async with psql_pool .acquire () as connection :
172+ await connection .execute (
173+ f"DROP TABLE { listener_table_name } " ,
174+ )
170175
171176
172177@pytest .fixture
173178async def create_table_for_map_parameters_test (
174179 psql_pool : ConnectionPool ,
175180 map_parameters_table_name : str ,
176181) -> AsyncGenerator [None , None ]:
177- connection = await psql_pool .connection ()
178- await connection .execute (
179- f"CREATE TABLE { map_parameters_table_name } "
180- "(id SERIAL, name VARCHAR(255),surname VARCHAR(255), age SMALLINT)" ,
181- )
182+ async with psql_pool .acquire () as connection :
183+ await connection .execute (
184+ f"CREATE TABLE { map_parameters_table_name } "
185+ "(id SERIAL, name VARCHAR(255),surname VARCHAR(255), age SMALLINT)" ,
186+ )
182187
183188 yield
184- await connection .execute (
185- f"DROP TABLE { map_parameters_table_name } " ,
186- )
189+
190+ async with psql_pool .acquire () as connection :
191+ await connection .execute (
192+ f"DROP TABLE { map_parameters_table_name } " ,
193+ )
187194
188195
189196@pytest .fixture
190197async def test_cursor (
191198 psql_pool : ConnectionPool ,
192199 table_name : str ,
193200) -> AsyncGenerator [Cursor , None ]:
194- connection = await psql_pool .connection ()
195- transaction = connection .transaction ()
196- await transaction .begin ()
197- cursor = transaction .cursor (
198- querystring = f"SELECT * FROM { table_name } " ,
199- )
200- await cursor .start ()
201- yield cursor
202- await transaction .commit ()
201+ async with psql_pool .acquire () as connection :
202+ transaction = connection .transaction ()
203+ await transaction .begin ()
204+ cursor = transaction .cursor (
205+ querystring = f"SELECT * FROM { table_name } " ,
206+ )
207+ await cursor .start ()
208+ yield cursor
209+ await transaction .commit ()
0 commit comments