82 lines
2.3 KiB
Python
82 lines
2.3 KiB
Python
import sys
|
|
import os
|
|
|
|
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
|
|
|
import asyncio
|
|
import tempfile
|
|
import os
|
|
from context.sketch_pad import RedisFileSketchPadBackend
|
|
from config.config import get_config
|
|
|
|
|
|
def _create_sketch_pad(sketch_pad_id: str, file_path: str) -> RedisFileSketchPadBackend:
|
|
config = get_config()
|
|
return RedisFileSketchPadBackend(
|
|
sketch_pad_id=sketch_pad_id,
|
|
redis_host=config.REDIS_HOST,
|
|
redis_port=int(config.REDIS_PORT),
|
|
redis_db=int(config.REDIS_DB),
|
|
file_path=file_path,
|
|
)
|
|
|
|
|
|
async def test_basic_operations():
|
|
"""Test basic operations."""
|
|
print("=== SketchPad Basic Operation Test ===")
|
|
|
|
# Create a temporary file.
|
|
with tempfile.NamedTemporaryFile(mode="w", suffix=".json", delete=False) as f:
|
|
temp_file = f.name
|
|
|
|
try:
|
|
# Initialize SketchPad.
|
|
sketch_pad = _create_sketch_pad("test_pad", temp_file)
|
|
|
|
# Set items.
|
|
print("1. Setting items...")
|
|
await sketch_pad.set_item(
|
|
key="user_config",
|
|
value={"theme": "dark", "lang": "en"},
|
|
summary="User configuration",
|
|
tags={"config", "user"},
|
|
)
|
|
|
|
await sketch_pad.set_item(
|
|
key="temp_data", value="temporary data", summary="temporary storage", tags={"temp"}, ttl=5
|
|
)
|
|
|
|
# Get items.
|
|
print("2. Getting items...")
|
|
item = sketch_pad.get_item("user_config")
|
|
print(f" user_config: {item.value if item else 'Not found'}")
|
|
|
|
# Check existence.
|
|
print("3. Checking existence...")
|
|
print(f" user_config exists: {sketch_pad.exists('user_config')}")
|
|
|
|
# Get all keys.
|
|
print("4. Getting all keys...")
|
|
keys = sketch_pad.keys()
|
|
print(f" All keys: {keys}")
|
|
|
|
# Search by tag.
|
|
print("5. Searching by tag...")
|
|
config_items = sketch_pad.search_by_tags({"config"})
|
|
print(f" Configuration item count: {len(config_items)}")
|
|
|
|
# Statistics.
|
|
print("6. Statistics...")
|
|
stats = sketch_pad.get_statistics()
|
|
print(f" Total item count: {stats.total_items}")
|
|
|
|
print("=== Test Complete ===")
|
|
|
|
finally:
|
|
if os.path.exists(temp_file):
|
|
os.unlink(temp_file)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
asyncio.run(test_basic_operations())
|