c56bef871b
Sync docs with Docusaurus / sync (push) Waiting to run
Tests / Check if changed (push) Waiting to run
Tests / format (push) Blocked by required conditions
Tests / check-imports (push) Blocked by required conditions
Tests / Unit / macos-latest (push) Blocked by required conditions
Tests / Unit / ubuntu-latest (push) Blocked by required conditions
Tests / Unit / windows-latest (push) Blocked by required conditions
Tests / mypy (push) Blocked by required conditions
Tests / Integration / ubuntu-latest (push) Blocked by required conditions
Tests / Integration / macos-latest (push) Blocked by required conditions
Tests / Integration / windows-latest (push) Blocked by required conditions
Tests / notify-slack-on-failure (push) Blocked by required conditions
Tests / Mark tests as completed (push) Blocked by required conditions
Update Platform Components Table / update (push) Waiting to run
CodeQL / Analyze (python) (push) Waiting to run
Docker image release / Build base image (push) Waiting to run
42 lines
1.5 KiB
Python
42 lines
1.5 KiB
Python
# SPDX-FileCopyrightText: 2022-present deepset GmbH <info@deepset.ai>
|
|
#
|
|
# SPDX-License-Identifier: Apache-2.0
|
|
|
|
import sys
|
|
from unittest.mock import patch
|
|
|
|
from haystack.lazy_imports import LazyImport
|
|
|
|
|
|
def test_lazy_importer_avoids_importing_unused_modules():
|
|
# Save the original state of the module if it exists
|
|
original_imported = sys.modules.get("haystack.components.generators.chat.azure")
|
|
|
|
with patch.dict(sys.modules):
|
|
# Remove the module from sys.modules if it was already imported
|
|
if original_imported:
|
|
del sys.modules["haystack.components.generators.chat.azure"]
|
|
|
|
from haystack.components.generators.chat import OpenAIChatGenerator # Import the intended class # noqa: F401
|
|
|
|
assert "haystack.components.generators.chat.openai" in sys.modules.keys()
|
|
assert "haystack.components.generators.chat.azure" not in sys.modules.keys()
|
|
|
|
# Restore the module if it was previously imported (preserves test isolation)
|
|
if original_imported:
|
|
sys.modules["haystack.components.generators.chat.azure"] = original_imported
|
|
|
|
|
|
def test_import_error_is_suppressed_and_deferred():
|
|
with LazyImport() as lazy_import:
|
|
import a_module # noqa: F401
|
|
|
|
assert lazy_import._deferred is not None
|
|
exc_value, message = lazy_import._deferred
|
|
assert isinstance(exc_value, ImportError)
|
|
expected_message = (
|
|
"Haystack failed to import the optional dependency 'a_module'. Try 'pip install a_module'. "
|
|
"Original error: No module named 'a_module'"
|
|
)
|
|
assert expected_message in message
|