Skip to content

Commit

Permalink
ref: call session.close() instead of using two separate sessions in t…
Browse files Browse the repository at this point in the history
…est_create_without_commit
  • Loading branch information
e-kondr01 committed Feb 20, 2024
1 parent ff293e7 commit bf0ff27
Showing 1 changed file with 12 additions and 25 deletions.
37 changes: 12 additions & 25 deletions tests/test_public_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from fastapi import HTTPException
from sqlalchemy import insert, select
from sqlalchemy.exc import MissingGreenlet
from sqlalchemy.ext.asyncio import AsyncConnection, AsyncSession, AsyncTransaction
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.orm import selectinload

from tests.models import (
Expand Down Expand Up @@ -295,33 +295,22 @@ async def test_create_unique_filed_validation(session: AsyncSession):
)


async def test_create_without_commit(
connection: AsyncConnection, transaction: AsyncTransaction
):
async def test_create_without_commit(session: AsyncSession):
category_title = "test-category-title"
no_commit_category_title = f"{category_title}-no-commit"

first_session = AsyncSession(
bind=connection,
join_transaction_mode="create_savepoint",
expire_on_commit=False,
await category_manager.create(
session=session, in_obj=CategorySchema(title=category_title), commit=True
)
async with first_session as session:
await category_manager.create(
session=session, in_obj=CategorySchema(title=category_title), commit=True
)
await category_manager.create(
session=session,
in_obj=CategorySchema(title=no_commit_category_title),
commit=False,
)

second_session = AsyncSession(
bind=connection,
join_transaction_mode="create_savepoint",
expire_on_commit=False,
await category_manager.create(
session=session,
in_obj=CategorySchema(title=no_commit_category_title),
commit=False,
)
async with second_session as session:

await session.close()

async with session:
category_to_check_without_commit = await session.execute(
select(Category).where(Category.title == no_commit_category_title)
)
Expand All @@ -332,8 +321,6 @@ async def test_create_without_commit(
)
assert category_to_check_with_commit.scalars().first().title == category_title

await transaction.rollback()


async def test_update(session: AsyncSession):
category = Category(title="test-update-category-title")
Expand Down

0 comments on commit bf0ff27

Please sign in to comment.