* chore: Update launch.json to use debugpy instead of python for debugging * refactor: Update import statements for Record in langflow components * feat: Add image handling functionality to langflow schema * update projects * 📝 (constants.py): Add 'output_types' to NODE_FORMAT_ATTRIBUTES for consistency and completeness ♻️ (setup.py): Refactor imports to improve readability and maintainability ♻️ (setup.py): Update code to remove fields that are not in the latest template for consistency * refactor: Update schema from Record to Message * refactor: Remove print statement in MonitorService * refactor: Remove fields not in the latest template for consistency * refactor: Update code to handle Record objects in utils.py * update projects * 📝 (monitor.py): Add type hint for message_id parameter in update_message function 📝 (parse.py): Rename ParsedContext to ParsedArgs for clarity 📝 (chat.py): Remove unused imports and methods in ChatComponent class 📝 (StoreMessage.py): Change return type of store_message method from list[Record] to list[Message] 📝 (base.py): Change type hint from Dict[str, str | list[str]] to Mapping[str, str | list[str]] in update_raw_params method 📝 (loading.py): Add condition to check if raw is not None before accessing its attributes in instantiate_custom_component function 📝 (memory.py): Change return type of get_messages function from list[Record] to list[Message] 📝 (memory.py): Change parameter type of add_messages function from Message to Message | list[Message] 📝 (image.py): Add type hint for image_prompt_value variable in Message class 🐛 (record.py): fix type hint for image_prompt_value variable to ImagePromptValue to improve code clarity and maintainability * chore: Add orjson options for serialization * chore: Update orjson options for serialization in setup.py * chore: Update input_value options for models This commit updates the input_value options for the models in the `OpenAIModel.py`, `MistralModel.py`, `CohereModel.py`, `VertexAiModel.py`, `ChatLiteLLMModel.py`, `OllamaModel.py`, `HuggingFaceModel.py`, `AnthropicModel.py`, and `AmazonBedrockModel.py` files. The `input_value` now supports the additional input type "Prompt". This change allows for more flexibility in the input data that can be provided to the models. Fixes #<issue_number> * chore: Update edges with latest component versions This commit updates the edges in the project data with the latest component versions. It ensures that the source and target nodes are correctly updated based on their corresponding nodes in the project. The commit also includes escaping of JSON dumps for the source and target handles in the edges. * 📝 (utils.py): Remove unnecessary async keyword from dict_values_to_string function to improve code readability and consistency 🔧 (utils.py): Simplify handling of Message objects by directly accessing the text property instead of calling to_lc_message() method * chore: Refactor PromptComponent to use updated Prompt class and remove unused imports * feat: Add support for image files in Message model This commit modifies the Message model to support image files as attachments. It introduces the `is_image_file` function to check if a file is an image, and the `to_content_dict` method in the Image class to convert the image object to a content dictionary. Additionally, the `get_file_content_dicts` method is added to generate content dictionaries for all files in the message, including images. This enhancement improves the handling of image attachments in the messaging system. Fixes #<issue_number> * update projects and lock * chore: Update LCModelComponent to use Prompt instead of Record * refactor: Update artifact type to include message in utils.py * fix: Add check for input_value to only pass if string * ✨ (switchOutputView/index.tsx): introduce constant RECORD_TYPES to store valid record types for better readability and maintainability 🔧 (switchOutputView/index.tsx): refactor switch cases to use RECORD_TYPES constant for checking valid record types and simplify the logic for handling different types of result messages * feat: Enable loading from database for openai_api_key field in Langflow starter projects This commit updates the Langflow starter projects by enabling the loading of the `openai_api_key` field from the database. Previously, the field was not being loaded from the database, but now it will be loaded and used in the projects. This change improves the functionality and flexibility of the projects. Fixes #<issue_number> * ♻️ (constants.py): remove unnecessary import statement and clean up code formatting in ORJSON_OPTIONS constant definition * refactor: Update MemoryComponent to use messages instead of records This commit updates the MemoryComponent class in the langflow/components/helpers/MemoryComponent.py file to use the term "messages" instead of "records" for better clarity and consistency. It also updates the get_messages method to return a list of Message objects instead of Record objects. This change improves the naming and readability of the code. * refactor: Update Message model to include timestamp conversion function This commit updates the Message model in the langflow/schema/message.py file to include a new function `_timestamp_to_str` that converts the timestamp to a string format. This function is used as a BeforeValidator for the `timestamp` field, ensuring that it is always formatted correctly. This change improves the consistency and reliability of the timestamp handling in the messaging system. * refactor: Update test_data_components.py to improve directory component loading This commit updates the test_data_components.py file to improve the loading of the directory component. It ensures that the directory component can load mdx files from the ../docs/docs/components directory. This change enhances the functionality and reliability of the directory component. Fixes #<issue_number> * refactor: Update .gitattributes to specify working-tree-encoding for .mdx and .json files This commit updates the .gitattributes file to specify the working-tree-encoding for .mdx and .json files. It sets the encoding to UTF-8 for both file types, ensuring consistent handling of character encoding. This change improves the reliability and compatibility of the repository. Fixes #<issue_number> * fix: 🐛 corrects encoding error * refactor: Update toolkits.mdx to improve documentation and fix formatting * refactor: Add dictdiffer library as a dependency This commit adds the dictdiffer library as a dependency in the poetry.lock file. The dictdiffer library is a useful tool for diffing and patching dictionaries. It will enhance the functionality and flexibility of the project.
186 lines
6.2 KiB
Python
186 lines
6.2 KiB
Python
import os
|
|
from pathlib import Path
|
|
from unittest.mock import Mock, patch
|
|
|
|
import httpx
|
|
import pytest
|
|
import respx
|
|
from dictdiffer import diff
|
|
from httpx import Response
|
|
|
|
from langflow.components import data
|
|
|
|
|
|
@pytest.fixture
|
|
def api_request():
|
|
# This fixture provides an instance of APIRequest for each test case
|
|
return data.APIRequest()
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
@respx.mock
|
|
async def test_successful_get_request(api_request):
|
|
# Mocking a successful GET request
|
|
url = "https://example.com/api/test"
|
|
method = "GET"
|
|
mock_response = {"success": True}
|
|
respx.get(url).mock(return_value=Response(200, json=mock_response))
|
|
|
|
# Making the request
|
|
result = await api_request.make_request(client=httpx.AsyncClient(), method=method, url=url)
|
|
|
|
# Assertions
|
|
assert result.data["status_code"] == 200
|
|
assert result.data["result"] == mock_response
|
|
|
|
|
|
def test_parse_curl(api_request):
|
|
# Arrange
|
|
field_value = (
|
|
"curl -X GET https://example.com/api/test -H 'Content-Type: application/json' -d '{\"key\": \"value\"}'"
|
|
)
|
|
build_config = {
|
|
"method": {"value": ""},
|
|
"urls": {"value": []},
|
|
"headers": {},
|
|
"body": {},
|
|
}
|
|
# Act
|
|
new_build_config = api_request.parse_curl(field_value, build_config.copy())
|
|
|
|
# Assert
|
|
assert new_build_config["method"]["value"] == "GET"
|
|
assert new_build_config["urls"]["value"] == ["https://example.com/api/test"]
|
|
assert new_build_config["headers"]["value"] == {"Content-Type": "application/json"}
|
|
assert new_build_config["body"]["value"] == {"key": "value"}
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
@respx.mock
|
|
async def test_failed_request(api_request):
|
|
# Mocking a failed GET request
|
|
url = "https://example.com/api/test"
|
|
method = "GET"
|
|
respx.get(url).mock(return_value=Response(404))
|
|
|
|
# Making the request
|
|
result = await api_request.make_request(client=httpx.AsyncClient(), method=method, url=url)
|
|
|
|
# Assertions
|
|
assert result.data["status_code"] == 404
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
@respx.mock
|
|
async def test_timeout(api_request):
|
|
# Mocking a timeout
|
|
url = "https://example.com/api/timeout"
|
|
method = "GET"
|
|
respx.get(url).mock(side_effect=httpx.TimeoutException(message="Timeout", request=None))
|
|
|
|
# Making the request
|
|
result = await api_request.make_request(client=httpx.AsyncClient(), method=method, url=url, timeout=1)
|
|
|
|
# Assertions
|
|
assert result.data["status_code"] == 408
|
|
assert result.data["error"] == "Request timed out"
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
@respx.mock
|
|
async def test_build_with_multiple_urls(api_request):
|
|
# This test depends on having a working internet connection and accessible URLs
|
|
# It's better to mock these requests using respx or a similar library
|
|
|
|
# Setup for multiple URLs
|
|
method = "GET"
|
|
urls = ["https://example.com/api/one", "https://example.com/api/two"]
|
|
# You would mock these requests similarly to the single request tests
|
|
for url in urls:
|
|
respx.get(url).mock(return_value=Response(200, json={"success": True}))
|
|
|
|
# Do I have to mock the async client?
|
|
#
|
|
|
|
# Execute the build method
|
|
results = await api_request.build(method=method, urls=urls)
|
|
|
|
# Assertions
|
|
assert len(results) == len(urls)
|
|
|
|
|
|
@patch("langflow.components.data.Directory.parallel_load_records")
|
|
@patch("langflow.components.data.Directory.retrieve_file_paths")
|
|
@patch("langflow.components.data.DirectoryComponent.resolve_path")
|
|
def test_directory_component_build_with_multithreading(
|
|
mock_resolve_path, mock_retrieve_file_paths, mock_parallel_load_records
|
|
):
|
|
# Arrange
|
|
directory_component = data.DirectoryComponent()
|
|
path = os.path.dirname(os.path.abspath(__file__))
|
|
depth = 1
|
|
max_concurrency = 2
|
|
load_hidden = False
|
|
recursive = True
|
|
silent_errors = False
|
|
use_multithreading = True
|
|
|
|
mock_resolve_path.return_value = path
|
|
mock_retrieve_file_paths.return_value = [
|
|
os.path.join(path, file) for file in os.listdir(path) if file.endswith(".py")
|
|
]
|
|
mock_parallel_load_records.return_value = [Mock()]
|
|
|
|
# Act
|
|
directory_component.build(
|
|
path,
|
|
depth,
|
|
max_concurrency,
|
|
load_hidden,
|
|
recursive,
|
|
silent_errors,
|
|
use_multithreading,
|
|
)
|
|
|
|
# Assert
|
|
mock_resolve_path.assert_called_once_with(path)
|
|
mock_retrieve_file_paths.assert_called_once_with(path, load_hidden, recursive, depth)
|
|
mock_parallel_load_records.assert_called_once_with(
|
|
mock_retrieve_file_paths.return_value, silent_errors, max_concurrency
|
|
)
|
|
|
|
|
|
def test_directory_without_mocks():
|
|
directory_component = data.DirectoryComponent()
|
|
from langflow.initial_setup import setup
|
|
from langflow.initial_setup.setup import load_starter_projects
|
|
|
|
_, projects = zip(*load_starter_projects())
|
|
# the setup module has a folder where the projects are stored
|
|
# the contents of that folder are in the projects variable
|
|
# the directory component can be used to load the projects
|
|
# and we can validate if the contents are the same as the projects variable
|
|
setup_path = Path(setup.__file__).parent / "starter_projects"
|
|
results = directory_component.build(str(setup_path), use_multithreading=False)
|
|
assert len(results) == len(projects)
|
|
# each result is a Record that contains the content attribute
|
|
# each are dict that are exactly the same as one of the projects
|
|
for i, result in enumerate(results):
|
|
assert result.text in projects, list(diff(result.text, projects[i]))
|
|
|
|
# in ../docs/docs/components there are many mdx files
|
|
# check if the directory component can load them
|
|
# just check if the number of results is the same as the number of files
|
|
docs_path = Path(__file__).parent.parent / "docs" / "docs" / "components"
|
|
results = directory_component.build(str(docs_path), use_multithreading=False)
|
|
docs_files = list(docs_path.glob("*.mdx"))
|
|
assert len(results) == len(docs_files)
|
|
|
|
|
|
def test_url_component():
|
|
url_component = data.URLComponent()
|
|
# the url component can be used to load the contents of a website
|
|
records = url_component.build(["https://langflow.org"])
|
|
assert all(record.data for record in records)
|
|
assert all(record.text for record in records)
|
|
assert all(record.source for record in records)
|