|
1 | 1 | """Integration tests for SQLiteVecClient.""" |
2 | 2 |
|
| 3 | +import sqlite3 |
| 4 | + |
3 | 5 | import pytest |
4 | 6 |
|
5 | 7 | from sqlite_vec_client import ( |
@@ -94,6 +96,155 @@ def test_add_invalid_embedding_dimension( |
94 | 96 | client_with_table.add(texts=sample_texts, embeddings=invalid_embeddings) |
95 | 97 |
|
96 | 98 |
|
| 99 | +@pytest.mark.integration |
| 100 | +class TestUniqueText: |
| 101 | + """Tests for unique_text constraint and on_conflict parameter.""" |
| 102 | + |
| 103 | + def test_unique_text_rejects_duplicates_by_default( |
| 104 | + self, client_with_unique_table, sample_embeddings |
| 105 | + ): |
| 106 | + """Duplicate text raises IntegrityError when on_conflict='error'.""" |
| 107 | + client_with_unique_table.add( |
| 108 | + texts=["hello"], embeddings=[sample_embeddings[0]] |
| 109 | + ) |
| 110 | + with pytest.raises(sqlite3.IntegrityError): |
| 111 | + client_with_unique_table.add( |
| 112 | + texts=["hello"], embeddings=[sample_embeddings[1]] |
| 113 | + ) |
| 114 | + |
| 115 | + def test_on_conflict_ignore_skips_duplicates( |
| 116 | + self, client_with_unique_table, sample_embeddings |
| 117 | + ): |
| 118 | + """Duplicate texts are silently skipped with on_conflict='ignore'.""" |
| 119 | + client_with_unique_table.add( |
| 120 | + texts=["hello"], embeddings=[sample_embeddings[0]] |
| 121 | + ) |
| 122 | + rowids = client_with_unique_table.add( |
| 123 | + texts=["hello", "world"], |
| 124 | + embeddings=[sample_embeddings[1], sample_embeddings[2]], |
| 125 | + on_conflict="ignore", |
| 126 | + ) |
| 127 | + assert len(rowids) == 1 |
| 128 | + assert client_with_unique_table.count() == 2 |
| 129 | + |
| 130 | + def test_on_conflict_ignore_all_duplicates( |
| 131 | + self, client_with_unique_table, sample_embeddings |
| 132 | + ): |
| 133 | + """All duplicates skipped returns empty rowids.""" |
| 134 | + client_with_unique_table.add( |
| 135 | + texts=["hello"], embeddings=[sample_embeddings[0]] |
| 136 | + ) |
| 137 | + rowids = client_with_unique_table.add( |
| 138 | + texts=["hello"], |
| 139 | + embeddings=[sample_embeddings[1]], |
| 140 | + on_conflict="ignore", |
| 141 | + ) |
| 142 | + assert rowids == [] |
| 143 | + assert client_with_unique_table.count() == 1 |
| 144 | + |
| 145 | + def test_on_conflict_ignore_preserves_original( |
| 146 | + self, client_with_unique_table, sample_embeddings |
| 147 | + ): |
| 148 | + """Ignored duplicates do not overwrite the original record.""" |
| 149 | + client_with_unique_table.add( |
| 150 | + texts=["hello"], |
| 151 | + embeddings=[sample_embeddings[0]], |
| 152 | + metadata=[{"version": 1}], |
| 153 | + ) |
| 154 | + client_with_unique_table.add( |
| 155 | + texts=["hello"], |
| 156 | + embeddings=[sample_embeddings[1]], |
| 157 | + metadata=[{"version": 2}], |
| 158 | + on_conflict="ignore", |
| 159 | + ) |
| 160 | + record = client_with_unique_table.get(1) |
| 161 | + assert record[2] == {"version": 1} |
| 162 | + |
| 163 | + def test_on_conflict_replace_updates_existing( |
| 164 | + self, client_with_unique_table, sample_embeddings |
| 165 | + ): |
| 166 | + """Duplicate texts are updated with on_conflict='replace'.""" |
| 167 | + client_with_unique_table.add( |
| 168 | + texts=["hello"], |
| 169 | + embeddings=[sample_embeddings[0]], |
| 170 | + metadata=[{"version": 1}], |
| 171 | + ) |
| 172 | + rowids = client_with_unique_table.add( |
| 173 | + texts=["hello"], |
| 174 | + embeddings=[sample_embeddings[1]], |
| 175 | + metadata=[{"version": 2}], |
| 176 | + on_conflict="replace", |
| 177 | + ) |
| 178 | + assert len(rowids) == 1 |
| 179 | + assert client_with_unique_table.count() == 1 |
| 180 | + record = client_with_unique_table.get(rowids[0]) |
| 181 | + assert record[2] == {"version": 2} |
| 182 | + assert record[3] == pytest.approx(sample_embeddings[1], abs=1e-6) |
| 183 | + |
| 184 | + def test_on_conflict_replace_mixed_insert_and_update( |
| 185 | + self, client_with_unique_table, sample_embeddings |
| 186 | + ): |
| 187 | + """Replace mode handles a mix of new and existing texts.""" |
| 188 | + client_with_unique_table.add( |
| 189 | + texts=["hello"], embeddings=[sample_embeddings[0]] |
| 190 | + ) |
| 191 | + rowids = client_with_unique_table.add( |
| 192 | + texts=["hello", "world"], |
| 193 | + embeddings=[sample_embeddings[1], sample_embeddings[2]], |
| 194 | + on_conflict="replace", |
| 195 | + ) |
| 196 | + assert len(rowids) == 2 |
| 197 | + assert client_with_unique_table.count() == 2 |
| 198 | + |
| 199 | + def test_on_conflict_replace_keeps_rowid( |
| 200 | + self, client_with_unique_table, sample_embeddings |
| 201 | + ): |
| 202 | + """Replace mode preserves the original rowid.""" |
| 203 | + original_rowids = client_with_unique_table.add( |
| 204 | + texts=["hello"], embeddings=[sample_embeddings[0]] |
| 205 | + ) |
| 206 | + new_rowids = client_with_unique_table.add( |
| 207 | + texts=["hello"], |
| 208 | + embeddings=[sample_embeddings[1]], |
| 209 | + on_conflict="replace", |
| 210 | + ) |
| 211 | + assert new_rowids == original_rowids |
| 212 | + |
| 213 | + def test_on_conflict_replace_vec_table_synced( |
| 214 | + self, client_with_unique_table, sample_embeddings |
| 215 | + ): |
| 216 | + """Replace mode keeps the vector table in sync for similarity search.""" |
| 217 | + client_with_unique_table.add( |
| 218 | + texts=["hello"], embeddings=[sample_embeddings[0]] |
| 219 | + ) |
| 220 | + client_with_unique_table.add( |
| 221 | + texts=["hello"], |
| 222 | + embeddings=[sample_embeddings[1]], |
| 223 | + on_conflict="replace", |
| 224 | + ) |
| 225 | + results = client_with_unique_table.similarity_search( |
| 226 | + embedding=sample_embeddings[1], top_k=1 |
| 227 | + ) |
| 228 | + assert results[0][1] == "hello" |
| 229 | + |
| 230 | + def test_on_conflict_invalid_value(self, client_with_unique_table): |
| 231 | + """Invalid on_conflict value raises ValidationError.""" |
| 232 | + with pytest.raises(ValidationError): |
| 233 | + client_with_unique_table.add( |
| 234 | + texts=["hello"], |
| 235 | + embeddings=[[0.1, 0.2, 0.3]], |
| 236 | + on_conflict="bad", |
| 237 | + ) |
| 238 | + |
| 239 | + def test_without_unique_text_allows_duplicates( |
| 240 | + self, client_with_table, sample_embeddings |
| 241 | + ): |
| 242 | + """Without unique_text, duplicate texts are allowed.""" |
| 243 | + client_with_table.add(texts=["hello"], embeddings=[sample_embeddings[0]]) |
| 244 | + client_with_table.add(texts=["hello"], embeddings=[sample_embeddings[1]]) |
| 245 | + assert client_with_table.count() == 2 |
| 246 | + |
| 247 | + |
97 | 248 | @pytest.mark.integration |
98 | 249 | class TestSimilaritySearch: |
99 | 250 | """Tests for similarity_search method.""" |
|
0 commit comments