191 lines
6.0 KiB
Python
191 lines
6.0 KiB
Python
# # import pytest
|
|
# # import asyncio # Importa asyncio para gerenciar loops de eventos
|
|
# # from fastapi.testclient import TestClient
|
|
# # from sqlalchemy import select
|
|
# # from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession
|
|
# # from sqlalchemy.orm import sessionmaker
|
|
# # from app.main import init_app
|
|
# # from app.database.session import sessionmanager
|
|
# # from app.database.models import Permissao
|
|
# # from app.database.session import Base # Certifique-se de que o Base contém todos os modelos
|
|
# # from app.config import URL_BD
|
|
# # from app.scripts.initialize_permissions_roles import process_permissions
|
|
# #
|
|
# # # Criação do engine de testes
|
|
# # engine = create_async_engine(URL_BD, echo=True)
|
|
# # TestSessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine, class_=AsyncSession)
|
|
# #
|
|
# #
|
|
# # async def create_test_database():
|
|
# # """
|
|
# # Exclui e recria todas as tabelas do banco de dados para garantir um estado limpo.
|
|
# # """
|
|
# # async with engine.begin() as conn:
|
|
# # # Exclui as tabelas se elas já existirem
|
|
# # await conn.run_sync(Base.metadata.drop_all)
|
|
# # # Cria as tabelas novamente
|
|
# # await conn.run_sync(Base.metadata.create_all)
|
|
# #
|
|
# # # Insere dados iniciais na tabela Permissao
|
|
# # async with AsyncSession(engine) as session:
|
|
# # permissoes = [
|
|
# # Permissao(nome="Permissão Total"),
|
|
# #
|
|
# # ]
|
|
# #
|
|
# # session.add_all(permissoes)
|
|
# # await session.commit()
|
|
# #
|
|
# #
|
|
# # @pytest.fixture(scope="module")
|
|
# # def client():
|
|
# # """
|
|
# # Fixture para inicializar o cliente de testes do FastAPI com o banco de dados de testes.
|
|
# # Cria as tabelas do banco de dados antes de iniciar o cliente.
|
|
# # """
|
|
# # # Inicializa o sessionmanager com a URL do banco de testes
|
|
# # sessionmanager.init(URL_BD)
|
|
# #
|
|
# # # Cria as tabelas no banco de testes (exclui e recria)
|
|
# # asyncio.run(create_test_database())
|
|
# #
|
|
# # # Inicializa o aplicativo FastAPI para testes
|
|
# # app = init_app(init_db=False)
|
|
# #
|
|
# # with TestClient(app) as c:
|
|
# # yield c
|
|
# #
|
|
# #
|
|
# # @pytest.fixture(scope="function")
|
|
# # async def session():
|
|
# # """
|
|
# # Fixture para fornecer uma sessão do SQLAlchemy para os testes.
|
|
# # """
|
|
# # async with TestSessionLocal() as session:
|
|
# # yield session
|
|
# #
|
|
# #
|
|
# import pytest
|
|
# import asyncio
|
|
# from fastapi.testclient import TestClient
|
|
# from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession
|
|
# from sqlalchemy.orm import sessionmaker
|
|
# from app.main import init_app
|
|
# from app.database.session import sessionmanager
|
|
# from app.database.models import Permissao
|
|
# from app.database.session import Base
|
|
# from app.config import URL_BD
|
|
#
|
|
# # Criação do engine de testes
|
|
# engine = create_async_engine(URL_BD, echo=True)
|
|
# TestSessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine, class_=AsyncSession)
|
|
#
|
|
#
|
|
# async def create_test_database():
|
|
# """
|
|
# Exclui e recria todas as tabelas do banco de dados para garantir um estado limpo.
|
|
# """
|
|
# async with engine.begin() as conn:
|
|
# await conn.run_sync(Base.metadata.drop_all)
|
|
# await conn.run_sync(Base.metadata.create_all)
|
|
#
|
|
# # Insere dados iniciais na tabela Permissao
|
|
# async with AsyncSession(engine) as session:
|
|
# permissoes = [
|
|
# Permissao(nome="Permissão Total"),
|
|
# ]
|
|
# session.add_all(permissoes)
|
|
# await session.commit()
|
|
#
|
|
#
|
|
# @pytest.fixture(scope="module")
|
|
# def client():
|
|
# """
|
|
# Fixture para inicializar o cliente de testes do FastAPI com o banco de dados de testes.
|
|
# Cria as tabelas do banco de dados antes de iniciar o cliente.
|
|
# """
|
|
# # Inicializa o sessionmanager com a URL do banco de testes
|
|
# sessionmanager.init(URL_BD)
|
|
#
|
|
# # Cria as tabelas no banco de testes (exclui e recria)
|
|
# asyncio.run(create_test_database())
|
|
#
|
|
# # Inicializa o aplicativo FastAPI para testes
|
|
# app = init_app(init_db=False)
|
|
#
|
|
# with TestClient(app) as c:
|
|
# yield c
|
|
#
|
|
#
|
|
# @pytest.fixture(scope="function")
|
|
# async def session():
|
|
# """
|
|
# Fixture para fornecer uma sessão do SQLAlchemy para os testes.
|
|
# """
|
|
# async with TestSessionLocal() as session:
|
|
# yield session
|
|
#
|
|
#
|
|
# @pytest.fixture(scope="session")
|
|
# def event_loop():
|
|
# """
|
|
# Cria um novo loop de eventos para a sessão de testes.
|
|
# """
|
|
# loop = asyncio.new_event_loop()
|
|
# yield loop
|
|
# loop.close()
|
|
|
|
import pytest
|
|
import asyncio
|
|
from httpx import AsyncClient
|
|
from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession
|
|
from sqlalchemy.orm import sessionmaker
|
|
from app.main import app # Importa o app diretamente do main.py
|
|
from app.database.session import sessionmanager
|
|
from app.database.models import Permissao
|
|
from app.database.session import Base
|
|
from app.config import URL_BD
|
|
|
|
# Criação do engine de testes
|
|
engine = create_async_engine(URL_BD, echo=True)
|
|
TestSessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine, class_=AsyncSession)
|
|
|
|
|
|
async def create_test_database():
|
|
async with engine.begin() as conn:
|
|
await conn.run_sync(Base.metadata.drop_all)
|
|
await conn.run_sync(Base.metadata.create_all)
|
|
|
|
async with AsyncSession(engine) as session:
|
|
permissoes = [
|
|
Permissao(nome="Permissão Total"),
|
|
]
|
|
session.add_all(permissoes)
|
|
await session.commit()
|
|
|
|
|
|
@pytest.fixture(scope="session")
|
|
def event_loop():
|
|
loop = asyncio.get_event_loop_policy().new_event_loop()
|
|
yield loop
|
|
loop.close()
|
|
|
|
|
|
@pytest.fixture(scope="session")
|
|
async def client():
|
|
# Inicializa o sessionmanager com a URL do banco de testes
|
|
sessionmanager.init(URL_BD)
|
|
|
|
# Cria as tabelas no banco de testes
|
|
await create_test_database()
|
|
|
|
# Usa o app diretamente do main.py
|
|
async with AsyncClient(app=app, base_url="http://test") as ac:
|
|
yield ac
|
|
|
|
|
|
@pytest.fixture(scope="function")
|
|
async def session():
|
|
async with TestSessionLocal() as session:
|
|
yield session
|