Skip to content

Commit

Permalink
[DOP-21294] refactor fixtures (#106)
Browse files Browse the repository at this point in the history
* [DOP-21294] refactor fixtures

* [DOP-21294] refactor fixtures jobs fixtures

* [DOP-21294] refactor fixtures for operations

* [DOP-21294] refactor fixtures for datasets

* [DOP-21294] Rafactor lineage fixtures

* [DOP-21294] fixes

* [DOP-21294] fixes

* [DOP-21294] fixes
  • Loading branch information
TiGrib authored Nov 14, 2024
1 parent e295082 commit 68a1ac6
Show file tree
Hide file tree
Showing 20 changed files with 1,547 additions and 1,465 deletions.
2 changes: 1 addition & 1 deletion data_rentgen/db/repositories/location.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ async def paginate(
)

async def update_external_id(self, location_id: int, external_id: str | None) -> Location:
query = select(Location).where(Location.id == location_id)
query = select(Location).where(Location.id == location_id).options(selectinload(Location.addresses))
location = await self._session.scalar(query)
if not location:
raise EntityNotFoundError("Location", "id", location_id)
Expand Down
76 changes: 15 additions & 61 deletions tests/test_server/fixtures/factories/address.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
from collections.abc import AsyncGenerator
from random import randint
from typing import AsyncContextManager, Callable

import pytest
import pytest_asyncio
from sqlalchemy import delete
from sqlalchemy.ext.asyncio import AsyncSession

from data_rentgen.db.models import Address, Location
from data_rentgen.db.models import Address
from tests.test_server.fixtures.factories.base import random_string


Expand All @@ -21,58 +16,17 @@ def address_factory(**kwargs):
return Address(**data)


@pytest_asyncio.fixture(params=[{}])
async def address(
request: pytest.FixtureRequest,
async_session_maker: Callable[[], AsyncContextManager[AsyncSession]],
location: Location,
) -> AsyncGenerator[Address, None]:
params = request.param
item = address_factory(location_id=location.id, **params)

del item.id

async with async_session_maker() as async_session:
async_session.add(item)

await async_session.commit()
await async_session.refresh(item)

async_session.expunge_all()

yield item

delete_query = delete(Address).where(Address.id == item.id)
# Add teardown cause fixture async_session doesn't used
async with async_session_maker() as async_session:
await async_session.execute(delete_query)
await async_session.commit()


@pytest_asyncio.fixture(params=[(2, {})])
async def addresses(
request: pytest.FixtureRequest,
async_session_maker: Callable[[], AsyncContextManager[AsyncSession]],
locations: list[Location],
) -> AsyncGenerator[list[Address], None]:
size, params = request.param
items = [address_factory(location_id=location.id, **params) for _ in range(size) for location in locations]

async with async_session_maker() as async_session:
for item in items:
del item.id
async_session.add(item)

await async_session.commit()
for item in items:
await async_session.refresh(item)

async_session.expunge_all()

yield items

delete_query = delete(Address).where(Address.id.in_([item.id for item in items]))
# Add teardown cause fixture async_session doesn't used
async with async_session_maker() as async_session:
await async_session.execute(delete_query)
await async_session.commit()
async def create_address(
async_session: AsyncSession,
location_id: int,
address_kwargs: dict | None = None,
) -> Address:
if address_kwargs:
address_kwargs.update({"location_id": location_id})
else:
address_kwargs = {"location_id": location_id}
address = address_factory(**address_kwargs)
async_session.add(address)
await async_session.commit()
await async_session.refresh(address)
return address
Loading

0 comments on commit 68a1ac6

Please sign in to comment.