@@ -514,3 +514,90 @@ async def get_connection_2():
514514 test_complete .set ()
515515 await task_1
516516 await task_2
517+
518+
519+ @pytest .mark .parametrize ("database_url" , DATABASE_URLS )
520+ @async_adapter
521+ async def test_connection_context_with_raw_connection (database_url ):
522+ """
523+ Test connection contexts with respect to the raw connection.
524+ """
525+ async with Database (database_url ) as database :
526+ async with database .connection () as connection_1 :
527+ async with database .connection () as connection_2 :
528+ assert connection_1 is connection_2
529+ assert connection_1 .raw_connection is connection_2 .raw_connection
530+
531+
532+ @pytest .mark .parametrize ("database_url" , DATABASE_URLS )
533+ @async_adapter
534+ async def test_queries_with_expose_backend_connection (database_url ):
535+ """
536+ Replication of `execute()`, `execute_many()`, `fetch_all()``, and
537+ `fetch_one()` using the raw driver interface.
538+ """
539+ async with Database (database_url ) as database :
540+ async with database .connection () as connection :
541+ async with database .transaction (force_rollback = True ):
542+ # Get the raw connection
543+ raw_connection = connection .raw_connection
544+
545+ # Insert query
546+ if str (database_url ).startswith ("mysql" ):
547+ insert_query = "INSERT INTO notes (text, completed) VALUES (%s, %s)"
548+ else :
549+ insert_query = "INSERT INTO notes (text, completed) VALUES ($1, $2)"
550+
551+ # execute()
552+ values = ("example1" , True )
553+
554+ if str (database_url ).startswith ("postgresql" ):
555+ await raw_connection .execute (insert_query , * values )
556+ elif str (database_url ).startswith ("mysql" ):
557+ cursor = await raw_connection .cursor ()
558+ await cursor .execute (insert_query , values )
559+ elif str (database_url ).startswith ("sqlite" ):
560+ await raw_connection .execute (insert_query , values )
561+
562+ # execute_many()
563+ values = [("example2" , False ), ("example3" , True )]
564+
565+ if str (database_url ).startswith ("mysql" ):
566+ cursor = await raw_connection .cursor ()
567+ await cursor .executemany (insert_query , values )
568+ else :
569+ await raw_connection .executemany (insert_query , values )
570+
571+ # Select query
572+ select_query = "SELECT notes.id, notes.text, notes.completed FROM notes"
573+
574+ # fetch_all()
575+ if str (database_url ).startswith ("postgresql" ):
576+ results = await raw_connection .fetch (select_query )
577+ elif str (database_url ).startswith ("mysql" ):
578+ cursor = await raw_connection .cursor ()
579+ await cursor .execute (select_query )
580+ results = await cursor .fetchall ()
581+ elif str (database_url ).startswith ("sqlite" ):
582+ results = await raw_connection .execute_fetchall (select_query )
583+
584+ assert len (results ) == 3
585+ # Raw output for the raw request
586+ assert results [0 ][1 ] == "example1"
587+ assert results [0 ][2 ] == True
588+ assert results [1 ][1 ] == "example2"
589+ assert results [1 ][2 ] == False
590+ assert results [2 ][1 ] == "example3"
591+ assert results [2 ][2 ] == True
592+
593+ # fetch_one()
594+ if str (database_url ).startswith ("postgresql" ):
595+ result = await raw_connection .fetchrow (select_query )
596+ else :
597+ cursor = await raw_connection .cursor ()
598+ await cursor .execute (select_query )
599+ result = await cursor .fetchone ()
600+
601+ # Raw output for the raw request
602+ assert result [1 ] == "example1"
603+ assert result [2 ] == True
0 commit comments