refactor: enhance CustomComponent class and updates tests (#3201)

* fix: update CustomComponent to use properties for user_id and flow_id

Refactored user_id and flow_id in CustomComponent to use properties for better encapsulation and code clarity

* refactor: update CustomComponent initialization and remove unused imports

Refactored the CustomComponent class to streamline initialization and removed unnecessary import for BaseCallbackHandler

* refactor: update build_custom_component_template to use cc_instance for field order to improve consistency and clarity

* refactor: update user_id parameter in FlowToolComponent to use self.user_id for consistency

* refactor: remove unused _tree attribute and clean up imports in CustomComponent for better code clarity

* refactor: rename CustomComponent to Component for consistency in directory_reader.py import and usage

* refactor: enhance timestamp handling in _timestamp_to_str for better validation and error reporting in message.py

* refactor: preserve async get_file_content_dicts method for backwards compatibility in message.py

* refactor: update function_entrypoint_name to _function_entrypoint_name for consistency in test_custom_component.py

* feat: add client fixture for improved test structure in test_data_components.py

* feat: add unit tests for PromptComponent including template processing and custom fields in test_prompt_component.py

* feat: add dev dependencies for improved testing and development tools in pyproject.toml
This commit is contained in:
Gabriel Luiz Freitas Almeida 2024-08-05 16:55:29 -03:00 • committed by GitHub
commit f706b05438
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
12 changed files with 1823 additions and 89 deletions

View file

@ -0,0 +1,19 @@
import pytest
from langflow.components.prompts.Prompt import PromptComponent # type: ignore
@pytest.fixture
def client():
pass
class TestPromptComponent:
def test_post_code_processing(self):
component = PromptComponent(template="Hello {name}!", name="John")
frontend_node = component.to_frontend_node()
node_data = frontend_node["data"]["node"]
assert node_data["template"]["template"]["value"] == "Hello {name}!"
assert "name" in node_data["custom_fields"]["template"]
assert "name" in node_data["template"]
assert node_data["template"]["name"]["value"] == "John"

View file

@ -72,16 +72,16 @@ def test_component_init():
"""
Test the initialization of the Component class.
"""
component = BaseComponent(_code=code_default, function_entrypoint_name="build")
component = BaseComponent(_code=code_default, _function_entrypoint_name="build")
assert component._code == code_default
assert component.function_entrypoint_name == "build"
assert component._function_entrypoint_name == "build"
def test_component_get_code_tree():
"""
Test the get_code_tree method of the Component class.
"""
component = BaseComponent(_code=code_default, function_entrypoint_name="build")
component = BaseComponent(_code=code_default, _function_entrypoint_name="build")
tree = component.get_code_tree(component._code)
assert "imports" in tree
@ -91,7 +91,7 @@ def test_component_code_null_error():
Test the get_function method raises the
ComponentCodeNullError when the code is empty.
"""
component = BaseComponent(_code="", function_entrypoint_name="")
component = BaseComponent(_code="", _function_entrypoint_name="")
with pytest.raises(ComponentCodeNullError):
component.get_function()
@ -102,16 +102,16 @@ def test_custom_component_init():
"""
function_entrypoint_name = "build"
custom_component = CustomComponent(_code=code_default, function_entrypoint_name=function_entrypoint_name)
custom_component = CustomComponent(_code=code_default, _function_entrypoint_name=function_entrypoint_name)
assert custom_component._code == code_default
assert custom_component.function_entrypoint_name == function_entrypoint_name
assert custom_component._function_entrypoint_name == function_entrypoint_name
def test_custom_component_build_template_config():
"""
Test the build_template_config property of the CustomComponent class.
"""
custom_component = CustomComponent(_code=code_default, function_entrypoint_name="build")
custom_component = CustomComponent(_code=code_default, _function_entrypoint_name="build")
config = custom_component.build_template_config()
assert isinstance(config, dict)
@ -120,7 +120,7 @@ def test_custom_component_get_function():
"""
Test the get_function property of the CustomComponent class.
"""
custom_component = CustomComponent(_code="def build(): pass", function_entrypoint_name="build")
custom_component = CustomComponent(_code="def build(): pass", _function_entrypoint_name="build")
my_function = custom_component.get_function()
assert isinstance(my_function, types.FunctionType)
@ -195,7 +195,7 @@ def test_component_get_function_valid():
Test the get_function method of the Component
class with valid code and function_entrypoint_name.
"""
component = BaseComponent(_code="def build(): pass", function_entrypoint_name="build")
component = BaseComponent(_code="def build(): pass", _function_entrypoint_name="build")
my_function = component.get_function()
assert callable(my_function)
@ -205,7 +205,7 @@ def test_custom_component_get_function_entrypoint_args():
Test the get_function_entrypoint_args
property of the CustomComponent class.
"""
custom_component = CustomComponent(_code=code_default, function_entrypoint_name="build")
custom_component = CustomComponent(_code=code_default, _function_entrypoint_name="build")
args = custom_component.get_function_entrypoint_args
assert len(args) == 3
assert args[0]["name"] == "self"
@ -219,7 +219,7 @@ def test_custom_component_get_function_entrypoint_return_type():
property of the CustomComponent class.
"""
custom_component = CustomComponent(_code=code_default, function_entrypoint_name="build")
custom_component = CustomComponent(_code=code_default, _function_entrypoint_name="build")
return_type = custom_component.get_function_entrypoint_return_type
assert return_type == [Document]
@ -228,7 +228,7 @@ def test_custom_component_get_main_class_name():
"""
Test the get_main_class_name property of the CustomComponent class.
"""
custom_component = CustomComponent(_code=code_default, function_entrypoint_name="build")
custom_component = CustomComponent(_code=code_default, _function_entrypoint_name="build")
class_name = custom_component.get_main_class_name
assert class_name == "YourComponent"
@ -238,7 +238,7 @@ def test_custom_component_get_function_valid():
Test the get_function property of the CustomComponent
class with valid code and function_entrypoint_name.
"""
custom_component = CustomComponent(_code="def build(): pass", function_entrypoint_name="build")
custom_component = CustomComponent(_code="def build(): pass", _function_entrypoint_name="build")
my_function = custom_component.get_function
assert callable(my_function)
@ -352,7 +352,7 @@ def test_component_get_code_tree_syntax_error():
Test the get_code_tree method of the Component class
raises the CodeSyntaxError when given incorrect syntax.
"""
component = BaseComponent(_code="import os as", function_entrypoint_name="build")
component = BaseComponent(_code="import os as", _function_entrypoint_name="build")
with pytest.raises(CodeSyntaxError):
component.get_code_tree(component._code)
@ -362,7 +362,7 @@ def test_custom_component_class_template_validation_no_code():
Test the _class_template_validation method of the CustomComponent class
raises the HTTPException when the code is None.
"""
custom_component = CustomComponent(_code=None, function_entrypoint_name="build")
custom_component = CustomComponent(_code=None, _function_entrypoint_name="build")
with pytest.raises(TypeError):
custom_component.get_function()
@ -372,7 +372,7 @@ def test_custom_component_get_code_tree_syntax_error():
Test the get_code_tree method of the CustomComponent class
raises the CodeSyntaxError when given incorrect syntax.
"""
custom_component = CustomComponent(_code="import os as", function_entrypoint_name="build")
custom_component = CustomComponent(_code="import os as", _function_entrypoint_name="build")
with pytest.raises(CodeSyntaxError):
custom_component.get_code_tree(custom_component._code)
@ -387,7 +387,7 @@ class MyMainClass(CustomComponent):
def build():
pass"""
custom_component = CustomComponent(_code=my_code, function_entrypoint_name="build")
custom_component = CustomComponent(_code=my_code, _function_entrypoint_name="build")
args = custom_component.get_function_entrypoint_args
assert len(args) == 0
@ -402,7 +402,7 @@ class MyClass(CustomComponent):
def build():
pass"""
custom_component = CustomComponent(_code=my_code, function_entrypoint_name="build")
custom_component = CustomComponent(_code=my_code, _function_entrypoint_name="build")
return_type = custom_component.get_function_entrypoint_return_type
assert return_type == []
@ -416,7 +416,7 @@ def test_custom_component_get_main_class_name_no_main_class():
def build():
pass"""
custom_component = CustomComponent(_code=my_code, function_entrypoint_name="build")
custom_component = CustomComponent(_code=my_code, _function_entrypoint_name="build")
class_name = custom_component.get_main_class_name
assert class_name == ""
@ -426,7 +426,7 @@ def test_custom_component_build_not_implemented():
Test the build method of the CustomComponent
class raises the NotImplementedError.
"""
custom_component = CustomComponent(_code="def build(): pass", function_entrypoint_name="build")
custom_component = CustomComponent(_code="def build(): pass", _function_entrypoint_name="build")
with pytest.raises(NotImplementedError):
custom_component.build()

View file

@ -12,6 +12,11 @@ from httpx import Response
from langflow.components import data
@pytest.fixture
def client():
pass
@pytest.fixture
def api_request():
# This fixture provides an instance of APIRequest for each test case