@@ -107,20 +107,19 @@ from sqlalchemy.ext.asyncio import (
107107
108108DATABASE_URL = ' sqlite+aiosqlite:///db.sqlite3'
109109
110- engine = create_async_engine(DATABASE_URL , future = True , echo = True )
110+ engine = create_async_engine(DATABASE_URL , echo = True )
111111
112112AsyncSessionLocal = async_sessionmaker(
113- autocommit = False ,
114- expire_on_commit = False ,
115- autoflush = True ,
116113 bind = engine,
114+ expire_on_commit = False ,
117115 class_ = AsyncSession,
118116)
119117
120118
121119async def get_session () -> typing.AsyncGenerator[AsyncSession, None ]:
122120 async with AsyncSessionLocal() as session:
123121 yield session
122+
124123```
125124
126125The last file before creating the routes is the ` schemas.py ` , which will contain all the [ Pydantic] ( https://docs.pydantic.dev/latest/ ) {: target ="\_ blank"} models.
@@ -197,8 +196,7 @@ SessionDep = Annotated[AsyncSession, Depends(get_session)]
197196async def get_all_tickets (session : SessionDep):
198197 async with session.begin():
199198 tickets = await session.scalars(select(Ticket))
200-
201- all_tickets = tickets.all()
199+ all_tickets = tickets.all()
202200
203201 return {' tickets' : all_tickets}
204202
@@ -213,10 +211,6 @@ async def create_ticket(session: SessionDep, ticket_in: TicketRequestCreate):
213211
214212 async with session.begin():
215213 session.add(new_ticket)
216- await session.commit()
217-
218- async with session.begin():
219- await session.refresh(new_ticket)
220214
221215 return new_ticket
222216
@@ -253,11 +247,6 @@ async def get_ticket_by_id(session: SessionDep, ticket_in: TicketRequestBuy):
253247 detail = ' Ticket has already been sold' ,
254248 )
255249
256- await session.commit()
257-
258- async with session.begin():
259- await session.refresh(ticket_db)
260-
261250 return ticket_db
262251
263252```
@@ -425,7 +414,6 @@ async def test_get_all_tickets_success(
425414
426415 async with async_session.begin():
427416 async_session.add_all(tickets)
428- await async_session.commit()
429417
430418 response = await async_client.get(' /tickets/all' )
431419
@@ -438,6 +426,7 @@ async def test_get_all_tickets_when_empty(async_client: AsyncClient):
438426
439427 assert response.status_code == HTTPStatus.OK
440428 assert response.json()[' tickets' ] == []
429+
441430```
442431
443432In total there are 6 test, and the rest of them has the same logic. Their full implementations can be checked in the repository.
0 commit comments