test: Add pytest fixture to block requests in unit tests (#4433)

* Add pytest fixture to block requests in unit tests

* Mark test correctly as integration

* Fix crawler unit test failing cause it tries to install chromedriver
This commit is contained in:
Silvano Cerza 2023-04-06 18:04:57 +02:00 committed by GitHub
parent c3abf73332
commit e85dc79eaa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 4 deletions

View File

@ -1006,3 +1006,16 @@ def chatgpt_prompt_model():
@pytest.fixture
def azure_conf():
return haystack_azure_conf()
@pytest.fixture(autouse=True)
def request_blocker(request: pytest.FixtureRequest, monkeypatch):
"""
This fixture is applied automatically to all tests.
Those that are marked as unit will have the requests module
monkeypatched to avoid making HTTP requests by mistake.
"""
marker = request.node.get_closest_marker("unit")
if marker is None:
return
monkeypatch.delattr("requests.sessions.Session")

View File

@ -56,8 +56,9 @@ def content_in_results(crawler: Crawler, url: str, results: List[Path], expected
@pytest.mark.unit
@patch("haystack.nodes.connector.crawler.ChromeDriverManager")
@patch("haystack.nodes.connector.crawler.webdriver")
def test_crawler_url_none_exception(webdriver):
def test_crawler_url_none_exception(webdriver, manager):
crawler = Crawler()
with pytest.raises(ValueError):
crawler.crawl()

View File

@ -35,7 +35,7 @@ docs = [
]
@pytest.mark.unit
@pytest.mark.integration
def test_top_p_sampling(top_p_sampler):
query = "What is the most important religious learning from the Bible?"
results = top_p_sampler.predict(query=query, documents=docs, top_p=0.98)
@ -44,7 +44,7 @@ def test_top_p_sampling(top_p_sampler):
assert results[1].id == "5"
@pytest.mark.unit
@pytest.mark.integration
def test_top_p_sampling_top_p_none(top_p_sampler):
query = "What is the most important religious learning from the Bible?"
results = top_p_sampler.predict(query=query, documents=docs, top_p=None)
@ -55,7 +55,7 @@ def test_top_p_sampling_top_p_none(top_p_sampler):
assert len(results) == 5
@pytest.mark.unit
@pytest.mark.integration
def test_top_p_sampling_at_least_one_result(top_p_sampler):
query = "What is the most important building in King's Landing that has a religious background?"
results = top_p_sampler.predict(query=query, documents=docs, top_p=0.9)