Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ def renameChat(self, chatId: int, title: str) -> ChatOperationResponse:
Returns:
ChatOperationResponse: the response of the operation.
"""
self.useCase.renameChat(ChatId(chatId), title)
return self.useCase.renameChat(ChatId(chatId), title)
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
from adapter._in.web.ask_chatbot_controller import AskChatbotController
from domain.chat.message import MessageSender

from domain.chat.message_response import MessageResponse
from domain.chat.message import Message
from domain.chat.message import Message, MessageSender
from domain.chat.chat_id import ChatId
from domain.document.document_id import DocumentId
import unittest

def test_askChatbot_with_existent_chat(mocker):
useCaseMock = mocker.Mock()
useCaseMock.askChatbot.return_value = MessageResponse(True, Message("response", unittest.mock.ANY, None, MessageSender.CHATBOT), ChatId(1))

askChatbotController = AskChatbotController(useCaseMock)

with unittest.mock.patch('adapter._in.web.ask_chatbot_controller.ChatId') as MockChatId, \
unittest.mock.patch('adapter._in.web.ask_chatbot_controller.Message') as MockMessage:
unittest.mock.patch('adapter._in.web.ask_chatbot_controller.Message') as MockMessage:
MockChatId.return_value = ChatId(1)
MockMessage.return_value = Message("response", unittest.mock.ANY, None, MessageSender.CHATBOT)

response = askChatbotController.askChatbot("message", 1)

MockChatId.assert_called_once_with(1)
MockMessage.assert_called_once_with(
"message",
Expand All @@ -29,27 +27,27 @@ def test_askChatbot_with_existent_chat(mocker):
)

assert isinstance(response, MessageResponse)

def test_askChatbot_without_chat(mocker):
useCaseMock = mocker.Mock()
useCaseMock.askChatbot.return_value = MessageResponse(True, Message("response", unittest.mock.ANY, None, MessageSender.CHATBOT), ChatId(1))

askChatbotController = AskChatbotController(useCaseMock)

with unittest.mock.patch('adapter._in.web.ask_chatbot_controller.ChatId') as MockChatId, \
unittest.mock.patch('adapter._in.web.ask_chatbot_controller.Message') as MockMessage:

MockChatId.return_value = ChatId(1)
MockMessage.return_value = Message("response", unittest.mock.ANY, [DocumentId("example.pdf")], MessageSender.CHATBOT)

response = askChatbotController.askChatbot("message")

MockChatId.assert_not_called()
MockMessage.assert_called_once_with(
"message",
unittest.mock.ANY,
None,
MessageSender.USER
)

assert isinstance(response, MessageResponse)
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
from typing import List

from adapter._in.web.get_chats_controller import GetChatsController
from domain.chat.chat_filter import ChatFilter
from domain.chat.chat_preview import ChatPreview
from domain.chat.message import MessageSender
from domain.chat.message_response import MessageResponse
from domain.chat.message import Message
from domain.chat.chat_id import ChatId
from domain.document.document_id import DocumentId
import unittest


def test_getChats_with_filter(mocker):
useCaseMock = mocker.Mock()
useCaseMock.getChats.return_value = [ChatPreview(id = ChatId(1),
title = "title",
lastMessage = Message("message",
unittest.mock.ANY,
[DocumentId("documentId1"), DocumentId("documentId2")],
MessageSender.USER))]

getChatsController = GetChatsController(useCaseMock)
with unittest.mock.patch('adapter._in.web.get_chats_controller.ChatFilter') as MockChatFilter:

MockChatFilter.return_value = ChatFilter("filter")
response = getChatsController.getChats("filter")

MockChatFilter.assert_called_once_with("filter")
assert isinstance(response[0], ChatPreview)


def test_getChats_without_filter(mocker):
useCaseMock = mocker.Mock()
useCaseMock.getChats.return_value = [ChatPreview(id=ChatId(1),
title="title",
lastMessage=Message("message",
unittest.mock.ANY,
[DocumentId("documentId1"),
DocumentId("documentId2")],
MessageSender.USER))]

getChatsController = GetChatsController(useCaseMock)
with unittest.mock.patch('adapter._in.web.get_chats_controller.ChatFilter') as MockChatFilter:
MockChatFilter.return_value = ChatFilter("")
response = getChatsController.getChats("")

MockChatFilter.assert_called_once_with("")
assert isinstance(response[0], ChatPreview)
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import unittest

from _in.web.get_configuration_options_controller import GetConfigurationOptionsController
from domain.configuration.configuration_options import ConfigurationOptions
from domain.configuration.document_store_configuration import DocumentStoreType, DocumentStoreConfiguration
from domain.configuration.embedding_model_configuration import EmbeddingModelConfiguration, EmbeddingModelType
from domain.configuration.llm_model_configuration import LLMModelType, LLMModelConfiguration
from domain.configuration.vector_store_configuration import VectorStoreType, VectorStoreConfiguration


def test_askChatbot_with_existent_chat(mocker):
useCaseMock = mocker.Mock()
vectorStoreConfiguration = [VectorStoreConfiguration(name=VectorStoreType.PINECONE, organization="organization", description="description", type="type", costIndicator="12")]
embeddingModel= [EmbeddingModelConfiguration(name=EmbeddingModelType.OPENAI, organization="organization", description="description", type="type", costIndicator="12")]
LLMModel = [LLMModelConfiguration(name=LLMModelType.OPENAI, organization="organization", description="description", type="type", costIndicator="12")]
documentStore= [DocumentStoreConfiguration(name=DocumentStoreType.AWS, organization="organization", description="description", type="type", costIndicator="12")]
useCaseMock.getConfigurationOptions.return_value = ConfigurationOptions(vectorStoreConfiguration, embeddingModel, LLMModel, documentStore)

getConfigurationOptions = GetConfigurationOptionsController(useCaseMock)
response = getConfigurationOptions.getConfigurationOptions()

assert isinstance(response, ConfigurationOptions)
21 changes: 21 additions & 0 deletions 3 - PB/MVP/tests/backend/adapter/_in/web/get_configuration_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import unittest
from adapter._in.web.get_configuration_controller import GetConfigurationController
from domain.configuration.configuration import Configuration
from domain.configuration.document_store_configuration import DocumentStoreType, DocumentStoreConfiguration
from domain.configuration.embedding_model_configuration import EmbeddingModelConfiguration, EmbeddingModelType
from domain.configuration.llm_model_configuration import LLMModelType, LLMModelConfiguration
from domain.configuration.vector_store_configuration import VectorStoreType, VectorStoreConfiguration


def test_askChatbot_with_existent_chat(mocker):
useCaseMock = mocker.Mock()
vectorStoreConfiguration = VectorStoreConfiguration(name=VectorStoreType.PINECONE, organization="organization", description="description", type="type", costIndicator="12")
embeddingModel= EmbeddingModelConfiguration(name=EmbeddingModelType.OPENAI, organization="organization", description="description", type="type", costIndicator="12")
LLMModel = LLMModelConfiguration(name=LLMModelType.OPENAI, organization="organization", description="description", type="type", costIndicator="12")
documentStore= DocumentStoreConfiguration(name=DocumentStoreType.AWS, organization="organization", description="description", type="type", costIndicator="12")
useCaseMock.getConfiguration.return_value = Configuration(vectorStoreConfiguration, embeddingModel, LLMModel, documentStore)

getConfiguration = GetConfigurationController(useCaseMock)
response = getConfiguration.getConfiguration()

assert isinstance(response, Configuration)
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import unittest

from adapter._in.web.get_document_content_controller import GetDocumentContentController
from domain.document.document import Document
from domain.document.document_content import DocumentContent
from domain.document.document_id import DocumentId
from domain.document.document_metadata import DocumentType, DocumentMetadata
from domain.document.document_status import DocumentStatus, Status
from domain.document.plain_document import PlainDocument


def test_get_document_content_with_id(mocker):
useCaseMock = mocker.Mock()
documentStatus = DocumentStatus(Status.ENABLED)
plainDocument = PlainDocument(DocumentMetadata(DocumentId("Prova.pdf"), DocumentType.PDF, 12, unittest.mock.ANY),
DocumentContent(b'content')
)
#getDocumentsContent è quello che simula la chiamata dello use case
useCaseMock.getDocumentsContent.return_value = [Document(documentStatus, plainDocument)]

with unittest.mock.patch('adapter._in.web.get_document_content_controller.DocumentId') as mockDocumentId:
mockDocumentId.return_value = DocumentId("Prova.pdf")

documentContentController = GetDocumentContentController(useCaseMock)

response = documentContentController.getDocumentContent("Prova.pdf")
mockDocumentId.assert_called_once_with("Prova.pdf")

assert isinstance(response, Document)

Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import unittest

from adapter._in.web.get_documents_controller import GetDocumentsController
from adapter._in.web.get_document_content_controller import GetDocumentContentController
from api_exceptions import APIElaborationException
from domain.document.document import Document
from domain.document.document_content import DocumentContent
from domain.document.document_filter import DocumentFilter
from domain.document.document_id import DocumentId
from domain.document.document_metadata import DocumentType, DocumentMetadata
from domain.document.document_status import DocumentStatus, Status
from domain.document.light_document import LightDocument
from domain.document.plain_document import PlainDocument


def test_get_document_content_with_id(mocker):
useCaseMock = mocker.Mock()
documentStatus = DocumentStatus(Status.ENABLED)
documentMetadata = DocumentMetadata(DocumentId("Prova.pdf"), DocumentType.PDF, 12, unittest.mock.ANY)
useCaseMock.getDocuments.return_value = [LightDocument(documentStatus, documentMetadata)]

with unittest.mock.patch('adapter._in.web.get_documents_controller.DocumentFilter') as mockDocumentFilter:
mockDocumentFilter.return_value = DocumentFilter("searchFilter")

getDocumentsController = GetDocumentsController(useCaseMock)

response = getDocumentsController.getDocuments("searchFilter")
mockDocumentFilter.assert_called_once_with("searchFilter")

assert isinstance(response[0], LightDocument)

def test_get_document_content_withException(mocker):
useCaseMock = mocker.Mock()
useCaseMock.getDocuments.side_effect = APIElaborationException("message")

with unittest.mock.patch('adapter._in.web.get_documents_controller.DocumentFilter') as mockDocumentFilter:
mockDocumentFilter.return_value = DocumentId("1")
getDocumentsController = GetDocumentsController(useCaseMock)
try:
response = getDocumentsController.getDocuments("1")
assert False
except APIElaborationException as e:
assert True
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import unittest

from adapter._in.web.rename_chat_controller import RenameChatController
from domain.chat.chat_id import ChatId
from domain.chat.chat_operation_response import ChatOperationResponse


def test_rename_chat_with_id(mocker):
useCaseMock = mocker.Mock()
useCaseMock.renameChat.return_value = ChatOperationResponse(True,"message",ChatId(1))

with unittest.mock.patch('adapter._in.web.rename_chat_controller.ChatId') as mockChatId:
mockChatId.return_value = ChatId(1)

renameChatController = RenameChatController(useCaseMock)

response = renameChatController.renameChat(1, "title")
mockChatId.assert_called_once_with(1)

assert isinstance(response, ChatOperationResponse)
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import unittest

from adapter._in.web.presentation_domain.new_document import NewDocument
from adapter._in.web.upload_documents_controller import UploadDocumentsController
from domain.document.document_id import DocumentId
from domain.document.document_operation_response import DocumentOperationResponse


def test_rename_chat_with_id(mocker):
useCaseMock = mocker.Mock()
useCaseMock.uploadDocuments.return_value = [DocumentOperationResponse(DocumentId('123'), True, 'message')]
uploadDocumentsController = UploadDocumentsController(useCaseMock)

response = uploadDocumentsController.uploadDocuments([NewDocument('123', 'pdf', 1.0, b'content')], False)

assert isinstance(response[0], DocumentOperationResponse)