@@ -188,7 +188,9 @@ async def test_insert_select(conn, db_parameters, caplog):
188
188
assert "Number of results in first chunk: 3" in caplog .text
189
189
190
190
191
- async def test_insert_and_select_by_separate_connection (conn , db_parameters , caplog ):
191
+ async def test_insert_and_select_by_separate_connection (
192
+ conn , conn_cnx , db_parameters , caplog
193
+ ):
192
194
"""Inserts a record and select it by a separate connection."""
193
195
caplog .set_level (logging .DEBUG )
194
196
async with conn () as cnx :
@@ -202,20 +204,7 @@ async def test_insert_and_select_by_separate_connection(conn, db_parameters, cap
202
204
cnt += int (rec [0 ])
203
205
assert cnt == 1 , "wrong number of records were inserted"
204
206
assert result .rowcount == 1 , "wrong number of records were inserted"
205
-
206
- cnx2 = snowflake .connector .aio .SnowflakeConnection (
207
- user = db_parameters ["user" ],
208
- password = db_parameters ["password" ],
209
- host = db_parameters ["host" ],
210
- port = db_parameters ["port" ],
211
- account = db_parameters ["account" ],
212
- database = db_parameters ["database" ],
213
- schema = db_parameters ["schema" ],
214
- protocol = db_parameters ["protocol" ],
215
- timezone = "UTC" ,
216
- )
217
- await cnx2 .connect ()
218
- try :
207
+ async with conn_cnx (timezone = "UTC" ) as cnx2 :
219
208
c = cnx2 .cursor ()
220
209
await c .execute ("select aa from {name}" .format (name = db_parameters ["name" ]))
221
210
results = []
@@ -225,8 +214,6 @@ async def test_insert_and_select_by_separate_connection(conn, db_parameters, cap
225
214
assert results [0 ] == 1234 , "the first result was wrong"
226
215
assert result .rowcount == 1 , "wrong number of records were selected"
227
216
assert "Number of results in first chunk: 1" in caplog .text
228
- finally :
229
- await cnx2 .close ()
230
217
231
218
232
219
def _total_milliseconds_from_timedelta (td ):
@@ -239,7 +226,7 @@ def _total_seconds_from_timedelta(td):
239
226
return _total_milliseconds_from_timedelta (td ) // 10 ** 3
240
227
241
228
242
- async def test_insert_timestamp_select (conn , db_parameters ):
229
+ async def test_insert_timestamp_select (conn , conn_cnx , db_parameters ):
243
230
"""Inserts and gets timestamp, timestamp with tz, date, and time.
244
231
245
232
Notes:
@@ -282,19 +269,7 @@ async def test_insert_timestamp_select(conn, db_parameters):
282
269
finally :
283
270
await c .close ()
284
271
285
- cnx2 = snowflake .connector .aio .SnowflakeConnection (
286
- user = db_parameters ["user" ],
287
- password = db_parameters ["password" ],
288
- host = db_parameters ["host" ],
289
- port = db_parameters ["port" ],
290
- account = db_parameters ["account" ],
291
- database = db_parameters ["database" ],
292
- schema = db_parameters ["schema" ],
293
- protocol = db_parameters ["protocol" ],
294
- timezone = "UTC" ,
295
- )
296
- await cnx2 .connect ()
297
- try :
272
+ async with conn_cnx (timezone = "UTC" ) as cnx2 :
298
273
c = cnx2 .cursor ()
299
274
await c .execute (
300
275
"select aa, tsltz, tstz, tsntz, dt, tm from {name}" .format (
@@ -374,8 +349,6 @@ async def test_insert_timestamp_select(conn, db_parameters):
374
349
assert (
375
350
constants .FIELD_ID_TO_NAME [type_code (desc [5 ])] == "TIME"
376
351
), "invalid column name"
377
- finally :
378
- await cnx2 .close ()
379
352
380
353
381
354
async def test_insert_timestamp_ltz (conn , db_parameters ):
@@ -475,7 +448,7 @@ async def test_struct_time(conn, db_parameters):
475
448
time .tzset ()
476
449
477
450
478
- async def test_insert_binary_select (conn , db_parameters ):
451
+ async def test_insert_binary_select (conn , conn_cnx , db_parameters ):
479
452
"""Inserts and get a binary value."""
480
453
value = b"\x00 \xFF \xA1 \xB2 \xC3 "
481
454
@@ -490,18 +463,7 @@ async def test_insert_binary_select(conn, db_parameters):
490
463
finally :
491
464
await c .close ()
492
465
493
- cnx2 = snowflake .connector .aio .SnowflakeConnection (
494
- user = db_parameters ["user" ],
495
- password = db_parameters ["password" ],
496
- host = db_parameters ["host" ],
497
- port = db_parameters ["port" ],
498
- account = db_parameters ["account" ],
499
- database = db_parameters ["database" ],
500
- schema = db_parameters ["schema" ],
501
- protocol = db_parameters ["protocol" ],
502
- )
503
- await cnx2 .connect ()
504
- try :
466
+ async with conn_cnx () as cnx2 :
505
467
c = cnx2 .cursor ()
506
468
await c .execute ("select b from {name}" .format (name = db_parameters ["name" ]))
507
469
@@ -524,11 +486,9 @@ async def test_insert_binary_select(conn, db_parameters):
524
486
assert (
525
487
constants .FIELD_ID_TO_NAME [type_code (desc [0 ])] == "BINARY"
526
488
), "invalid column name"
527
- finally :
528
- await cnx2 .close ()
529
489
530
490
531
- async def test_insert_binary_select_with_bytearray (conn , db_parameters ):
491
+ async def test_insert_binary_select_with_bytearray (conn , conn_cnx , db_parameters ):
532
492
"""Inserts and get a binary value using the bytearray type."""
533
493
value = bytearray (b"\x00 \xFF \xA1 \xB2 \xC3 " )
534
494
@@ -543,18 +503,7 @@ async def test_insert_binary_select_with_bytearray(conn, db_parameters):
543
503
finally :
544
504
await c .close ()
545
505
546
- cnx2 = snowflake .connector .aio .SnowflakeConnection (
547
- user = db_parameters ["user" ],
548
- password = db_parameters ["password" ],
549
- host = db_parameters ["host" ],
550
- port = db_parameters ["port" ],
551
- account = db_parameters ["account" ],
552
- database = db_parameters ["database" ],
553
- schema = db_parameters ["schema" ],
554
- protocol = db_parameters ["protocol" ],
555
- )
556
- await cnx2 .connect ()
557
- try :
506
+ async with conn_cnx () as cnx2 :
558
507
c = cnx2 .cursor ()
559
508
await c .execute ("select b from {name}" .format (name = db_parameters ["name" ]))
560
509
@@ -577,8 +526,6 @@ async def test_insert_binary_select_with_bytearray(conn, db_parameters):
577
526
assert (
578
527
constants .FIELD_ID_TO_NAME [type_code (desc [0 ])] == "BINARY"
579
528
), "invalid column name"
580
- finally :
581
- await cnx2 .close ()
582
529
583
530
584
531
async def test_variant (conn , db_parameters ):
0 commit comments