瀏覽代碼

Fix: incorrect array element validation in SegmentType (#23289)

tags/1.7.2
Yongtao Huang 3 月之前
父節點
當前提交
be914438a5
No account linked to committer's email address
共有 2 個文件被更改,包括 27 次插入4 次删除
  1. 2
    2
      api/core/variables/types.py
  2. 25
    2
      api/tests/unit_tests/core/variables/test_segment_type.py

+ 2
- 2
api/core/variables/types.py 查看文件

elif array_validation == ArrayValidation.FIRST: elif array_validation == ArrayValidation.FIRST:
return element_type.is_valid(value[0]) return element_type.is_valid(value[0])
else: else:
return all([element_type.is_valid(i, array_validation=ArrayValidation.NONE)] for i in value)
return all(element_type.is_valid(i, array_validation=ArrayValidation.NONE) for i in value)


def is_valid(self, value: Any, array_validation: ArrayValidation = ArrayValidation.FIRST) -> bool: def is_valid(self, value: Any, array_validation: ArrayValidation = ArrayValidation.FIRST) -> bool:
""" """




_ARRAY_ELEMENT_TYPES_MAPPING: Mapping[SegmentType, SegmentType] = { _ARRAY_ELEMENT_TYPES_MAPPING: Mapping[SegmentType, SegmentType] = {
# ARRAY_ANY does not have correpond element type.
# ARRAY_ANY does not have corresponding element type.
SegmentType.ARRAY_STRING: SegmentType.STRING, SegmentType.ARRAY_STRING: SegmentType.STRING,
SegmentType.ARRAY_NUMBER: SegmentType.NUMBER, SegmentType.ARRAY_NUMBER: SegmentType.NUMBER,
SegmentType.ARRAY_OBJECT: SegmentType.OBJECT, SegmentType.ARRAY_OBJECT: SegmentType.OBJECT,

+ 25
- 2
api/tests/unit_tests/core/variables/test_segment_type.py 查看文件

from core.variables.types import SegmentType
from core.variables.types import ArrayValidation, SegmentType




class TestSegmentTypeIsArrayType: class TestSegmentTypeIsArrayType:
value is tested for the is_array_type method. value is tested for the is_array_type method.
""" """
# Arrange # Arrange
all_segment_types = set(SegmentType)
expected_array_types = [ expected_array_types = [
SegmentType.ARRAY_ANY, SegmentType.ARRAY_ANY,
SegmentType.ARRAY_STRING, SegmentType.ARRAY_STRING,
for seg_type in enum_values: for seg_type in enum_values:
is_array = seg_type.is_array_type() is_array = seg_type.is_array_type()
assert isinstance(is_array, bool), f"is_array_type does not return a boolean for segment type {seg_type}" assert isinstance(is_array, bool), f"is_array_type does not return a boolean for segment type {seg_type}"


class TestSegmentTypeIsValidArrayValidation:
"""
Test SegmentType.is_valid with array types using different validation strategies.
"""

def test_array_validation_all_success(self):
value = ["hello", "world", "foo"]
assert SegmentType.ARRAY_STRING.is_valid(value, array_validation=ArrayValidation.ALL)

def test_array_validation_all_fail(self):
value = ["hello", 123, "world"]
# Should return False, since 123 is not a string
assert not SegmentType.ARRAY_STRING.is_valid(value, array_validation=ArrayValidation.ALL)

def test_array_validation_first(self):
value = ["hello", 123, None]
assert SegmentType.ARRAY_STRING.is_valid(value, array_validation=ArrayValidation.FIRST)

def test_array_validation_none(self):
value = [1, 2, 3]
# validation is None, skip
assert SegmentType.ARRAY_STRING.is_valid(value, array_validation=ArrayValidation.NONE)

Loading…
取消
儲存