rag-chatbot / tests /test_cache.py
Abeshith's picture
RAG Chatbot with LangChain, FastAPI, and service layer architecture
64d7fdf
import pytest
from app.core.cache import semantic_cache
@pytest.mark.unit
class TestSemanticCache:
@pytest.mark.asyncio
async def test_cache_set_and_get(self, clear_cache, sample_query):
response = "This is a test response."
await semantic_cache.set(sample_query, response)
cached = await semantic_cache.get(sample_query)
assert cached == response
@pytest.mark.asyncio
async def test_cache_miss(self, clear_cache):
cached = await semantic_cache.get("This query was never cached")
assert cached is None
@pytest.mark.asyncio
async def test_cache_with_use_context_flag(self, clear_cache, sample_query):
response_rag = "Response with RAG"
response_no_rag = "Response without RAG"
await semantic_cache.set(sample_query, response_rag, use_context=True)
await semantic_cache.set(sample_query, response_no_rag, use_context=False)
cached_rag = await semantic_cache.get(sample_query, use_context=True)
cached_no_rag = await semantic_cache.get(sample_query, use_context=False)
assert cached_rag == response_rag
assert cached_no_rag == response_no_rag
assert cached_rag != cached_no_rag