|
|
|
""" |
|
Test the new cache system to verify it works correctly. |
|
""" |
|
|
|
import asyncio |
|
import sys |
|
import tempfile |
|
import shutil |
|
from pathlib import Path |
|
|
|
|
|
project_root = Path(__file__).parent.parent |
|
sys.path.insert(0, str(project_root)) |
|
|
|
from src.services.word_cache import WordCacheManager |
|
|
|
async def test_cache_system(): |
|
"""Test the cache system functionality.""" |
|
print("π§ͺ Testing Word Cache System\n") |
|
|
|
|
|
temp_dir = tempfile.mkdtemp() |
|
print(f"π Using temporary cache directory: {temp_dir}") |
|
|
|
try: |
|
|
|
cache_manager = WordCacheManager(cache_dir=temp_dir) |
|
await cache_manager.initialize() |
|
|
|
|
|
print("\nπ§ͺ Test 1: Caching words") |
|
test_words = [ |
|
{"word": "ELEPHANT", "clue": "Large mammal with trunk", "similarity": 0.8, "source": "vector_search"}, |
|
{"word": "TIGER", "clue": "Striped big cat", "similarity": 0.7, "source": "vector_search"}, |
|
{"word": "LION", "clue": "King of jungle", "similarity": 0.75, "source": "vector_search"}, |
|
] |
|
|
|
success = await cache_manager.cache_words("Animals", "medium", test_words) |
|
print(f"β
Cache operation {'succeeded' if success else 'failed'}") |
|
|
|
|
|
print("\nπ§ͺ Test 2: Retrieving cached words") |
|
cached_words = await cache_manager.get_cached_words("Animals", "medium", 5) |
|
print(f"π¦ Retrieved {len(cached_words)} cached words") |
|
|
|
if cached_words: |
|
print("π Cached words:") |
|
for word in cached_words: |
|
print(f" - {word['word']}: {word['clue']}") |
|
|
|
|
|
print("\nπ§ͺ Test 3: Cache statistics") |
|
stats = cache_manager.get_cache_stats() |
|
print(f"π Cache stats: {stats}") |
|
|
|
|
|
print("\nπ§ͺ Test 4: Non-existent topic") |
|
empty_words = await cache_manager.get_cached_words("NonExistent", "medium", 5) |
|
print(f"π Non-existent topic returned {len(empty_words)} words (expected 0)") |
|
|
|
|
|
print("\nπ§ͺ Test 5: Bootstrap warming simulation") |
|
static_data = { |
|
"Technology": [ |
|
{"word": "COMPUTER", "clue": "Electronic device"}, |
|
{"word": "ROBOT", "clue": "Automated machine"}, |
|
] |
|
} |
|
await cache_manager.warm_cache_from_static(static_data) |
|
|
|
tech_words = await cache_manager.get_cached_words("Technology", "medium", 5) |
|
print(f"π₯ Bootstrap warming: Retrieved {len(tech_words)} tech words") |
|
|
|
print("\nβ
All cache system tests completed!") |
|
return True |
|
|
|
except Exception as e: |
|
print(f"\nβ Cache system test failed: {e}") |
|
import traceback |
|
traceback.print_exc() |
|
return False |
|
|
|
finally: |
|
|
|
shutil.rmtree(temp_dir) |
|
print(f"π§Ή Cleaned up temporary directory") |
|
|
|
async def test_vector_integration(): |
|
"""Test integration with vector search service.""" |
|
print("\nπ Testing Vector Search Integration\n") |
|
|
|
try: |
|
from src.services.vector_search import VectorSearchService |
|
|
|
|
|
vector_service = VectorSearchService() |
|
|
|
|
|
print("π§ͺ Testing cache fallback when vector search not initialized") |
|
fallback_words = await vector_service._get_cached_fallback("Animals", "medium", 5) |
|
print(f"π¦ Fallback returned {len(fallback_words)} words") |
|
|
|
print("β
Vector integration test completed!") |
|
return True |
|
|
|
except Exception as e: |
|
print(f"β Vector integration test failed: {e}") |
|
import traceback |
|
traceback.print_exc() |
|
return False |
|
|
|
async def main(): |
|
"""Run all tests.""" |
|
print("π Testing Cache System Replacement\n") |
|
|
|
cache_test = await test_cache_system() |
|
integration_test = await test_vector_integration() |
|
|
|
if cache_test and integration_test: |
|
print("\nπ All tests passed! Cache system is working correctly.") |
|
print("π¦ Static word dependencies have been successfully replaced with caching.") |
|
else: |
|
print("\nβ Some tests failed. Check the output above.") |
|
|
|
if __name__ == "__main__": |
|
asyncio.run(main()) |