|
| 1 | +"""Unit tests for __repr__ implementations on core RedisVL classes.""" |
| 2 | + |
| 3 | +from unittest.mock import MagicMock, patch |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from redisvl.index import AsyncSearchIndex, SearchIndex |
| 8 | +from redisvl.schema import IndexSchema |
| 9 | + |
| 10 | +# --------------------------------------------------------------------------- |
| 11 | +# Helpers |
| 12 | +# --------------------------------------------------------------------------- |
| 13 | + |
| 14 | + |
| 15 | +def _make_schema(name="my-index", prefix="rvl", storage_type="hash"): |
| 16 | + return IndexSchema.from_dict( |
| 17 | + {"index": {"name": name, "prefix": prefix, "storage_type": storage_type}} |
| 18 | + ) |
| 19 | + |
| 20 | + |
| 21 | +def _make_mock_vectorizer(dims=768, dtype="float32"): |
| 22 | + mock_vec = MagicMock() |
| 23 | + mock_vec.dims = dims |
| 24 | + mock_vec.dtype = dtype |
| 25 | + return mock_vec |
| 26 | + |
| 27 | + |
| 28 | +# --------------------------------------------------------------------------- |
| 29 | +# SearchIndex / AsyncSearchIndex |
| 30 | +# --------------------------------------------------------------------------- |
| 31 | + |
| 32 | + |
| 33 | +def test_search_index_repr_hash(): |
| 34 | + schema = _make_schema(name="test-index", prefix="rvl", storage_type="hash") |
| 35 | + index = SearchIndex(schema=schema) |
| 36 | + assert ( |
| 37 | + repr(index) |
| 38 | + == "SearchIndex(name='test-index', prefixes=['rvl'], storage_type='hash')" |
| 39 | + ) |
| 40 | + |
| 41 | + |
| 42 | +def test_search_index_repr_json(): |
| 43 | + schema = _make_schema(name="docs", prefix="doc", storage_type="json") |
| 44 | + index = SearchIndex(schema=schema) |
| 45 | + assert ( |
| 46 | + repr(index) == "SearchIndex(name='docs', prefixes=['doc'], storage_type='json')" |
| 47 | + ) |
| 48 | + |
| 49 | + |
| 50 | +def test_async_search_index_repr(): |
| 51 | + schema = _make_schema(name="async-idx", prefix="data", storage_type="json") |
| 52 | + index = AsyncSearchIndex(schema=schema) |
| 53 | + assert ( |
| 54 | + repr(index) |
| 55 | + == "AsyncSearchIndex(name='async-idx', prefixes=['data'], storage_type='json')" |
| 56 | + ) |
| 57 | + |
| 58 | + |
| 59 | +# --------------------------------------------------------------------------- |
| 60 | +# SemanticCache |
| 61 | +# --------------------------------------------------------------------------- |
| 62 | + |
| 63 | + |
| 64 | +@pytest.fixture() |
| 65 | +def patched_semantic_cache(): |
| 66 | + """Patch out Redis and HFTextVectorizer so SemanticCache can be instantiated |
| 67 | + without a running Redis server or ML model download.""" |
| 68 | + mock_vec = _make_mock_vectorizer() |
| 69 | + mock_idx = MagicMock() |
| 70 | + mock_idx.exists.return_value = False |
| 71 | + |
| 72 | + with patch( |
| 73 | + "redisvl.extensions.cache.llm.semantic.HFTextVectorizer", |
| 74 | + return_value=mock_vec, |
| 75 | + ): |
| 76 | + with patch( |
| 77 | + "redisvl.extensions.cache.llm.semantic.SearchIndex", |
| 78 | + return_value=mock_idx, |
| 79 | + ): |
| 80 | + yield |
| 81 | + |
| 82 | + |
| 83 | +def test_semantic_cache_repr_defaults(patched_semantic_cache): |
| 84 | + from redisvl.extensions.cache.llm.semantic import SemanticCache |
| 85 | + |
| 86 | + cache = SemanticCache(name="llmcache", distance_threshold=0.1) |
| 87 | + assert ( |
| 88 | + repr(cache) |
| 89 | + == "SemanticCache(name='llmcache', distance_threshold=0.1, ttl=None)" |
| 90 | + ) |
| 91 | + |
| 92 | + |
| 93 | +def test_semantic_cache_repr_with_ttl(patched_semantic_cache): |
| 94 | + from redisvl.extensions.cache.llm.semantic import SemanticCache |
| 95 | + |
| 96 | + cache = SemanticCache(name="my-cache", distance_threshold=0.2, ttl=300) |
| 97 | + assert ( |
| 98 | + repr(cache) == "SemanticCache(name='my-cache', distance_threshold=0.2, ttl=300)" |
| 99 | + ) |
| 100 | + |
| 101 | + |
| 102 | +# --------------------------------------------------------------------------- |
| 103 | +# SemanticRouter |
| 104 | +# --------------------------------------------------------------------------- |
| 105 | + |
| 106 | + |
| 107 | +def test_semantic_router_repr(): |
| 108 | + from redisvl.extensions.router.schema import Route |
| 109 | + from redisvl.extensions.router.semantic import SemanticRouter |
| 110 | + |
| 111 | + routes = [ |
| 112 | + Route(name="greeting", references=["hello", "hi"]), |
| 113 | + Route(name="farewell", references=["bye", "goodbye"]), |
| 114 | + ] |
| 115 | + # model_construct bypasses __init__ (and therefore Redis/vectorizer setup) |
| 116 | + # while still setting the Pydantic fields that __repr__ reads. |
| 117 | + router = SemanticRouter.model_construct(name="my-router", routes=routes) |
| 118 | + assert repr(router) == "SemanticRouter(name='my-router', routes=2)" |
| 119 | + |
| 120 | + |
| 121 | +def test_semantic_router_repr_single_route(): |
| 122 | + from redisvl.extensions.router.schema import Route |
| 123 | + from redisvl.extensions.router.semantic import SemanticRouter |
| 124 | + |
| 125 | + routes = [Route(name="support", references=["help", "issue"])] |
| 126 | + router = SemanticRouter.model_construct(name="support-router", routes=routes) |
| 127 | + assert repr(router) == "SemanticRouter(name='support-router', routes=1)" |
| 128 | + |
| 129 | + |
| 130 | +# --------------------------------------------------------------------------- |
| 131 | +# MessageHistory |
| 132 | +# --------------------------------------------------------------------------- |
| 133 | + |
| 134 | + |
| 135 | +@pytest.fixture() |
| 136 | +def patched_message_history(): |
| 137 | + """Patch SearchIndex.create so MessageHistory init doesn't need Redis.""" |
| 138 | + with patch("redisvl.extensions.message_history.message_history.SearchIndex.create"): |
| 139 | + yield |
| 140 | + |
| 141 | + |
| 142 | +def test_message_history_repr(patched_message_history): |
| 143 | + from redisvl.extensions.message_history.message_history import MessageHistory |
| 144 | + |
| 145 | + mh = MessageHistory(name="chat", session_tag="abc123") |
| 146 | + assert repr(mh) == "MessageHistory(name='chat', session_tag='abc123')" |
| 147 | + |
| 148 | + |
| 149 | +def test_message_history_repr_custom_name(patched_message_history): |
| 150 | + from redisvl.extensions.message_history.message_history import MessageHistory |
| 151 | + |
| 152 | + mh = MessageHistory(name="support-chat", session_tag="sess-001") |
| 153 | + assert repr(mh) == "MessageHistory(name='support-chat', session_tag='sess-001')" |
| 154 | + |
| 155 | + |
| 156 | +# --------------------------------------------------------------------------- |
| 157 | +# SemanticMessageHistory |
| 158 | +# --------------------------------------------------------------------------- |
| 159 | + |
| 160 | + |
| 161 | +@pytest.fixture() |
| 162 | +def patched_semantic_message_history(): |
| 163 | + """Patch out Redis and HFTextVectorizer for SemanticMessageHistory.""" |
| 164 | + mock_vec = _make_mock_vectorizer(dims=384) |
| 165 | + mock_idx = MagicMock() |
| 166 | + mock_idx.exists.return_value = False |
| 167 | + |
| 168 | + with patch( |
| 169 | + "redisvl.extensions.message_history.semantic_history.HFTextVectorizer", |
| 170 | + return_value=mock_vec, |
| 171 | + ): |
| 172 | + with patch( |
| 173 | + "redisvl.extensions.message_history.semantic_history.SearchIndex", |
| 174 | + return_value=mock_idx, |
| 175 | + ): |
| 176 | + yield |
| 177 | + |
| 178 | + |
| 179 | +def test_semantic_message_history_repr(patched_semantic_message_history): |
| 180 | + from redisvl.extensions.message_history.semantic_history import ( |
| 181 | + SemanticMessageHistory, |
| 182 | + ) |
| 183 | + |
| 184 | + smh = SemanticMessageHistory( |
| 185 | + name="sem-chat", session_tag="sess-42", distance_threshold=0.3 |
| 186 | + ) |
| 187 | + assert ( |
| 188 | + repr(smh) |
| 189 | + == "SemanticMessageHistory(name='sem-chat', session_tag='sess-42', distance_threshold=0.3)" |
| 190 | + ) |
| 191 | + |
| 192 | + |
| 193 | +def test_semantic_message_history_repr_custom_threshold( |
| 194 | + patched_semantic_message_history, |
| 195 | +): |
| 196 | + from redisvl.extensions.message_history.semantic_history import ( |
| 197 | + SemanticMessageHistory, |
| 198 | + ) |
| 199 | + |
| 200 | + smh = SemanticMessageHistory( |
| 201 | + name="history", session_tag="s1", distance_threshold=0.5 |
| 202 | + ) |
| 203 | + assert ( |
| 204 | + repr(smh) |
| 205 | + == "SemanticMessageHistory(name='history', session_tag='s1', distance_threshold=0.5)" |
| 206 | + ) |
0 commit comments