Working factories

This commit is contained in:
Eden Kirin
2023-09-20 21:25:05 +02:00
parent 6109630ed1
commit 6ccb660ccc
8 changed files with 109 additions and 27 deletions

View File

View File

View File

@ -0,0 +1,15 @@
import factory
from faker import Faker
from app.domain.city import City
from tests.factories.factory_base import FactoryBase
fake = Faker()
class CityFactory(FactoryBase):
name = factory.Faker("city")
postal_code = factory.Faker("postcode")
class Meta:
model = City

View File

@ -0,0 +1,11 @@
from async_factory_boy.factory.sqlalchemy import AsyncSQLAlchemyFactory
from sqlalchemy.orm import scoped_session
from tests.conftest import async_session_factory
session = scoped_session(async_session_factory)
class FactoryBase(AsyncSQLAlchemyFactory):
class Meta:
sqlalchemy_session = session

28
tests/test_cities.py Normal file
View File

@ -0,0 +1,28 @@
import pytest
from litestar.status_codes import HTTP_200_OK
from litestar.testing import AsyncTestClient
from sqlalchemy.ext.asyncio import AsyncSession
from main import app
from tests.factories.city_factory import CityFactory
class TestCitiesEndpoints:
@pytest.fixture(scope="function", autouse=True)
def setup_class(self, db_session: AsyncSession):
self.db_session = db_session
@pytest.mark.asyncio
async def test_get(self):
city_1 = await CityFactory.create()
city_2 = await CityFactory.create()
city_3 = await CityFactory.create()
city_4 = await CityFactory.create()
city_5 = await CityFactory.create()
async with AsyncTestClient(app=app) as client:
response = await client.get("/v1/cities")
print("#" * 100)
print(response.content)
print("#" * 100)
assert response.status_code == HTTP_200_OK

View File

@ -2,40 +2,30 @@
# https://coderpad.io/blog/development/a-guide-to-database-unit-testing-with-pytest-and-sqlalchemy/
import pytest
from sqlalchemy import text, event
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker
from sqlalchemy import text
from sqlalchemy.ext.asyncio import AsyncSession
from app.domain.city import City
from app.lib import settings
from app.lib.sqlalchemy_plugin import engine
import pytest
import pytest_asyncio
import sqlalchemy
from sqlalchemy.ext.asyncio import (
AsyncSession,
create_async_engine,
async_scoped_session,
AsyncConnection,
)
from tests.factories.city_factory import CityFactory
class TestGeneral:
@pytest.fixture(scope="function", autouse=True)
def setup_class(self, db_session):
def setup_class(self, db_session: AsyncSession):
self.db_session = db_session
# async def teardown_class(self):
# await self.session.rollback()
# await self.session.close()
@pytest.mark.asyncio
async def test_bla(self):
async def test_get_cities(self):
city_1 = await CityFactory.create()
city_2 = await CityFactory.create()
city_3 = await CityFactory.create()
city_4 = await CityFactory.create()
city_5 = await CityFactory.create()
stmt = text("select * from cities")
result = await self.db_session.execute(stmt)
print("#"*100)
print("#" * 100)
for c in result:
print(c)
print("#"*100)
print("#" * 100)
assert True