From 68cc646a3e1f455a9a95d528e973a6a2588eb4c0 Mon Sep 17 00:00:00 2001 From: Alex Date: Fri, 20 Sep 2024 12:07:17 +0100 Subject: [PATCH] fix(test): better test + cov --- .github/workflows/pytest.yml | 2 +- tests/test_vector_store.py | 30 ++++++++++++++++++++++++++---- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/.github/workflows/pytest.yml b/.github/workflows/pytest.yml index 9ee2d14d..c6615e56 100644 --- a/.github/workflows/pytest.yml +++ b/.github/workflows/pytest.yml @@ -21,7 +21,7 @@ jobs: if [ -f requirements.txt ]; then pip install -r requirements.txt; fi - name: Test with pytest and generate coverage report run: | - python -m pytest --cov=application --cov=scripts --cov=extensions --cov-report=xml + python -m pytest --cov=application --cov-report=xml - name: Upload coverage reports to Codecov if: github.event_name == 'pull_request' && matrix.python-version == '3.11' uses: codecov/codecov-action@v3 diff --git a/tests/test_vector_store.py b/tests/test_vector_store.py index 6b700dc5..83654b7e 100644 --- a/tests/test_vector_store.py +++ b/tests/test_vector_store.py @@ -9,11 +9,33 @@ from application.core.settings import settings def test_init_local_faiss_store_huggingface(): """ - Test that asserts that trying to initialize a FaissStore with + Test that asserts that initializing a FaissStore with the huggingface sentence transformer below together with the index.faiss file in the application/ folder results in a dimension mismatch error. """ - settings.EMBEDDINGS_NAME = "openai_text-embedding-ada-002" - with pytest.raises(ValueError): - FaissStore("application/", "", None) + import os + from langchain.embeddings import HuggingFaceEmbeddings + from langchain.docstore.document import Document + from langchain_community.vectorstores import FAISS + + # Ensure application directory exists + index_path = os.path.join("application") + os.makedirs(index_path, exist_ok=True) + + # Create an index.faiss with a different embeddings dimension + # Use a different embedding model with a smaller dimension + other_embedding_model = "sentence-transformers/all-MiniLM-L6-v2" # Dimension 384 + other_embeddings = HuggingFaceEmbeddings(model_name=other_embedding_model) + # Create some dummy documents + docs = [Document(page_content="Test document")] + # Create index using the other embeddings + other_docsearch = FAISS.from_documents(docs, other_embeddings) + # Save index to application/ + other_docsearch.save_local(index_path) + + # Now set the EMBEDDINGS_NAME to the one with a different dimension + settings.EMBEDDINGS_NAME = "huggingface_sentence-transformers/all-mpnet-base-v2" # Dimension 768 + with pytest.raises(ValueError) as exc_info: + FaissStore("", None) + assert "Embedding dimension mismatch" in str(exc_info.value)