2024-05-09 15:40:36 +02:00
|
|
|
# SPDX-FileCopyrightText: 2022-present deepset GmbH <info@deepset.ai>
|
|
|
|
#
|
|
|
|
# SPDX-License-Identifier: Apache-2.0
|
2025-02-06 15:19:47 +01:00
|
|
|
|
2022-01-26 18:12:55 +01:00
|
|
|
from pathlib import Path
|
2024-03-13 16:59:26 +01:00
|
|
|
from test.tracing.utils import SpyingTracer
|
2025-02-06 15:19:47 +01:00
|
|
|
from typing import Generator, Dict
|
|
|
|
from unittest.mock import Mock
|
2023-12-21 16:21:24 +01:00
|
|
|
|
2020-05-04 18:00:07 +02:00
|
|
|
import pytest
|
2025-02-06 15:19:47 +01:00
|
|
|
import time
|
|
|
|
import asyncio
|
|
|
|
|
2021-06-14 17:53:43 +02:00
|
|
|
|
2025-02-06 15:19:47 +01:00
|
|
|
from haystack import tracing, component
|
2023-11-24 14:48:43 +01:00
|
|
|
from haystack.testing.test_utils import set_all_seeds
|
2020-05-04 18:00:07 +02:00
|
|
|
|
2023-11-24 11:52:55 +01:00
|
|
|
set_all_seeds(0)
|
2022-01-25 20:36:28 +01:00
|
|
|
|
2024-06-26 12:32:05 +02:00
|
|
|
# Tracing is disable by default to avoid failures in CI
|
|
|
|
tracing.disable_tracing()
|
|
|
|
|
2022-08-12 09:27:56 +01:00
|
|
|
|
2025-02-06 15:19:47 +01:00
|
|
|
@pytest.fixture()
|
|
|
|
def waiting_component():
|
|
|
|
@component
|
|
|
|
class Waiter:
|
|
|
|
@component.output_types(waited_for=int)
|
|
|
|
def run(self, wait_for: int) -> Dict[str, int]:
|
|
|
|
time.sleep(wait_for)
|
|
|
|
return {"waited_for": wait_for}
|
|
|
|
|
|
|
|
@component.output_types(waited_for=int)
|
|
|
|
async def run_async(self, wait_for: int) -> Dict[str, int]:
|
|
|
|
await asyncio.sleep(wait_for)
|
|
|
|
return {"waited_for": wait_for}
|
|
|
|
|
|
|
|
return Waiter
|
|
|
|
|
|
|
|
|
2023-11-24 11:52:55 +01:00
|
|
|
@pytest.fixture()
|
|
|
|
def mock_tokenizer():
|
2023-01-16 15:36:14 +01:00
|
|
|
"""
|
2023-11-24 11:52:55 +01:00
|
|
|
Tokenizes the string by splitting on spaces.
|
2023-01-16 15:36:14 +01:00
|
|
|
"""
|
2023-11-24 11:52:55 +01:00
|
|
|
tokenizer = Mock()
|
|
|
|
tokenizer.encode = lambda text: text.split()
|
|
|
|
tokenizer.decode = lambda tokens: " ".join(tokens)
|
|
|
|
return tokenizer
|
2023-06-13 14:52:24 +02:00
|
|
|
|
|
|
|
|
2023-11-24 11:52:55 +01:00
|
|
|
@pytest.fixture()
|
|
|
|
def test_files_path():
|
|
|
|
return Path(__file__).parent / "test_files"
|
2023-12-21 08:51:54 +01:00
|
|
|
|
|
|
|
|
|
|
|
@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)
|
2024-02-22 12:52:04 +01:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture()
|
|
|
|
def spying_tracer() -> Generator[SpyingTracer, None, None]:
|
|
|
|
tracer = SpyingTracer()
|
|
|
|
tracing.enable_tracing(tracer)
|
2025-02-06 15:19:47 +01:00
|
|
|
tracer.is_content_tracing_enabled = True
|
2024-02-22 12:52:04 +01:00
|
|
|
|
|
|
|
yield tracer
|
|
|
|
|
|
|
|
# Make sure to disable tracing after the test to avoid affecting other tests
|
|
|
|
tracing.disable_tracing()
|