diff --git a/tests/test_vicinity.py b/tests/test_vicinity.py index 1a24ac2..bbd8a39 100644 --- a/tests/test_vicinity.py +++ b/tests/test_vicinity.py @@ -167,6 +167,25 @@ def test_vicinity_save_and_load_vector_store(tmp_path: Path, vicinity_instance_w assert v.vector_store is not None +def test_vicinity_overwrite_without_vector_store(tmp_path: Path) -> None: + """Overwriting a stored-vector snapshot removes the previous vector store.""" + save_path = tmp_path / "vicinity_data" + original = Vicinity.from_vectors_and_items(np.array([[1.0, 0.0]]), ["old"], store_vectors=True) + original.save(save_path) + + vectors = np.array([[0.0, 1.0]]) + replacement = Vicinity.from_vectors_and_items(vectors, ["new"], store_vectors=False) + replacement.save(save_path, overwrite=True) + + loaded = Vicinity.load(save_path) + assert loaded.items == ["new"] + assert loaded.query(vectors, k=1) == replacement.query(vectors, k=1) + assert loaded.vector_store is None + assert not (save_path / "store").exists() + with pytest.raises(ValueError, match="No vector store was provided"): + loaded.get_vector_by_index(0) + + def test_vicinity_save_and_load_non_serializable_items( tmp_path: Path, non_serializable_items: list[str], vectors: np.ndarray ) -> None: diff --git a/vicinity/vicinity.py b/vicinity/vicinity.py index 6c1fd9e..de16d1b 100644 --- a/vicinity/vicinity.py +++ b/vicinity/vicinity.py @@ -3,6 +3,7 @@ from __future__ import annotations import logging +import shutil from collections.abc import Iterable, Sequence from pathlib import Path from time import perf_counter @@ -196,10 +197,12 @@ def save( raise JSONEncodeError(f"Items could not be encoded to JSON because they are not serializable: {e}") self.backend.save(path) + store_path = path / "store" if self.vector_store is not None: - store_path = path / "store" store_path.mkdir(exist_ok=overwrite) self.vector_store.save(store_path) + elif store_path.exists(): + shutil.rmtree(store_path) @classmethod def load(cls, filename: PathLike) -> Vicinity[Any]: