mirror of
https://github.com/langgenius/dify.git
synced 2026-01-07 23:04:12 +00:00
test(api): add test cases for ParameterConfig validation logic
This commit is contained in:
@@ -58,20 +58,6 @@ class ParameterConfig(BaseModel):
|
||||
description: str
|
||||
required: bool
|
||||
|
||||
_is_old_select_type: bool = PrivateAttr(default=False)
|
||||
|
||||
@model_validator(mode="wrap")
|
||||
@classmethod
|
||||
def log_failed_validation(cls, data: Any, handler: ModelWrapValidatorHandler[Self]) -> Self:
|
||||
if not isinstance(data, dict):
|
||||
return handler(data)
|
||||
|
||||
original_type = data.get("type")
|
||||
instance = handler(data)
|
||||
if original_type == _OLD_SELECT_TYPE_NAME:
|
||||
instance._is_old_select_type = True
|
||||
return instance
|
||||
|
||||
@field_validator("name", mode="before")
|
||||
@classmethod
|
||||
def validate_name(cls, value) -> str:
|
||||
@@ -138,7 +124,7 @@ class ParameterExtractorNodeData(BaseNodeData):
|
||||
else:
|
||||
parameter_schema["type"] = parameter.type
|
||||
|
||||
if parameter._is_old_select_type:
|
||||
if parameter.options:
|
||||
parameter_schema["enum"] = parameter.options
|
||||
|
||||
parameters["properties"][parameter.name] = parameter_schema
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
from core.variables.types import SegmentType
|
||||
from core.workflow.nodes.parameter_extractor.entities import ParameterConfig
|
||||
|
||||
|
||||
class TestParameterConfig:
|
||||
def test_select_type(self):
|
||||
data = {
|
||||
"name": "yes_or_no",
|
||||
"type": "select",
|
||||
"options": ["yes", "no"],
|
||||
"description": "a simple select made of `yes` and `no`",
|
||||
"required": True,
|
||||
}
|
||||
|
||||
pc = ParameterConfig.model_validate(data)
|
||||
assert pc.type == SegmentType.STRING
|
||||
assert pc.options == data["options"]
|
||||
|
||||
def test_validate_bool_type(self):
|
||||
data = {
|
||||
"name": "boolean",
|
||||
"type": "bool",
|
||||
"description": "a simple boolean parameter",
|
||||
"required": True,
|
||||
}
|
||||
pc = ParameterConfig.model_validate(data)
|
||||
assert pc.type == SegmentType.BOOLEAN
|
||||
Reference in New Issue
Block a user