langflow/src/backend/tests/unit
Gabriel Luiz Freitas Almeida 270f609fe7
refactor: enhance tool creation logic and add FeatureFlags (#3662)
* Add `required_inputs` field to `Output` model in `base.py`

* Refactor ComponentTool to ComponentToolkit and enhance tool creation logic

- Replaced `ComponentTool` with `ComponentToolkit` to better encapsulate component-related tools.
- Introduced `build_description` and `_build_output_function` helper functions for dynamic tool creation.
- Updated tool initialization to handle multiple outputs and required inputs using `StructuredTool`.
- Improved schema creation for tool arguments based on component inputs.

* Refactor `to_tool` method to `to_toolkit` to use `ComponentToolkit` instead of `ComponentTool`

* Refactor `ComponentTool` to `ComponentToolkit` in unit tests

- Updated import statements to reflect the new `ComponentToolkit` class.
- Modified test logic to use `ComponentToolkit` for retrieving tools.
- Adjusted assertions to match the new structure and output format.
- Ensured compatibility with `Message` schema for output validation.

* Refactor `test_component_to_tool` to validate `ComponentToolkit` and tool properties

* Refactor `build_description` to include input types in the output format

* Add method to set required inputs for outputs based on method analysis

- Introduced `_set_output_required_inputs` method to determine and set required inputs for each output by analyzing the method's source code.
- Added necessary imports (`ast` and `dedent`) to support the new functionality.

* Update test to assert full tool description in test_component_to_tool.py

* Add unit tests for verifying required inputs of various components

- Added tests to ensure that required inputs for outputs are present in the inputs of `ChatInput`, `ChatOutput`, `SequentialTaskComponent`, `ToolCallingAgentComponent`, and `OpenAIModelComponent`.
- Included helper functions to check if required inputs are in inputs and to assert that all outputs have different required inputs.

* Add RequiredInputsVisitor to identify required inputs in AST nodes

- Introduced RequiredInputsVisitor class to traverse AST nodes and collect required inputs.
- The visitor checks for 'self' attributes matching the provided inputs and adds them to the required_inputs set.

* Refactor required inputs extraction using `RequiredInputsVisitor`

* Add feature flags configuration for toolkit output in settings

* Add toolkit output handling based on feature flag in custom component utils

* Add method to append 'component_as_tool' output in custom component

* Add unit test for toolkit output feature flag in custom component

* Add utility functions for lazy loading and instantiating input types in langflow

- Introduced `get_InputTypesMap` for lazy loading of `InputTypesMap`.
- Added `instantiate_input` function to create instances of input types dynamically.
- Included type checking and error handling for invalid input types.

* Refactor input instantiation logic and update imports

- Removed `instantiate_input` function from `inputs.py` and moved it to `utils.py`.
- Updated imports in `base.py` to reflect the new location of `instantiate_input`.
- Added missing import for `Callable` in `base.py`.

* Refactor import statement to use `instantiate_input` from `langflow.inputs.utils` in test_inputs.py

* Add TOOL_OUTPUT_NAME constant to tools module

* Add type checking and TOOL_OUTPUT_NAME filter in ComponentToolkit

- Introduced `TYPE_CHECKING` for type hints to avoid circular imports.
- Added `TOOL_OUTPUT_NAME` constant to filter specific outputs in `ComponentToolkit`.
- Updated type annotations to use forward references.

* Refactor component toolkit import to avoid circular dependency and use constant for tool output name

* Refactor `ComponentToolkit` class to remove inheritance from `BaseToolkit` and add an initializer for `component`

* Add unit test for ComponentToolkit in test_component_to_tool

- Added `test_component_to_tool_has_no_component_as_tool` to verify that `ComponentToolkit` correctly initializes with a `ChatInput` component and returns the expected tools.

* Refactor toolkit output handling to `custom_component` module

* fix: mypy errors union-attr and arg-type

* Add 'OTHER' field type to schema in langflow/io/schema.py

* Add tool name formatting to ComponentToolkit to ensure valid characters

* Refactor toolkit output handling and add type hint for `to_toolkit` method

* Add `is_interface_component` attribute to vertex types and update import order

* Add tests for ToolCallingAgentComponent and ChatOutput with API key handling

- Updated `test_component_tool` to reflect new description format.
- Added `test_component_tool_with_api_key` to test `ToolCallingAgentComponent` with `ChatOutput` and OpenAI API key.
- Enabled `add_toolkit_output` feature flag for testing.

* Refactor `_find_matching_output_method` to accept `input_name` parameter for more precise input-output matching

* Replace ValueError with warning in build_description function

* use chat_output component directly in set

* [autofix.ci] apply automated fixes

* [autofix.ci] apply automated fixes

* [autofix.ci] apply automated fixes (attempt 3/3)

* Refactor: Reorder method calls in `__init__` for logical consistency

Moved `set_class_code` method call to ensure output types and required inputs are set before class code initialization.

* Update _format_tool_name to allow '.' in tool names

* Refactor `_format_tool_name` to remove non-alphanumeric characters

* Update test assertions for component tool name and output mapping

* Handle case where 'required_inputs' is empty in 'component_tool.py'

* Refactor import statements for better readability in `base.py`

* [autofix.ci] apply automated fixes

* Add noqa comment to suppress import warning and re-add Any import in base.py

---------

Co-authored-by: italojohnny <italojohnnydosanjos@gmail.com>
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
2024-10-01 20:58:51 +00:00
..
api feature: add edit option to global variables table (#3712) 2024-09-09 10:48:04 -03:00
base/tools refactor: enhance tool creation logic and add FeatureFlags (#3662) 2024-10-01 20:58:51 +00:00
components feat: Enhance Data Components and Add SelectData Functionality (#3715) 2024-09-22 18:19:51 +00:00
custom refactor: enhance tool creation logic and add FeatureFlags (#3662) 2024-10-01 20:58:51 +00:00
events feat: add EventManager to centralize callbacks (#3434) 2024-09-02 15:41:19 +02:00
exceptions feat: Add suggestion message to API exception response (#3149) 2024-08-07 15:26:24 +00:00
graph refactor: enhance tool creation logic and add FeatureFlags (#3662) 2024-10-01 20:58:51 +00:00
initial_setup/starter_projects chore: refactor and add components integration tests (#3607) 2024-09-02 15:21:47 +02:00
inputs refactor: enhance tool creation logic and add FeatureFlags (#3662) 2024-10-01 20:58:51 +00:00
io refactor: enhance tool creation logic and add FeatureFlags (#3662) 2024-10-01 20:58:51 +00:00
schema fix: update message reload and update langchain-core (#3054) 2024-07-30 13:54:51 +00:00
services/variable feature: add edit option to global variables table (#3712) 2024-09-09 10:48:04 -03:00
utils feat: truncate parsed uploads to prevent database and frontend blocking caused by excessively large files (#3914) 2024-09-27 15:44:05 +00:00
__init__.py refactor: move tests folder structure and update pytest commands (#2785) 2024-07-18 15:19:43 +00:00
test_api_key.py refactor: move tests folder structure and update pytest commands (#2785) 2024-07-18 15:19:43 +00:00
test_chat_endpoint.py feat: ui build in one single http request (#3020) 2024-08-02 06:53:34 -07:00
test_cli.py refactor: move tests folder structure and update pytest commands (#2785) 2024-07-18 15:19:43 +00:00
test_custom_component.py refactor: enhance tool creation logic and add FeatureFlags (#3662) 2024-10-01 20:58:51 +00:00
test_custom_component_with_client.py feat: start using dev mode flag and add exception filter in logger (#3260) 2024-08-09 13:59:49 +00:00
test_data_class.py refactor: add fixtures for client in test files to speed them up (#3490) 2024-08-22 12:08:47 -07:00
test_data_components.py feat: add starter project graphs (#3369) 2024-08-16 12:47:19 -07:00
test_database.py feat: Add components_only parameter to filter flows by components in API endpoint (#3932) 2024-09-27 18:18:24 +00:00
test_endpoints.py chore: refactor and add components integration tests (#3607) 2024-09-02 15:21:47 +02:00
test_experimental_components.py fix: pythonfunction component execution (#3747) 2024-09-10 19:17:55 +00:00
test_files.py fix: improve file name generation in upload_file function to prevent files with the same name (#3550) 2024-08-26 18:35:19 +00:00
test_frontend_nodes.py fix: textarea with password visual bug (#3739) 2024-09-10 19:20:50 +00:00
test_helper_components.py fix: url component output types and id generator component (#3740) 2024-09-10 12:33:08 +00:00
test_initial_setup.py fix: starter projects refresh don't add new fields (#3110) 2024-07-31 13:43:00 +00:00
test_kubernetes_secrets.py refactor: change typing annotations in tests folder (#3594) 2024-08-28 19:45:19 +00:00
test_loading.py refactor: add fixtures for client in test files to speed them up (#3490) 2024-08-22 12:08:47 -07:00
test_logger.py refactor: add fixtures for client in test files to speed them up (#3490) 2024-08-22 12:08:47 -07:00
test_login.py refactor: move tests folder structure and update pytest commands (#2785) 2024-07-18 15:19:43 +00:00
test_messages.py refactor: move tests folder structure and update pytest commands (#2785) 2024-07-18 15:19:43 +00:00
test_messages_endpoints.py feat: add endpoint and tests for updating session ID in messages (#3946) 2024-10-01 12:35:46 -07:00
test_process.py refactor: move tests folder structure and update pytest commands (#2785) 2024-07-18 15:19:43 +00:00
test_schema.py refactor: update flaky tests in test_schema.py (#3933) 2024-09-27 16:45:26 +00:00
test_setup_superuser.py refactor: move tests folder structure and update pytest commands (#2785) 2024-07-18 15:19:43 +00:00
test_telemetry.py refactor: move tests folder structure and update pytest commands (#2785) 2024-07-18 15:19:43 +00:00
test_template.py refactor: change typing annotations in tests folder (#3594) 2024-08-28 19:45:19 +00:00
test_user.py chore: refactor and add components integration tests (#3607) 2024-09-02 15:21:47 +02:00
test_validate_code.py refactor: add fixtures for client in test files to speed them up (#3490) 2024-08-22 12:08:47 -07:00
test_version.py feat: expose main_version on /version endpoint (#2944) 2024-07-26 13:56:18 +02:00
test_webhook.py chore: refactor and add components integration tests (#3607) 2024-09-02 15:21:47 +02:00