|
| 1 | +"""Tests for SearchIndex serialization helpers.""" |
| 2 | +import tempfile |
| 3 | +from pathlib import Path |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from redisvl.index import AsyncSearchIndex, SearchIndex |
| 8 | +from redisvl.schema import IndexSchema |
| 9 | + |
| 10 | + |
| 11 | +@pytest.fixture |
| 12 | +def sample_schema(): |
| 13 | + """Create a sample schema for testing.""" |
| 14 | + return IndexSchema.from_dict({ |
| 15 | + "index": { |
| 16 | + "name": "test_index", |
| 17 | + "prefix": "test:", |
| 18 | + "storage_type": "hash", |
| 19 | + }, |
| 20 | + "fields": [ |
| 21 | + {"name": "text", "type": "text"}, |
| 22 | + {"name": "vector", "type": "vector", "attrs": {"dims": 128, "algorithm": "flat"}}, |
| 23 | + ] |
| 24 | + }) |
| 25 | + |
| 26 | + |
| 27 | +class TestSearchIndexSerialization: |
| 28 | + """Tests for SearchIndex serialization methods.""" |
| 29 | + |
| 30 | + def test_to_dict_without_connection(self, sample_schema): |
| 31 | + """Test to_dict() excludes connection info by default.""" |
| 32 | + index = SearchIndex( |
| 33 | + schema=sample_schema, |
| 34 | + redis_url="redis://localhost:6379", |
| 35 | + ) |
| 36 | + |
| 37 | + config = index.to_dict() |
| 38 | + |
| 39 | + assert "index" in config |
| 40 | + assert config["index"]["name"] == "test_index" |
| 41 | + assert "_redis_url" not in config |
| 42 | + assert "_connection_kwargs" not in config |
| 43 | + |
| 44 | + def test_to_dict_with_connection(self, sample_schema): |
| 45 | + """Test to_dict() includes connection info when requested.""" |
| 46 | + index = SearchIndex( |
| 47 | + schema=sample_schema, |
| 48 | + redis_url="redis://localhost:6379", |
| 49 | + connection_kwargs={"ssl": True, "socket_timeout": 30, "password": "secret"}, |
| 50 | + ) |
| 51 | + |
| 52 | + config = index.to_dict(include_connection=True) |
| 53 | + |
| 54 | + assert "_redis_url" in config |
| 55 | + assert config["_redis_url"] == "redis://localhost:6379" |
| 56 | + # Password should not be included (not in safe_keys) |
| 57 | + assert "password" not in config.get("_connection_kwargs", {}) |
| 58 | + # Safe keys should be included |
| 59 | + assert config["_connection_kwargs"]["ssl"] is True |
| 60 | + assert config["_connection_kwargs"]["socket_timeout"] == 30 |
| 61 | + |
| 62 | + def test_to_yaml_without_connection(self, sample_schema): |
| 63 | + """Test to_yaml() writes schema to YAML file.""" |
| 64 | + index = SearchIndex(schema=sample_schema) |
| 65 | + |
| 66 | + with tempfile.NamedTemporaryFile(mode="w", suffix=".yaml", delete=False) as f: |
| 67 | + path = f.name |
| 68 | + |
| 69 | + try: |
| 70 | + index.to_yaml(path) |
| 71 | + |
| 72 | + content = Path(path).read_text() |
| 73 | + assert "test_index" in content |
| 74 | + assert "text" in content |
| 75 | + assert "vector" in content |
| 76 | + finally: |
| 77 | + Path(path).unlink() |
| 78 | + |
| 79 | + def test_to_yaml_with_connection(self, sample_schema): |
| 80 | + """Test to_yaml() includes connection when requested.""" |
| 81 | + index = SearchIndex( |
| 82 | + schema=sample_schema, |
| 83 | + redis_url="redis://localhost:6379", |
| 84 | + ) |
| 85 | + |
| 86 | + with tempfile.NamedTemporaryFile(mode="w", suffix=".yaml", delete=False) as f: |
| 87 | + path = f.name |
| 88 | + |
| 89 | + try: |
| 90 | + index.to_yaml(path, include_connection=True) |
| 91 | + |
| 92 | + content = Path(path).read_text() |
| 93 | + assert "_redis_url" in content |
| 94 | + finally: |
| 95 | + Path(path).unlink() |
| 96 | + |
| 97 | + def test_roundtrip_dict(self, sample_schema): |
| 98 | + """Test that to_dict() and from_dict() roundtrip correctly.""" |
| 99 | + original_index = SearchIndex( |
| 100 | + schema=sample_schema, |
| 101 | + connection_kwargs={"ssl": True}, |
| 102 | + ) |
| 103 | + |
| 104 | + config = original_index.to_dict() |
| 105 | + restored_index = SearchIndex.from_dict(config) |
| 106 | + |
| 107 | + assert restored_index.schema.index.name == original_index.schema.index.name |
| 108 | + assert restored_index.schema.index.prefix == original_index.schema.index.prefix |
| 109 | + |
| 110 | + |
| 111 | +class TestAsyncSearchIndexSerialization: |
| 112 | + """Tests for AsyncSearchIndex serialization methods.""" |
| 113 | + |
| 114 | + def test_to_dict_without_connection(self, sample_schema): |
| 115 | + """Test to_dict() excludes connection info by default.""" |
| 116 | + index = AsyncSearchIndex( |
| 117 | + schema=sample_schema, |
| 118 | + redis_url="redis://localhost:6379", |
| 119 | + ) |
| 120 | + |
| 121 | + config = index.to_dict() |
| 122 | + |
| 123 | + assert "index" in config |
| 124 | + assert config["index"]["name"] == "test_index" |
| 125 | + assert "_redis_url" not in config |
| 126 | + |
| 127 | + def test_to_dict_with_connection(self, sample_schema): |
| 128 | + """Test to_dict() includes connection info when requested.""" |
| 129 | + index = AsyncSearchIndex( |
| 130 | + schema=sample_schema, |
| 131 | + redis_url="redis://localhost:6379", |
| 132 | + connection_kwargs={"ssl": True, "password": "secret"}, |
| 133 | + ) |
| 134 | + |
| 135 | + config = index.to_dict(include_connection=True) |
| 136 | + |
| 137 | + assert "_redis_url" in config |
| 138 | + # Password should not be included (not in safe_keys) |
| 139 | + assert "password" not in config.get("_connection_kwargs", {}) |
| 140 | + |
| 141 | + def test_to_yaml(self, sample_schema): |
| 142 | + """Test to_yaml() writes schema to YAML file.""" |
| 143 | + index = AsyncSearchIndex(schema=sample_schema) |
| 144 | + |
| 145 | + with tempfile.NamedTemporaryFile(mode="w", suffix=".yaml", delete=False) as f: |
| 146 | + path = f.name |
| 147 | + |
| 148 | + try: |
| 149 | + index.to_yaml(path) |
| 150 | + |
| 151 | + content = Path(path).read_text() |
| 152 | + assert "test_index" in content |
| 153 | + finally: |
| 154 | + Path(path).unlink() |
| 155 | + |
| 156 | + def test_roundtrip_dict(self, sample_schema): |
| 157 | + """Test that to_dict() and from_dict() roundtrip correctly.""" |
| 158 | + original_index = AsyncSearchIndex( |
| 159 | + schema=sample_schema, |
| 160 | + connection_kwargs={"ssl": True}, |
| 161 | + ) |
| 162 | + |
| 163 | + config = original_index.to_dict() |
| 164 | + restored_index = AsyncSearchIndex.from_dict(config) |
| 165 | + |
| 166 | + assert restored_index.schema.index.name == original_index.schema.index.name |
0 commit comments