Spaces:
Sleeping
Sleeping
File size: 2,054 Bytes
47e4aa2 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
import pytest
from unittest.mock import Mock, patch
import os
from .llm_handling import answer_question
# app/test_llm_handling.py
@pytest.fixture
def mock_embeddings():
with patch('langchain_community.embeddings.HuggingFaceEmbeddings') as mock:
yield mock
@pytest.fixture
def mock_vectorstore():
with patch('langchain_community.vectorstores.FAISS') as mock:
mock_instance = Mock()
mock_instance.as_retriever.return_value = Mock()
mock.load_local.return_value = mock_instance
yield mock
@pytest.fixture
def mock_chat_openai():
with patch('langchain_openai.ChatOpenAI') as mock:
yield mock
def test_database_not_found():
result = answer_question("test question", "nonexistent_db")
assert len(result) == 2
assert result[0]["role"] == "user"
assert result[0]["content"] == "test question"
assert result[1]["role"] == "assistant"
assert result[1]["content"] == "Database non trovato"
@patch('os.path.exists', return_value=True)
def test_successful_answer(mock_exists, mock_embeddings, mock_vectorstore, mock_chat_openai):
mock_qa_chain = Mock()
mock_qa_chain.return_value = {"result": "Test answer"}
with patch('langchain.chains.RetrievalQA.from_chain_type', return_value=mock_qa_chain):
result = answer_question("test question", "test_db")
assert len(result) == 2
assert result[0]["role"] == "user"
assert result[0]["content"] == "test question"
assert result[1]["role"] == "assistant"
assert result[1]["content"] == "Test answer"
@patch('os.path.exists', return_value=True)
def test_error_handling(mock_exists, mock_embeddings):
mock_embeddings.side_effect = Exception("Test error")
result = answer_question("test question", "test_db")
assert len(result) == 2
assert result[0]["role"] == "user"
assert result[0]["content"] == "test question"
assert result[1]["role"] == "assistant"
assert "Si è verificato un errore: Test error" in result[1]["content"] |