chwellofficial/nt360Slides
0
1import asyncio2import uuid3from unittest.mock import AsyncMock, patch4 5import pytest6from fastapi import HTTPException7from pydantic import ValidationError8 9from api.v1.ppt.endpoints.presentation import generate_presentation_sync10from models.generate_presentation_request import GeneratePresentationRequest11from models.presentation_and_path import PresentationPathAndEditPath12 13 14class FakeAsyncSession:15 async def get(self, *_args, **_kwargs):16 return None17 18 def add(self, *_args, **_kwargs):19 return None20 21 def add_all(self, *_args, **_kwargs):22 return None23 24 async def commit(self):25 return None26 27 28class TestPresentationGenerationAPI:29 def test_generate_presentation_export_as_pdf(self):30 request = GeneratePresentationRequest(31 content="Create a presentation about artificial intelligence and machine learning",32 n_slides=5,33 language="English",34 export_as="pdf",35 template="general",36 )37 response_payload = PresentationPathAndEditPath(38 presentation_id=uuid.uuid4(),39 path="/tmp/exports/test.pdf",40 edit_path="/presentation?id=test",41 )42 43 with patch(44 "api.v1.ppt.endpoints.presentation.generate_presentation_handler",45 new=AsyncMock(return_value=response_payload),46 ) as mock_handler:47 response = asyncio.run(48 generate_presentation_sync(request, sql_session=FakeAsyncSession())49 )50 51 assert response == response_payload52 mock_handler.assert_awaited_once()53 54 def test_generate_presentation_export_as_pptx(self):55 request = GeneratePresentationRequest(56 content="Create a presentation about artificial intelligence and machine learning",57 n_slides=5,58 language="English",59 export_as="pptx",60 template="general",61 )62 response_payload = PresentationPathAndEditPath(63 presentation_id=uuid.uuid4(),64 path="/tmp/exports/test.pptx",65 edit_path="/presentation?id=test",66 )67 68 with patch(69 "api.v1.ppt.endpoints.presentation.generate_presentation_handler",70 new=AsyncMock(return_value=response_payload),71 ) as mock_handler:72 response = asyncio.run(73 generate_presentation_sync(request, sql_session=FakeAsyncSession())74 )75 76 assert response == response_payload77 mock_handler.assert_awaited_once()78 79 def test_generate_presentation_with_no_content(self):80 with pytest.raises(ValidationError):81 GeneratePresentationRequest.model_validate(82 {83 "n_slides": 5,84 "language": "English",85 "export_as": "pdf",86 "template": "general",87 }88 )89 90 def test_generate_presentation_with_n_slides_less_than_one(self):91 request = GeneratePresentationRequest(92 content="Create a presentation about artificial intelligence and machine learning",93 n_slides=0,94 language="English",95 export_as="pdf",96 template="general",97 )98 99 with pytest.raises(HTTPException) as exc:100 asyncio.run(101 generate_presentation_sync(request, sql_session=FakeAsyncSession())102 )103 104 assert exc.value.status_code == 400105 assert exc.value.detail == "Number of slides must be greater than 0"106 107 def test_generate_presentation_with_invalid_export_type(self):108 with pytest.raises(ValidationError):109 GeneratePresentationRequest.model_validate(110 {111 "content": "Create a presentation about artificial intelligence and machine learning",112 "n_slides": 5,113 "language": "English",114 "export_as": "invalid_type",115 "template": "general",116 }117 )118 