Skip to content

Commit

Permalink
Create Controller On Request To Create New Chat
Browse files Browse the repository at this point in the history
  • Loading branch information
alvindera97 committed Aug 31, 2024
1 parent c9975db commit f4b415d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
10 changes: 9 additions & 1 deletion api/endpoints/endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,19 @@
"""
import os
import uuid
from concurrent.futures.thread import ThreadPoolExecutor

from fastapi import FastAPI, WebSocket
from fastapi.params import Depends
from sqlalchemy.orm import Session
from starlette.responses import RedirectResponse

from controller import Controller
from database import db
from utils.functions import utility_functions

app = FastAPI()
executor = ThreadPoolExecutor()


@app.websocket("/chat/{chat_uuid}")
Expand Down Expand Up @@ -44,4 +47,9 @@ async def set_up_chat(session: Session = Depends(db.get_db)):
Creates a unique chat uuid and saves in database returning a redirect response.
:return:
"""
return "chat/" + utility_functions.add_new_chat(session)
chat_url = "chat/" + utility_functions.add_new_chat(session)

# run blocking Controller function in separate thread.
executor.submit(Controller, 1, 'ws://localhost:8000/' + chat_url)

return chat_url
11 changes: 11 additions & 0 deletions tests/api/test_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
WebSocketTestCase
"""
import os
from unittest.mock import patch

from fastapi.websockets import WebSocketDisconnect

Expand Down Expand Up @@ -95,3 +96,13 @@ def test_endpoint_redirect_url_matches_that_of_the_expected_chat_url(self) -> No

self.assertEqual(f'chat/{[i for i in self.session.query(Chat)][-1].uuid.__str__()}',
'/'.join(response.url.__str__().split("/")[-2:]))

def test_endpoint_creates_new_application_controller_for_chat_session(self) -> None:
"""
Test that endpoint creates application controller.
:return: None
"""
with patch("api.endpoints.endpoints.Controller") as mock_application_controller:
with patch("controller.controller_def.websockets"):
response = self.client.post("/set_up_chat/", follow_redirects=True)
mock_application_controller.assert_called_once_with(1, "ws://localhost:8000/" + "/".join(response.url.__str__().split("/")[-2:]))

0 comments on commit f4b415d

Please sign in to comment.