File size: 1,290 Bytes
64d7fdf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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