Skip to main content
Glama
test_server.py4.06 kB
"""Tests for the storage backend.""" import json import os import tempfile from pathlib import Path import pytest from store_mcp.storage import Storage @pytest.fixture def temp_storage(): """Create a temporary storage instance for testing.""" with tempfile.TemporaryDirectory() as tmpdir: storage_path = os.path.join(tmpdir, "test_data.json") storage = Storage(storage_path) yield storage def test_store_and_retrieve(temp_storage): """Test storing and retrieving data.""" temp_storage.store("test_key", "test_value") assert temp_storage.retrieve("test_key") == "test_value" def test_store_complex_data(temp_storage): """Test storing complex JSON data.""" data = { "name": "John", "age": 30, "hobbies": ["reading", "coding"], "address": { "city": "San Francisco", "zip": "94102" } } temp_storage.store("user_data", data) retrieved = temp_storage.retrieve("user_data") assert retrieved == data def test_retrieve_nonexistent_key(temp_storage): """Test retrieving a key that doesn't exist.""" assert temp_storage.retrieve("nonexistent") is None def test_delete(temp_storage): """Test deleting data.""" temp_storage.store("to_delete", "value") assert temp_storage.delete("to_delete") is True assert temp_storage.retrieve("to_delete") is None assert temp_storage.delete("to_delete") is False # Already deleted def test_list_keys(temp_storage): """Test listing all keys.""" temp_storage.store("key1", "value1") temp_storage.store("key2", "value2") temp_storage.store("key3", "value3") keys = temp_storage.list_keys() assert set(keys) == {"key1", "key2", "key3"} def test_list_keys_empty(temp_storage): """Test listing keys when storage is empty.""" assert temp_storage.list_keys() == [] def test_search_by_key(temp_storage): """Test searching by key pattern.""" temp_storage.store("user_john", {"name": "John"}) temp_storage.store("user_jane", {"name": "Jane"}) temp_storage.store("product_apple", {"name": "Apple"}) results = temp_storage.search("user") assert len(results) == 2 assert "user_john" in results assert "user_jane" in results def test_search_by_value(temp_storage): """Test searching by value content.""" temp_storage.store("item1", {"color": "red", "size": "large"}) temp_storage.store("item2", {"color": "blue", "size": "small"}) temp_storage.store("item3", {"color": "red", "size": "medium"}) results = temp_storage.search("red") assert len(results) == 2 assert "item1" in results assert "item3" in results def test_search_case_insensitive(temp_storage): """Test that search is case-insensitive.""" temp_storage.store("TestKey", "TestValue") results = temp_storage.search("testkey") assert len(results) == 1 assert "TestKey" in results results = temp_storage.search("testvalue") assert len(results) == 1 def test_search_no_results(temp_storage): """Test searching with no matches.""" temp_storage.store("key1", "value1") results = temp_storage.search("nonexistent") assert results == {} def test_clear(temp_storage): """Test clearing all data.""" temp_storage.store("key1", "value1") temp_storage.store("key2", "value2") temp_storage.clear() assert temp_storage.list_keys() == [] def test_persistence(temp_storage): """Test that data persists across storage instances.""" storage_path = temp_storage.storage_path # Store data temp_storage.store("persistent_key", "persistent_value") # Create new storage instance with same path new_storage = Storage(str(storage_path)) assert new_storage.retrieve("persistent_key") == "persistent_value" def test_overwrite_key(temp_storage): """Test overwriting an existing key.""" temp_storage.store("key", "old_value") temp_storage.store("key", "new_value") assert temp_storage.retrieve("key") == "new_value"

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/mariano-cecowski/store_mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server