Skip to content

Commit

Permalink
[DOP-21294] Rafactor lineage fixtures
Browse files Browse the repository at this point in the history
  • Loading branch information
TiGrib committed Nov 14, 2024
1 parent 4ff313e commit 2236542
Show file tree
Hide file tree
Showing 10 changed files with 1,082 additions and 1,028 deletions.
64 changes: 1 addition & 63 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 Down Expand Up @@ -35,60 +30,3 @@ async def create_address(
await async_session.commit()
await async_session.refresh(address)
return address


@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()
10 changes: 4 additions & 6 deletions tests/test_server/fixtures/factories/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@

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

from data_rentgen.db.models import Address, Dataset
from data_rentgen.db.models import Dataset
from data_rentgen.db.models.dataset_symlink import DatasetSymlink, DatasetSymlinkType
from tests.test_server.fixtures.factories.base import random_string
from tests.test_server.fixtures.factories.location import create_location
Expand Down Expand Up @@ -251,10 +250,9 @@ async def datasets_search(

datasets_by_name = {dataset.name: dataset for dataset in datasets_with_name}
datasets_by_location = dict(zip([location.name for location in locations_with_name], datasets_with_location_name))
datasets_by_address = {
name: dataset
for name, dataset in zip(addresses_url, [dataset for dataset in datasets_with_address_urls for _ in range(2)])
}
datasets_by_address = dict(
zip(addresses_url, [dataset for dataset in datasets_with_address_urls for _ in range(2)]),
)

yield datasets_by_name, datasets_by_location, datasets_by_address

Expand Down
Loading

0 comments on commit 2236542

Please sign in to comment.