isayahc commited on
Commit
5e5ca32
·
unverified ·
1 Parent(s): 612008b

fixed condition and added intergration test

Browse files
rag_app/vector_store_handler/vectorstores.py CHANGED
@@ -167,7 +167,7 @@ class FAISSVectorStore(BaseVectorStore):
167
  Raises:
168
  ValueError: If the vector store is not initialized.
169
  """
170
- if self.vectorstore is not None:
171
  raise ValueError("Vector store not initialized. Nothing to save.")
172
  self.vectorstore.save_local(self.persist_directory)
173
 
 
167
  Raises:
168
  ValueError: If the vector store is not initialized.
169
  """
170
+ if self.vectorstore is None:
171
  raise ValueError("Vector store not initialized. Nothing to save.")
172
  self.vectorstore.save_local(self.persist_directory)
173
 
tests/integration/test_vector_store_integration.py ADDED
@@ -0,0 +1,89 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pytest
2
+ from langchain.schema import Document
3
+ from rag_app.vector_store_handler.vectorstores import ChromaVectorStore, FAISSVectorStore
4
+ # from rag_app.database.init_db import db
5
+ from config import EMBEDDING_MODEL, VECTOR_DATABASE_LOCATION
6
+ from langchain.embeddings import HuggingFaceEmbeddings # Or whatever embedding you're using
7
+
8
+ @pytest.fixture(scope="module")
9
+ def embedding_model():
10
+ return HuggingFaceEmbeddings(model_name=EMBEDDING_MODEL)
11
+
12
+ @pytest.fixture(params=[ChromaVectorStore, FAISSVectorStore])
13
+ def vector_store(request, embedding_model, tmp_path):
14
+ store = request.param(embedding_model, persist_directory=str(tmp_path))
15
+ yield store
16
+ # Clean up (if necessary)
17
+ if hasattr(store, 'vectorstore'):
18
+ store.vectorstore.delete_collection()
19
+
20
+ @pytest.fixture
21
+ def sample_documents():
22
+ return [
23
+ Document(page_content="This is a test document about AI."),
24
+ Document(page_content="Another document discussing machine learning."),
25
+ Document(page_content="A third document about natural language processing.")
26
+ ]
27
+
28
+ def test_create_and_search(vector_store, sample_documents):
29
+ # Create vector store
30
+ vector_store.create_vectorstore(sample_documents)
31
+
32
+ # Perform a search
33
+ results = vector_store.similarity_search("AI and machine learning")
34
+
35
+ assert len(results) > 0
36
+ assert any("AI" in doc.page_content for doc in results)
37
+ assert any("machine learning" in doc.page_content for doc in results)
38
+
39
+ def test_save_and_load(vector_store, sample_documents, tmp_path):
40
+ # Create and save vector store
41
+ vector_store.create_vectorstore(sample_documents)
42
+ vector_store.save()
43
+
44
+ # Load the vector store
45
+ loaded_store = type(vector_store)(vector_store.embeddings, persist_directory=str(tmp_path))
46
+ loaded_store.load_existing_vectorstore()
47
+
48
+ # Perform a search on the loaded store
49
+ results = loaded_store.similarity_search("natural language processing")
50
+
51
+ assert len(results) > 0
52
+ assert any("natural language processing" in doc.page_content for doc in results)
53
+
54
+ def test_update_vectorstore(vector_store, sample_documents):
55
+ # Create initial vector store
56
+ vector_store.create_vectorstore(sample_documents)
57
+
58
+ # Add a new document
59
+ new_doc = Document(page_content="A new document about deep learning.")
60
+ vector_store.vectorstore.add_documents([new_doc])
61
+
62
+ # Search for the new content
63
+ results = vector_store.similarity_search("deep learning")
64
+
65
+ assert len(results) > 0
66
+ assert any("deep learning" in doc.page_content for doc in results)
67
+
68
+ @pytest.mark.parametrize("query,expected_content", [
69
+ ("AI", "AI"),
70
+ ("machine learning", "machine learning"),
71
+ ("natural language processing", "natural language processing")
72
+ ])
73
+ def test_search_accuracy(vector_store, sample_documents, query, expected_content):
74
+ vector_store.create_vectorstore(sample_documents)
75
+ results = vector_store.similarity_search(query)
76
+ assert any(expected_content in doc.page_content for doc in results)
77
+
78
+ # def test_database_integration(vector_store, sample_documents):
79
+ # # This test assumes your vector store interacts with the database in some way
80
+ # # You may need to adjust this based on your actual implementation
81
+ # vector_store.create_vectorstore(sample_documents)
82
+
83
+ # # Here, you might add some assertions about how the vector store interacts with the database
84
+ # # For example, if you're storing metadata about the documents in the database:
85
+ # for doc in sample_documents:
86
+ # result = db.session.query(YourDocumentModel).filter_by(content=doc.page_content).first()
87
+ # assert result is not None
88
+
89
+ # Add more integration tests as needed