langflow/src/backend/tests/base.py
Gabriel Luiz Freitas Almeida 0dc6cce8dc
tests: Enhance component testing and error handling with dynamic retrieval (#4526)
* Add module parameter to build_component_instance_for_tests function for dynamic component retrieval

* Enhance component test base with detailed version mapping and error handling

- Introduced `VersionComponentMapping` TypedDict for structured version mapping.
- Updated `FILE_NAMES_MAPPING` to use a list of `VersionComponentMapping`.
- Added comprehensive error messages for missing or invalid mappings in `test_all_versions_have_a_file_name_defined`.
- Improved `test_component_versions` with detailed exception handling and error reporting.
- Ensured `component_class` is defined before running tests.

* Refactor FILE_NAMES_MAPPING to use a list of dictionaries for better structure and readability in test_prompt_component.py

* refactor: Enhance ComponentTestBase with fixture validation and improved version handling

* Refactor test setup in `test_prompt_component.py` to use fixtures for improved modularity and readability

* fix: Add PlaceholderGraph NamedTuple and handle 'graph' attribute in Component class

* Add attribute checks for 'graph' and 'vertex' to prevent errors

* Handle missing 'graph' attribute in 'store_message' method to prevent errors.

* Handle missing 'graph' attribute in Message creation to prevent errors

* Handle missing 'graph' attribute in chat message flow ID assignment

* Add component code to test instance creation and error logging

* Update SUPPORTED_VERSIONS to remove older versions

* test: add unit tests for ChatInput and TextInputComponent

Implement comprehensive tests for both ChatInput and TextInputComponent to ensure proper functionality, including message responses and handling of various input scenarios. This enhances reliability and aids in future development.

* test: add unit tests for ChatOutput and TextOutputComponent

Implement comprehensive tests for ChatOutput and TextOutputComponent, validating message responses, source properties, and behavior with various input types to ensure reliability and consistency across output components.

* Update JSON files to improve code readability and add missing info fields

- Added missing `info` fields to various input components to provide better context and descriptions.
- Improved code readability by ensuring consistent formatting and structure across JSON files.
- Updated `message_response` method to handle cases where `graph` attribute might not be present.
- Enhanced `build_vectorize_options` method to set `authentication` and `parameters` to `None` if no values are provided.
- Refined `AgentComponent` to include `info` for `agent_llm` and other fields, improving clarity on their purpose.

* Refactor: update attribute access to use private `_vertex` attribute

* test: enhance TextInputComponent tests and update properties assertions

* Remove redundant unit tests for output components in test_output_components.py

* feat: add PlaceholderGraph for backwards compatibility and enhance Component attributes

* fix: improve run_id assignment and ensure user_id is a string in PlaceholderGraph

* Add check for non-empty incoming_edges in get_properties_from_source_component
2024-11-12 10:50:56 -08:00

146 lines
5.5 KiB
Python

from typing import Any
import pytest
from typing_extensions import TypedDict
from tests.constants import SUPPORTED_VERSIONS
from tests.integration.utils import build_component_instance_for_tests
class VersionComponentMapping(TypedDict):
version: str
module: str
file_name: str
# Sentinel value to mark undefined test cases
DID_NOT_EXIST = object()
class ComponentTestBase:
@pytest.fixture(autouse=True)
def _validate_required_fixtures(
self,
component_class: type[Any],
default_kwargs: dict[str, Any],
file_names_mapping: list[VersionComponentMapping],
) -> None:
"""Validate that all required fixtures are implemented."""
# If we get here, all fixtures exist
@pytest.fixture
def component_class(self) -> type[Any]:
"""Return the component class to test."""
msg = f"{self.__class__.__name__} must implement the component_class fixture"
raise NotImplementedError(msg)
@pytest.fixture
def default_kwargs(self) -> dict[str, Any]:
"""Return the default kwargs for the component."""
return {}
@pytest.fixture
def file_names_mapping(self) -> list[VersionComponentMapping]:
"""Return the file names mapping for different versions."""
msg = f"{self.__class__.__name__} must implement the file_names_mapping fixture"
raise NotImplementedError(msg)
def test_latest_version(self, component_class: type[Any], default_kwargs: dict[str, Any]) -> None:
"""Test that the component works with the latest version."""
result = component_class(**default_kwargs)()
assert result is not None, "Component returned None for the latest version."
def test_all_versions_have_a_file_name_defined(self, file_names_mapping: list[VersionComponentMapping]) -> None:
"""Ensure all supported versions have a file name defined."""
if not file_names_mapping:
msg = (
f"file_names_mapping is empty for {self.__class__.__name__}. "
"Please define the version mappings for your component."
)
raise AssertionError(msg)
version_mappings = {mapping["version"]: mapping for mapping in file_names_mapping}
for version in SUPPORTED_VERSIONS:
if version not in version_mappings:
supported_versions = ", ".join(sorted(m["version"] for m in file_names_mapping))
msg = (
f"Version {version} not found in file_names_mapping for {self.__class__.__name__}.\n"
f"Currently defined versions: {supported_versions}\n"
"Please add this version to your component's file_names_mapping."
)
raise AssertionError(msg)
mapping = version_mappings[version]
if mapping["file_name"] is None:
msg = (
f"file_name is None for version {version} in {self.__class__.__name__}.\n"
"Please provide a valid file_name in file_names_mapping or set it to DID_NOT_EXIST."
)
raise AssertionError(msg)
if mapping["module"] is None:
msg = (
f"module is None for version {version} in {self.__class__.__name__}.\n"
"Please provide a valid module name in file_names_mapping or set it to DID_NOT_EXIST."
)
raise AssertionError(msg)
@pytest.mark.parametrize("version", SUPPORTED_VERSIONS)
def test_component_versions(
self,
version: str,
default_kwargs: dict[str, Any],
file_names_mapping: list[VersionComponentMapping],
) -> None:
"""Test if the component works across different versions."""
version_mappings = {mapping["version"]: mapping for mapping in file_names_mapping}
mapping = version_mappings[version]
if mapping["file_name"] is DID_NOT_EXIST:
pytest.skip(f"Skipping version {version} as it does not have a file name defined.")
try:
instance, component_code = build_component_instance_for_tests(
version, file_name=mapping["file_name"], module=mapping["module"], **default_kwargs
)
except Exception as e:
msg = (
f"Failed to build component instance for {self.__class__.__name__} "
f"version {version}:\n"
f"Module: {mapping['module']}\n"
f"File: {mapping['file_name']}\n"
f"Error: {e!s}"
)
raise AssertionError(msg) from e
try:
result = instance()
except Exception as e:
msg = (
f"Failed to execute component {self.__class__.__name__} "
f"for version {version}:\n"
f"Module: {mapping['module']}\n"
f"File: {mapping['file_name']}\n"
f"Error: {e!s}\n"
f"Component Code: {component_code}"
)
raise AssertionError(msg) from e
if result is None:
msg = (
f"Component {self.__class__.__name__} returned None "
f"for version {version}.\n"
f"Module: {mapping['module']}\n"
f"File: {mapping['file_name']}"
)
raise AssertionError(msg)
@pytest.mark.usefixtures("client")
class ComponentTestBaseWithClient(ComponentTestBase):
pass
class ComponentTestBaseWithoutClient(ComponentTestBase):
pass