Skip to content
Open
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
9 changes: 5 additions & 4 deletions app/domain/models.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
from typing import cast

import shortuuid
from langchain_core.messages import human, ai
from pydantic import v1 as pd1
from pydantic import v1 as pd1 # Utilizando pydantic v1 de manera consistente
from langchain_core.messages.ai import AIMessage


def generate_uuid() -> str:
return cast(str, shortuuid.uuid())


# Entities


class Conversation(pd1.BaseModel):
history: list[ai.AIMessage | human.HumanMessage] = pd1.Field(default_factory=list)

class Config:
arbitrary_types_allowed = True # Permitir tipos personalizados


# Aggregates
class Chat(pd1.BaseModel):
Expand Down
2 changes: 1 addition & 1 deletion app/services/usecases.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def continue_conversation(
return self._agent.get_last_response()

def _update_chat(self, chat: models.Chat) -> None:
chat = models.Chat(id=chat.id)
# Aquí utilizamos el objeto `chat` que recibimos como argumento en lugar de crear uno nuevo
chat.update_conversation(self._agent.get_conversation_history())
self._db.save_chat(chat)

Expand Down
86 changes: 85 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ python-multipart = "^0.0.11"
shortuuid = "^1.0.13"
boto3 = "^1.35.29"
langfuse = "^2.51.2"
coverage = "^7.6.1"
pytest = "^8.3.3"

[tool.poetry.group.dev.dependencies]
black = "^24.8.0"
Expand Down
Empty file added test/__init__.py
Empty file.
Binary file added test/fotoParcialSoft2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
81 changes: 81 additions & 0 deletions test/test_app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import pytest
from app.domain import ports, models
from app.services.usecases import ChatService
from unittest.mock import MagicMock


@pytest.fixture
def mock_agent():
return MagicMock(spec=ports.AgentPort)


@pytest.fixture
def mock_db():
return MagicMock(spec=ports.ChatRepository)


@pytest.fixture
def mock_transcriber():
return MagicMock(spec=ports.TranscriptionPort)


@pytest.fixture
def chat_service(mock_agent, mock_db, mock_transcriber):
return ChatService(agent=mock_agent, db=mock_db, transcriber=mock_transcriber)



def test_deberia_iniciar_chat_al_llamar_iniciar_chat(chat_service, mock_db):
# Arrange
expected_chat_model = models.Chat(id="mock_chat_id")
mock_db.save_chat.return_value = None
mock_db.save_chat.side_effect = lambda chat: setattr(chat, 'id', expected_chat_model.id)

# Act
chat_id = chat_service.start_conversation()

# Assert
mock_db.save_chat.assert_called_once()
assert chat_id == expected_chat_model.id


def test_deberia_continuar_conversacion_con_consulta_al_llamar_continuar_conversacion(chat_service, mock_db, mock_agent):

conversation_id = "mock_chat_id"
query = "¿Por qué Java nunca se siente solo?"

chat_model = MagicMock(spec=models.Chat)
chat_model.id = conversation_id
chat_model.get_conversation_history.return_value = []

mock_db.get_chat.return_value = chat_model
mock_agent.get_last_response.return_value = "¡Porque siempre tiene un montón de classes!"


response = chat_service.continue_conversation(conversation_id, query=query)


mock_db.get_chat.assert_called_once_with(conversation_id)
mock_agent.set_memory_variables.assert_called_once_with([])
mock_agent.assert_called_once_with(query=query)
assert response == "¡Porque siempre tiene un montón de classes!"


def test_debería_actualizar_chat_al_llamar_a_actualizar_chat(chat_service, mock_db, mock_agent):

chat_id = "mock_chat_id"

chat_model = MagicMock(spec=models.Chat)
chat_model.id = chat_id
mock_db.get_chat.return_value = chat_model

mock_agent.get_conversation_history.return_value = ["Hi there!", "I'm doing great, thanks!"]



chat_service._update_chat(chat_model)


mock_agent.get_conversation_history.assert_called_once()
mock_agent.get_conversation_history.return_value = ["Hi there!", "I'm doing great, thanks!"]
mock_db.save_chat.assert_called_once_with(chat_model)