|
| 1 | +# SPDX-FileCopyrightText: 2025-present deepset GmbH <info@deepset.ai> |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: Apache-2.0 |
| 4 | + |
| 5 | +from unittest.mock import MagicMock, patch |
| 6 | + |
| 7 | +import pytest |
| 8 | + |
| 9 | +from deepset_mcp.store import create_redis_backend, initialize_store |
| 10 | +from deepset_mcp.tools.tokonomics.object_store import InMemoryBackend, ObjectStore |
| 11 | + |
| 12 | + |
| 13 | +class TestStoreInitialization: |
| 14 | + """Test store initialization functions.""" |
| 15 | + |
| 16 | + def test_initialize_store_memory_backend(self) -> None: |
| 17 | + """Test initializing store with memory backend.""" |
| 18 | + store = initialize_store(backend="memory", ttl=1800) |
| 19 | + |
| 20 | + assert isinstance(store, ObjectStore) |
| 21 | + assert isinstance(store._backend, InMemoryBackend) |
| 22 | + assert store._ttl == 1800 |
| 23 | + |
| 24 | + def test_initialize_store_default_backend(self) -> None: |
| 25 | + """Test initializing store with default backend.""" |
| 26 | + store = initialize_store() |
| 27 | + |
| 28 | + assert isinstance(store, ObjectStore) |
| 29 | + assert isinstance(store._backend, InMemoryBackend) |
| 30 | + assert store._ttl == 600 |
| 31 | + |
| 32 | + def test_initialize_store_redis_backend_success(self) -> None: |
| 33 | + """Test initializing store with Redis backend successfully.""" |
| 34 | + mock_redis_client = MagicMock() |
| 35 | + mock_redis_client.ping.return_value = True |
| 36 | + |
| 37 | + with patch("redis.from_url", return_value=mock_redis_client): |
| 38 | + store = initialize_store(backend="redis", redis_url="redis://localhost:6379", ttl=7200) |
| 39 | + |
| 40 | + assert isinstance(store, ObjectStore) |
| 41 | + assert store._ttl == 7200 |
| 42 | + mock_redis_client.ping.assert_called_once() |
| 43 | + |
| 44 | + def test_initialize_store_redis_backend_no_url(self) -> None: |
| 45 | + """Test initializing store with Redis backend but no URL.""" |
| 46 | + with pytest.raises(ValueError, match="redis_url.*is None"): |
| 47 | + initialize_store(backend="redis", redis_url=None) |
| 48 | + |
| 49 | + def test_initialize_store_redis_backend_connection_failure(self) -> None: |
| 50 | + """Test initializing store with Redis backend connection failure.""" |
| 51 | + mock_redis_client = MagicMock() |
| 52 | + mock_redis_client.ping.side_effect = Exception("Connection failed") |
| 53 | + |
| 54 | + with patch("redis.from_url", return_value=mock_redis_client): |
| 55 | + with pytest.raises(Exception, match="Connection failed"): |
| 56 | + initialize_store(backend="redis", redis_url="redis://localhost:6379") |
| 57 | + |
| 58 | + def test_create_redis_backend_success(self) -> None: |
| 59 | + """Test creating Redis backend successfully.""" |
| 60 | + mock_redis_client = MagicMock() |
| 61 | + mock_redis_client.ping.return_value = True |
| 62 | + |
| 63 | + with patch("redis.from_url", return_value=mock_redis_client): |
| 64 | + backend = create_redis_backend("redis://localhost:6379") |
| 65 | + |
| 66 | + assert backend is not None |
| 67 | + mock_redis_client.ping.assert_called_once() |
| 68 | + |
| 69 | + def test_create_redis_backend_redis_not_installed(self) -> None: |
| 70 | + """Test creating Redis backend when redis package is not installed.""" |
| 71 | + with patch("builtins.__import__", side_effect=ImportError("No module named 'redis'")): |
| 72 | + with pytest.raises(ImportError, match="Redis package not installed"): |
| 73 | + create_redis_backend("redis://localhost:6379") |
| 74 | + |
| 75 | + def test_create_redis_backend_connection_failure(self) -> None: |
| 76 | + """Test creating Redis backend with connection failure.""" |
| 77 | + mock_redis = MagicMock() |
| 78 | + mock_redis.from_url.return_value = mock_redis |
| 79 | + mock_redis.ping.side_effect = Exception("Connection failed") |
| 80 | + |
| 81 | + with patch("redis.from_url", return_value=mock_redis): |
| 82 | + with pytest.raises(Exception, match="Connection failed"): |
| 83 | + create_redis_backend("redis://localhost:6379") |
| 84 | + |
| 85 | + def test_initialize_store_caching(self) -> None: |
| 86 | + """Test that initialize_store uses caching.""" |
| 87 | + # Clear any existing cache |
| 88 | + initialize_store.cache_clear() |
| 89 | + |
| 90 | + store1 = initialize_store(backend="memory", ttl=1800) |
| 91 | + store2 = initialize_store(backend="memory", ttl=1800) |
| 92 | + |
| 93 | + # Should return the same instance due to caching |
| 94 | + assert store1 is store2 |
| 95 | + |
| 96 | + def test_initialize_store_different_params_different_instances(self) -> None: |
| 97 | + """Test that different parameters create different instances.""" |
| 98 | + # Clear any existing cache |
| 99 | + initialize_store.cache_clear() |
| 100 | + |
| 101 | + store1 = initialize_store(backend="memory", ttl=1800) |
| 102 | + store2 = initialize_store(backend="memory", ttl=3600) |
| 103 | + |
| 104 | + # Should return different instances due to different parameters |
| 105 | + assert store1 is not store2 |
| 106 | + assert store1._ttl == 1800 |
| 107 | + assert store2._ttl == 3600 |
| 108 | + |
| 109 | + def test_initialize_store_unknown_backend(self) -> None: |
| 110 | + """Test initializing store with unknown backend defaults to memory.""" |
| 111 | + store = initialize_store(backend="unknown", ttl=1800) |
| 112 | + |
| 113 | + assert isinstance(store, ObjectStore) |
| 114 | + assert isinstance(store._backend, InMemoryBackend) |
| 115 | + assert store._ttl == 1800 |
0 commit comments