intra_max_bot/tests/test_application_service.py
2026-03-19 16:47:49 +03:00

95 lines
2.6 KiB
Python

import asyncio
from app.models import ExternalCreateResult
from app.repositories.applications import ApplicationRepository
from app.services.application_service import ApplicationService
class ExternalClientOk:
enabled = True
async def create_ticket(self, application):
return ExternalCreateResult(ok=True, external_id="EXT-1")
class ExternalClientFail:
enabled = True
async def create_ticket(self, application):
return ExternalCreateResult(ok=False, error="boom")
def test_service_external_success(database):
repo = ApplicationRepository(database)
service = ApplicationService(
repo=repo,
external_client=ExternalClientOk(),
channel_local_enabled=True,
channel_external_enabled=True,
)
outcome = asyncio.run(
service.create_application(
customer_id=1,
customer_name="Bob",
chat_id=2,
category="A",
description="text",
attachments=[],
source="test",
)
)
assert outcome.created_local is True
assert outcome.sent_external is True
assert outcome.application is not None
assert outcome.application.external_sync_status == "sent"
def test_service_external_failure_keeps_local(database):
repo = ApplicationRepository(database)
service = ApplicationService(
repo=repo,
external_client=ExternalClientFail(),
channel_local_enabled=True,
channel_external_enabled=True,
)
outcome = asyncio.run(
service.create_application(
customer_id=1,
customer_name="Bob",
chat_id=2,
category="A",
description="text",
attachments=[],
source="test",
)
)
assert outcome.created_local is True
assert outcome.sent_external is False
assert outcome.external_error == "boom"
assert outcome.application is not None
assert outcome.application.external_sync_status == "failed"
def test_service_both_channels_disabled(database):
repo = ApplicationRepository(database)
service = ApplicationService(
repo=repo,
external_client=ExternalClientOk(),
channel_local_enabled=False,
channel_external_enabled=False,
)
try:
asyncio.run(
service.create_application(
customer_id=1,
customer_name="Bob",
chat_id=2,
category="A",
description="text",
attachments=[],
source="test",
)
)
assert False, "Expected ValueError"
except ValueError:
pass