mirror of
https://github.com/langflow-ai/langflow.git
synced 2026-07-26 01:49:27 +08:00
Add test for custom component with TemplateField
This commit is contained in:
@ -308,3 +308,11 @@ def test_component_code():
|
||||
# load the content as a string
|
||||
with open(path, "r") as f:
|
||||
return f.read()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def test_component_with_templatefield_code():
|
||||
path = Path(__file__).parent.absolute() / "data" / "component_with_templatefield.py"
|
||||
# load the content as a string
|
||||
with open(path, "r") as f:
|
||||
return f.read()
|
||||
|
||||
17
tests/data/component_with_templatefield.py
Normal file
17
tests/data/component_with_templatefield.py
Normal file
@ -0,0 +1,17 @@
|
||||
import random
|
||||
|
||||
from langflow import CustomComponent
|
||||
from langflow.template.field.base import TemplateField
|
||||
|
||||
|
||||
class TestComponent(CustomComponent):
|
||||
def refresh_values(self):
|
||||
# This is a function that will be called every time the component is updated
|
||||
# and should return a list of random strings
|
||||
return [f"Random {random.randint(1, 100)}" for _ in range(5)]
|
||||
|
||||
def build_config(self):
|
||||
return {"param": TemplateField(display_name="Param", options=self.refresh_values)}
|
||||
|
||||
def build(self, param: int):
|
||||
return param
|
||||
@ -549,3 +549,17 @@ def test_build_langchain_template_custom_component_valid_code(test_component_cod
|
||||
frontend_node = build_custom_component_template(component, update_field="param")
|
||||
new_param_options = frontend_node["template"]["param"]["options"]
|
||||
assert param_options != new_param_options
|
||||
|
||||
|
||||
def test_build_langchain_template_custom_component_templatefield(test_component_with_templatefield_code):
|
||||
component = create_and_validate_component(test_component_with_templatefield_code)
|
||||
frontend_node = build_custom_component_template(component)
|
||||
assert isinstance(frontend_node, dict)
|
||||
template = frontend_node["template"]
|
||||
assert isinstance(template, dict)
|
||||
assert "param" in template
|
||||
param_options = template["param"]["options"]
|
||||
# Now run it again with an update field
|
||||
frontend_node = build_custom_component_template(component, update_field="param")
|
||||
new_param_options = frontend_node["template"]["param"]["options"]
|
||||
assert param_options != new_param_options
|
||||
|
||||
Reference in New Issue
Block a user