test: Add fixture to block requests in tests (#6585)

* Add fixture to block requests in tests

* Mark tests making requests as integration
This commit is contained in:
Silvano Cerza 2023-12-21 08:51:54 +01:00 committed by GitHub
parent 5546c8144e
commit 8a513f3b8c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 0 deletions

View File

@ -21,3 +21,20 @@ def mock_tokenizer():
@pytest.fixture()
def test_files_path():
return Path(__file__).parent / "test_files"
@pytest.fixture(autouse=True)
def request_blocker(request: pytest.FixtureRequest, monkeypatch):
"""
This fixture is applied automatically to all tests.
Those that are not marked as integration will have the requests module
monkeypatched to avoid making HTTP requests by mistake.
"""
marker = request.node.get_closest_marker("integration")
if marker is not None:
return
def urlopen_mock(self, method, url, *args, **kwargs):
raise RuntimeError(f"The test was about to {method} {self.scheme}://{self.host}{url}")
monkeypatch.setattr("urllib3.connectionpool.HTTPConnectionPool.urlopen", urlopen_mock)

View File

@ -6,6 +6,7 @@ from haystack.document_stores import InMemoryDocumentStore
class TestIndexingPipeline:
# indexing files without embeddings
@pytest.mark.integration
def test_indexing_files_without_embeddings(self, test_files_path):
file_paths = [test_files_path / "txt" / "doc_1.txt", test_files_path / "txt" / "doc_2.txt"]
document_store = InMemoryDocumentStore()
@ -36,6 +37,7 @@ class TestIndexingPipeline:
assert result["documents_written"] >= 3
# indexing multiple files
@pytest.mark.integration
def test_indexing_multiple_file_types(self, test_files_path):
document_store = InMemoryDocumentStore()
pipeline = build_indexing_pipeline(

View File

@ -37,6 +37,7 @@ def mock_chat_completion():
yield mock_chat_completion_create
@pytest.mark.integration
def test_rag_pipeline(mock_chat_completion):
rag_pipe = build_rag_pipeline(document_store=InMemoryDocumentStore())
answer = rag_pipe.run(query="question")