Upload backend/tests/test_faiss_store.py with huggingface_hub
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111import faissimport numpy as npimport pytest
import core.faiss_store as store
@pytest.fixture()def tiny_store(monkeypatch): """A 4-dimensional index with two orthogonal unit vectors.""" index = faiss.IndexFlatL2(4) index.add(np.array([ [1.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0], ], dtype="float32"))
monkeypatch.setattr(store, "_index", index) monkeypatch.setattr(store, "_metadata", { "000001.jpg": ["Male", "Smiling"], "000002.jpg": ["Female", "Young"], }) monkeypatch.setattr(store, "_faiss_order", ["000001.jpg", "000002.jpg"]) return store
def test_squared_l2_to_cosine_endpoints(): assert store.squared_l2_to_cosine(0.0) == pytest.approx(1.0) assert store.squared_l2_to_cosine(2.0) == pytest.approx(0.0) assert store.squared_l2_to_cosine(4.0) == pytest.approx(-1.0) # Orthogonal-ish float noise must stay inside [-1, 1]. assert -1.0 <= store.squared_l2_to_cosine(4.5) <= 1.0
def test_search_returns_cosine_similarity_not_distance(tiny_store): results = tiny_store.search_by_vector( np.array([1.0, 0.0, 0.0, 0.0], dtype="float32"), top_k=2, )
assert [r["image_id"] for r in results] == ["000001.jpg", "000002.jpg"] assert results[0]["similarity"] == pytest.approx(1.0) assert results[1]["similarity"] == pytest.approx(0.0) assert results[0]["attributes"] == ["Male", "Smiling"]
def test_search_normalises_query(tiny_store): results = tiny_store.search_by_vector( np.array([5.0, 0.0, 0.0, 0.0], dtype="float32"), top_k=1, ) assert results[0]["similarity"] == pytest.approx(1.0)
def test_search_rejects_wrong_dimension(tiny_store): with pytest.raises(ValueError, match="dimension"): tiny_store.search_by_vector(np.zeros(3, dtype="float32"), top_k=1)
def test_search_attributes_is_case_insensitive(tiny_store): matches = tiny_store.search_by_attributes(["male", "smiling"], top_k=5) assert len(matches) == 1 assert matches[0]["image_id"] == "000001.jpg" assert matches[0]["faiss_id"] == 0
def test_search_attributes_requires_all(tiny_store): assert tiny_store.search_by_attributes(["Male", "Young"], top_k=5) == []
def test_search_attributes_honours_exclusions(tiny_store): matches = tiny_store.search_by_attributes( ["Young"], top_k=5, excluded_attrs=["Male"], ) assert [m["image_id"] for m in matches] == ["000002.jpg"]
def test_search_attributes_exclusion_alone_filters_everything(tiny_store): matches = tiny_store.search_by_attributes( [], top_k=5, excluded_attrs=["male"], ) assert [m["image_id"] for m in matches] == ["000002.jpg"]
def test_search_attributes_unknown_requirement_returns_empty(tiny_store): assert tiny_store.search_by_attributes(["Does_Not_Exist"], top_k=5) == []
def test_attribute_matrix_is_cached(tiny_store): matrix_one, columns_one = tiny_store.get_attribute_matrix() matrix_two, columns_two = tiny_store.get_attribute_matrix()
assert matrix_one is matrix_two assert columns_one is columns_two
def test_attribute_matrix_rebuilds_when_metadata_is_replaced(tiny_store, monkeypatch): tiny_store.warm_attribute_index()
monkeypatch.setattr(store, "_metadata", { "000001.jpg": ["Young"], "000002.jpg": ["Male"], }) monkeypatch.setattr(store, "_faiss_order", ["000001.jpg", "000002.jpg"])
matches = tiny_store.search_by_attributes(["Young"], top_k=5)
assert [m["image_id"] for m in matches] == ["000001.jpg"]backend/tests/test_faiss_store.pyAdded+111