You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
94 lines
3.6 KiB
Python
94 lines
3.6 KiB
Python
import os
|
|
import unittest
|
|
from unittest.mock import AsyncMock, patch
|
|
|
|
os.environ.setdefault("DEBUG", "false")
|
|
|
|
from app.db.mock_models import Vehicle
|
|
from app.services.domain import order_service, rental_service, review_service
|
|
from tests.test_order_service import FakeSession
|
|
from tests.test_rental_service import RentalServiceTests
|
|
from tests.test_review_service import ReviewLockingSession
|
|
|
|
|
|
class IntegrationDomainHookTests(unittest.IsolatedAsyncioTestCase):
|
|
async def test_realizar_pedido_publica_evento_apos_sucesso(self):
|
|
vehicle = Vehicle(id=8, modelo="Toyota Corolla 2024", categoria="suv", preco=76087.0)
|
|
session = FakeSession(vehicle=vehicle)
|
|
fake_publish = AsyncMock(return_value=[])
|
|
|
|
async def fake_hydrate_mock_customer_from_cpf(cpf: str, user_id: int | None = None):
|
|
return {"cpf": cpf, "user_id": user_id}
|
|
|
|
async def fake_validar_cliente_venda(cpf: str, valor_veiculo: float):
|
|
return {"aprovado": True}
|
|
|
|
with patch.object(order_service, "SessionMockLocal", return_value=session), patch.object(
|
|
order_service,
|
|
"hydrate_mock_customer_from_cpf",
|
|
new=fake_hydrate_mock_customer_from_cpf,
|
|
), patch.object(
|
|
order_service,
|
|
"validar_cliente_venda",
|
|
new=fake_validar_cliente_venda,
|
|
), patch.object(order_service, "_get_vehicle_for_update", return_value=vehicle), patch.object(
|
|
order_service,
|
|
"_get_active_order_for_vehicle",
|
|
return_value=None,
|
|
), patch.object(order_service, "publish_business_event_safely", fake_publish):
|
|
await order_service.realizar_pedido(cpf="123.456.789-09", vehicle_id=8)
|
|
|
|
fake_publish.assert_awaited_once()
|
|
self.assertEqual(fake_publish.await_args.args[0], order_service.ORDER_CREATED_EVENT)
|
|
|
|
async def test_agendar_revisao_publica_evento_apos_sucesso(self):
|
|
session = ReviewLockingSession(query_results=[None, None])
|
|
fake_publish = AsyncMock(return_value=[])
|
|
|
|
with patch.object(review_service, "SessionMockLocal", return_value=session), patch.object(
|
|
review_service,
|
|
"publish_business_event_safely",
|
|
fake_publish,
|
|
):
|
|
await review_service.agendar_revisao(
|
|
placa="ABC1234",
|
|
data_hora="18/03/2026 09:00",
|
|
modelo="Onix",
|
|
ano=2022,
|
|
km=15000,
|
|
revisao_previa_concessionaria=False,
|
|
user_id=7,
|
|
)
|
|
|
|
fake_publish.assert_awaited_once()
|
|
self.assertEqual(fake_publish.await_args.args[0], review_service.REVIEW_SCHEDULED_EVENT)
|
|
|
|
async def test_abrir_locacao_publica_evento_apos_sucesso(self):
|
|
helper = RentalServiceTests()
|
|
SessionLocal = helper._build_session_local()
|
|
db = SessionLocal()
|
|
try:
|
|
vehicle = helper._create_rental_vehicle(db)
|
|
vehicle_placa = vehicle.placa
|
|
finally:
|
|
db.close()
|
|
|
|
fake_publish = AsyncMock(return_value=[])
|
|
with patch("app.services.domain.rental_service.SessionMockLocal", SessionLocal), patch.object(
|
|
rental_service,
|
|
"publish_business_event_safely",
|
|
fake_publish,
|
|
):
|
|
await rental_service.abrir_locacao_aluguel(
|
|
placa=vehicle_placa,
|
|
data_inicio="17/03/2026 10:00",
|
|
data_fim_prevista="20/03/2026 10:00",
|
|
)
|
|
|
|
fake_publish.assert_awaited_once()
|
|
self.assertEqual(fake_publish.await_args.args[0], rental_service.RENTAL_OPENED_EVENT)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|