Co-authored-by: -LAN- <laipz8200@outlook.com>
This commit is contained in:
@@ -17,31 +17,31 @@ from core.app.segments.exc import VariableError
|
||||
|
||||
|
||||
def test_string_variable():
|
||||
test_data = {'value_type': 'string', 'name': 'test_text', 'value': 'Hello, World!'}
|
||||
test_data = {"value_type": "string", "name": "test_text", "value": "Hello, World!"}
|
||||
result = factory.build_variable_from_mapping(test_data)
|
||||
assert isinstance(result, StringVariable)
|
||||
|
||||
|
||||
def test_integer_variable():
|
||||
test_data = {'value_type': 'number', 'name': 'test_int', 'value': 42}
|
||||
test_data = {"value_type": "number", "name": "test_int", "value": 42}
|
||||
result = factory.build_variable_from_mapping(test_data)
|
||||
assert isinstance(result, IntegerVariable)
|
||||
|
||||
|
||||
def test_float_variable():
|
||||
test_data = {'value_type': 'number', 'name': 'test_float', 'value': 3.14}
|
||||
test_data = {"value_type": "number", "name": "test_float", "value": 3.14}
|
||||
result = factory.build_variable_from_mapping(test_data)
|
||||
assert isinstance(result, FloatVariable)
|
||||
|
||||
|
||||
def test_secret_variable():
|
||||
test_data = {'value_type': 'secret', 'name': 'test_secret', 'value': 'secret_value'}
|
||||
test_data = {"value_type": "secret", "name": "test_secret", "value": "secret_value"}
|
||||
result = factory.build_variable_from_mapping(test_data)
|
||||
assert isinstance(result, SecretVariable)
|
||||
|
||||
|
||||
def test_invalid_value_type():
|
||||
test_data = {'value_type': 'unknown', 'name': 'test_invalid', 'value': 'value'}
|
||||
test_data = {"value_type": "unknown", "name": "test_invalid", "value": "value"}
|
||||
with pytest.raises(VariableError):
|
||||
factory.build_variable_from_mapping(test_data)
|
||||
|
||||
@@ -49,51 +49,51 @@ def test_invalid_value_type():
|
||||
def test_build_a_blank_string():
|
||||
result = factory.build_variable_from_mapping(
|
||||
{
|
||||
'value_type': 'string',
|
||||
'name': 'blank',
|
||||
'value': '',
|
||||
"value_type": "string",
|
||||
"name": "blank",
|
||||
"value": "",
|
||||
}
|
||||
)
|
||||
assert isinstance(result, StringVariable)
|
||||
assert result.value == ''
|
||||
assert result.value == ""
|
||||
|
||||
|
||||
def test_build_a_object_variable_with_none_value():
|
||||
var = factory.build_segment(
|
||||
{
|
||||
'key1': None,
|
||||
"key1": None,
|
||||
}
|
||||
)
|
||||
assert isinstance(var, ObjectSegment)
|
||||
assert var.value['key1'] is None
|
||||
assert var.value["key1"] is None
|
||||
|
||||
|
||||
def test_object_variable():
|
||||
mapping = {
|
||||
'id': str(uuid4()),
|
||||
'value_type': 'object',
|
||||
'name': 'test_object',
|
||||
'description': 'Description of the variable.',
|
||||
'value': {
|
||||
'key1': 'text',
|
||||
'key2': 2,
|
||||
"id": str(uuid4()),
|
||||
"value_type": "object",
|
||||
"name": "test_object",
|
||||
"description": "Description of the variable.",
|
||||
"value": {
|
||||
"key1": "text",
|
||||
"key2": 2,
|
||||
},
|
||||
}
|
||||
variable = factory.build_variable_from_mapping(mapping)
|
||||
assert isinstance(variable, ObjectSegment)
|
||||
assert isinstance(variable.value['key1'], str)
|
||||
assert isinstance(variable.value['key2'], int)
|
||||
assert isinstance(variable.value["key1"], str)
|
||||
assert isinstance(variable.value["key2"], int)
|
||||
|
||||
|
||||
def test_array_string_variable():
|
||||
mapping = {
|
||||
'id': str(uuid4()),
|
||||
'value_type': 'array[string]',
|
||||
'name': 'test_array',
|
||||
'description': 'Description of the variable.',
|
||||
'value': [
|
||||
'text',
|
||||
'text',
|
||||
"id": str(uuid4()),
|
||||
"value_type": "array[string]",
|
||||
"name": "test_array",
|
||||
"description": "Description of the variable.",
|
||||
"value": [
|
||||
"text",
|
||||
"text",
|
||||
],
|
||||
}
|
||||
variable = factory.build_variable_from_mapping(mapping)
|
||||
@@ -104,11 +104,11 @@ def test_array_string_variable():
|
||||
|
||||
def test_array_number_variable():
|
||||
mapping = {
|
||||
'id': str(uuid4()),
|
||||
'value_type': 'array[number]',
|
||||
'name': 'test_array',
|
||||
'description': 'Description of the variable.',
|
||||
'value': [
|
||||
"id": str(uuid4()),
|
||||
"value_type": "array[number]",
|
||||
"name": "test_array",
|
||||
"description": "Description of the variable.",
|
||||
"value": [
|
||||
1,
|
||||
2.0,
|
||||
],
|
||||
@@ -121,18 +121,18 @@ def test_array_number_variable():
|
||||
|
||||
def test_array_object_variable():
|
||||
mapping = {
|
||||
'id': str(uuid4()),
|
||||
'value_type': 'array[object]',
|
||||
'name': 'test_array',
|
||||
'description': 'Description of the variable.',
|
||||
'value': [
|
||||
"id": str(uuid4()),
|
||||
"value_type": "array[object]",
|
||||
"name": "test_array",
|
||||
"description": "Description of the variable.",
|
||||
"value": [
|
||||
{
|
||||
'key1': 'text',
|
||||
'key2': 1,
|
||||
"key1": "text",
|
||||
"key2": 1,
|
||||
},
|
||||
{
|
||||
'key1': 'text',
|
||||
'key2': 1,
|
||||
"key1": "text",
|
||||
"key2": 1,
|
||||
},
|
||||
],
|
||||
}
|
||||
@@ -140,19 +140,19 @@ def test_array_object_variable():
|
||||
assert isinstance(variable, ArrayObjectVariable)
|
||||
assert isinstance(variable.value[0], dict)
|
||||
assert isinstance(variable.value[1], dict)
|
||||
assert isinstance(variable.value[0]['key1'], str)
|
||||
assert isinstance(variable.value[0]['key2'], int)
|
||||
assert isinstance(variable.value[1]['key1'], str)
|
||||
assert isinstance(variable.value[1]['key2'], int)
|
||||
assert isinstance(variable.value[0]["key1"], str)
|
||||
assert isinstance(variable.value[0]["key2"], int)
|
||||
assert isinstance(variable.value[1]["key1"], str)
|
||||
assert isinstance(variable.value[1]["key2"], int)
|
||||
|
||||
|
||||
def test_variable_cannot_large_than_5_kb():
|
||||
with pytest.raises(VariableError):
|
||||
factory.build_variable_from_mapping(
|
||||
{
|
||||
'id': str(uuid4()),
|
||||
'value_type': 'string',
|
||||
'name': 'test_text',
|
||||
'value': 'a' * 1024 * 6,
|
||||
"id": str(uuid4()),
|
||||
"value_type": "string",
|
||||
"name": "test_text",
|
||||
"value": "a" * 1024 * 6,
|
||||
}
|
||||
)
|
||||
|
||||
@@ -7,20 +7,20 @@ from core.workflow.enums import SystemVariableKey
|
||||
def test_segment_group_to_text():
|
||||
variable_pool = VariablePool(
|
||||
system_variables={
|
||||
SystemVariableKey('user_id'): 'fake-user-id',
|
||||
SystemVariableKey("user_id"): "fake-user-id",
|
||||
},
|
||||
user_inputs={},
|
||||
environment_variables=[
|
||||
SecretVariable(name='secret_key', value='fake-secret-key'),
|
||||
SecretVariable(name="secret_key", value="fake-secret-key"),
|
||||
],
|
||||
)
|
||||
variable_pool.add(('node_id', 'custom_query'), 'fake-user-query')
|
||||
variable_pool.add(("node_id", "custom_query"), "fake-user-query")
|
||||
template = (
|
||||
'Hello, {{#sys.user_id#}}! Your query is {{#node_id.custom_query#}}. And your key is {{#env.secret_key#}}.'
|
||||
"Hello, {{#sys.user_id#}}! Your query is {{#node_id.custom_query#}}. And your key is {{#env.secret_key#}}."
|
||||
)
|
||||
segments_group = parser.convert_template(template=template, variable_pool=variable_pool)
|
||||
|
||||
assert segments_group.text == 'Hello, fake-user-id! Your query is fake-user-query. And your key is fake-secret-key.'
|
||||
assert segments_group.text == "Hello, fake-user-id! Your query is fake-user-query. And your key is fake-secret-key."
|
||||
assert (
|
||||
segments_group.log
|
||||
== f"Hello, fake-user-id! Your query is fake-user-query. And your key is {encrypter.obfuscated_token('fake-secret-key')}."
|
||||
@@ -33,22 +33,22 @@ def test_convert_constant_to_segment_group():
|
||||
user_inputs={},
|
||||
environment_variables=[],
|
||||
)
|
||||
template = 'Hello, world!'
|
||||
template = "Hello, world!"
|
||||
segments_group = parser.convert_template(template=template, variable_pool=variable_pool)
|
||||
assert segments_group.text == 'Hello, world!'
|
||||
assert segments_group.log == 'Hello, world!'
|
||||
assert segments_group.text == "Hello, world!"
|
||||
assert segments_group.log == "Hello, world!"
|
||||
|
||||
|
||||
def test_convert_variable_to_segment_group():
|
||||
variable_pool = VariablePool(
|
||||
system_variables={
|
||||
SystemVariableKey('user_id'): 'fake-user-id',
|
||||
SystemVariableKey("user_id"): "fake-user-id",
|
||||
},
|
||||
user_inputs={},
|
||||
environment_variables=[],
|
||||
)
|
||||
template = '{{#sys.user_id#}}'
|
||||
template = "{{#sys.user_id#}}"
|
||||
segments_group = parser.convert_template(template=template, variable_pool=variable_pool)
|
||||
assert segments_group.text == 'fake-user-id'
|
||||
assert segments_group.log == 'fake-user-id'
|
||||
assert segments_group.value == [StringSegment(value='fake-user-id')]
|
||||
assert segments_group.text == "fake-user-id"
|
||||
assert segments_group.log == "fake-user-id"
|
||||
assert segments_group.value == [StringSegment(value="fake-user-id")]
|
||||
|
||||
@@ -13,60 +13,60 @@ from core.app.segments import (
|
||||
|
||||
|
||||
def test_frozen_variables():
|
||||
var = StringVariable(name='text', value='text')
|
||||
var = StringVariable(name="text", value="text")
|
||||
with pytest.raises(ValidationError):
|
||||
var.value = 'new value'
|
||||
var.value = "new value"
|
||||
|
||||
int_var = IntegerVariable(name='integer', value=42)
|
||||
int_var = IntegerVariable(name="integer", value=42)
|
||||
with pytest.raises(ValidationError):
|
||||
int_var.value = 100
|
||||
|
||||
float_var = FloatVariable(name='float', value=3.14)
|
||||
float_var = FloatVariable(name="float", value=3.14)
|
||||
with pytest.raises(ValidationError):
|
||||
float_var.value = 2.718
|
||||
|
||||
secret_var = SecretVariable(name='secret', value='secret_value')
|
||||
secret_var = SecretVariable(name="secret", value="secret_value")
|
||||
with pytest.raises(ValidationError):
|
||||
secret_var.value = 'new_secret_value'
|
||||
secret_var.value = "new_secret_value"
|
||||
|
||||
|
||||
def test_variable_value_type_immutable():
|
||||
with pytest.raises(ValidationError):
|
||||
StringVariable(value_type=SegmentType.ARRAY_ANY, name='text', value='text')
|
||||
StringVariable(value_type=SegmentType.ARRAY_ANY, name="text", value="text")
|
||||
|
||||
with pytest.raises(ValidationError):
|
||||
StringVariable.model_validate({'value_type': 'not text', 'name': 'text', 'value': 'text'})
|
||||
StringVariable.model_validate({"value_type": "not text", "name": "text", "value": "text"})
|
||||
|
||||
var = IntegerVariable(name='integer', value=42)
|
||||
var = IntegerVariable(name="integer", value=42)
|
||||
with pytest.raises(ValidationError):
|
||||
IntegerVariable(value_type=SegmentType.ARRAY_ANY, name=var.name, value=var.value)
|
||||
|
||||
var = FloatVariable(name='float', value=3.14)
|
||||
var = FloatVariable(name="float", value=3.14)
|
||||
with pytest.raises(ValidationError):
|
||||
FloatVariable(value_type=SegmentType.ARRAY_ANY, name=var.name, value=var.value)
|
||||
|
||||
var = SecretVariable(name='secret', value='secret_value')
|
||||
var = SecretVariable(name="secret", value="secret_value")
|
||||
with pytest.raises(ValidationError):
|
||||
SecretVariable(value_type=SegmentType.ARRAY_ANY, name=var.name, value=var.value)
|
||||
|
||||
|
||||
def test_object_variable_to_object():
|
||||
var = ObjectVariable(
|
||||
name='object',
|
||||
name="object",
|
||||
value={
|
||||
'key1': {
|
||||
'key2': 'value2',
|
||||
"key1": {
|
||||
"key2": "value2",
|
||||
},
|
||||
'key2': ['value5_1', 42, {}],
|
||||
"key2": ["value5_1", 42, {}],
|
||||
},
|
||||
)
|
||||
|
||||
assert var.to_object() == {
|
||||
'key1': {
|
||||
'key2': 'value2',
|
||||
"key1": {
|
||||
"key2": "value2",
|
||||
},
|
||||
'key2': [
|
||||
'value5_1',
|
||||
"key2": [
|
||||
"value5_1",
|
||||
42,
|
||||
{},
|
||||
],
|
||||
@@ -74,11 +74,11 @@ def test_object_variable_to_object():
|
||||
|
||||
|
||||
def test_variable_to_object():
|
||||
var = StringVariable(name='text', value='text')
|
||||
assert var.to_object() == 'text'
|
||||
var = IntegerVariable(name='integer', value=42)
|
||||
var = StringVariable(name="text", value="text")
|
||||
assert var.to_object() == "text"
|
||||
var = IntegerVariable(name="integer", value=42)
|
||||
assert var.to_object() == 42
|
||||
var = FloatVariable(name='float', value=3.14)
|
||||
var = FloatVariable(name="float", value=3.14)
|
||||
assert var.to_object() == 3.14
|
||||
var = SecretVariable(name='secret', value='secret_value')
|
||||
assert var.to_object() == 'secret_value'
|
||||
var = SecretVariable(name="secret", value="secret_value")
|
||||
assert var.to_object() == "secret_value"
|
||||
|
||||
Reference in New Issue
Block a user