Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

test_boolean_variable_assigner.py 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. #!/usr/bin/env python3
  2. """
  3. Test script to verify boolean support in VariableAssigner node
  4. """
  5. import sys
  6. import os
  7. # Add the api directory to the Python path
  8. sys.path.insert(0, os.path.join(os.path.dirname(__file__), "api"))
  9. from core.variables import SegmentType
  10. from core.workflow.nodes.variable_assigner.v2.helpers import (
  11. is_operation_supported,
  12. is_constant_input_supported,
  13. is_input_value_valid,
  14. )
  15. from core.workflow.nodes.variable_assigner.v2.enums import Operation
  16. from core.workflow.nodes.variable_assigner.v2.constants import EMPTY_VALUE_MAPPING
  17. def test_boolean_operation_support():
  18. """Test that boolean types support the correct operations"""
  19. print("Testing boolean operation support...")
  20. # Boolean should support SET, OVER_WRITE, and CLEAR
  21. assert is_operation_supported(
  22. variable_type=SegmentType.BOOLEAN, operation=Operation.SET
  23. )
  24. assert is_operation_supported(
  25. variable_type=SegmentType.BOOLEAN, operation=Operation.OVER_WRITE
  26. )
  27. assert is_operation_supported(
  28. variable_type=SegmentType.BOOLEAN, operation=Operation.CLEAR
  29. )
  30. # Boolean should NOT support arithmetic operations
  31. assert not is_operation_supported(
  32. variable_type=SegmentType.BOOLEAN, operation=Operation.ADD
  33. )
  34. assert not is_operation_supported(
  35. variable_type=SegmentType.BOOLEAN, operation=Operation.SUBTRACT
  36. )
  37. assert not is_operation_supported(
  38. variable_type=SegmentType.BOOLEAN, operation=Operation.MULTIPLY
  39. )
  40. assert not is_operation_supported(
  41. variable_type=SegmentType.BOOLEAN, operation=Operation.DIVIDE
  42. )
  43. # Boolean should NOT support array operations
  44. assert not is_operation_supported(
  45. variable_type=SegmentType.BOOLEAN, operation=Operation.APPEND
  46. )
  47. assert not is_operation_supported(
  48. variable_type=SegmentType.BOOLEAN, operation=Operation.EXTEND
  49. )
  50. print("✓ Boolean operation support tests passed")
  51. def test_array_boolean_operation_support():
  52. """Test that array boolean types support the correct operations"""
  53. print("Testing array boolean operation support...")
  54. # Array boolean should support APPEND, EXTEND, SET, OVER_WRITE, CLEAR
  55. assert is_operation_supported(
  56. variable_type=SegmentType.ARRAY_BOOLEAN, operation=Operation.APPEND
  57. )
  58. assert is_operation_supported(
  59. variable_type=SegmentType.ARRAY_BOOLEAN, operation=Operation.EXTEND
  60. )
  61. assert is_operation_supported(
  62. variable_type=SegmentType.ARRAY_BOOLEAN, operation=Operation.OVER_WRITE
  63. )
  64. assert is_operation_supported(
  65. variable_type=SegmentType.ARRAY_BOOLEAN, operation=Operation.CLEAR
  66. )
  67. assert is_operation_supported(
  68. variable_type=SegmentType.ARRAY_BOOLEAN, operation=Operation.REMOVE_FIRST
  69. )
  70. assert is_operation_supported(
  71. variable_type=SegmentType.ARRAY_BOOLEAN, operation=Operation.REMOVE_LAST
  72. )
  73. # Array boolean should NOT support arithmetic operations
  74. assert not is_operation_supported(
  75. variable_type=SegmentType.ARRAY_BOOLEAN, operation=Operation.ADD
  76. )
  77. assert not is_operation_supported(
  78. variable_type=SegmentType.ARRAY_BOOLEAN, operation=Operation.SUBTRACT
  79. )
  80. assert not is_operation_supported(
  81. variable_type=SegmentType.ARRAY_BOOLEAN, operation=Operation.MULTIPLY
  82. )
  83. assert not is_operation_supported(
  84. variable_type=SegmentType.ARRAY_BOOLEAN, operation=Operation.DIVIDE
  85. )
  86. print("✓ Array boolean operation support tests passed")
  87. def test_boolean_constant_input_support():
  88. """Test that boolean types support constant input for correct operations"""
  89. print("Testing boolean constant input support...")
  90. # Boolean should support constant input for SET and OVER_WRITE
  91. assert is_constant_input_supported(
  92. variable_type=SegmentType.BOOLEAN, operation=Operation.SET
  93. )
  94. assert is_constant_input_supported(
  95. variable_type=SegmentType.BOOLEAN, operation=Operation.OVER_WRITE
  96. )
  97. # Boolean should NOT support constant input for arithmetic operations
  98. assert not is_constant_input_supported(
  99. variable_type=SegmentType.BOOLEAN, operation=Operation.ADD
  100. )
  101. print("✓ Boolean constant input support tests passed")
  102. def test_boolean_input_validation():
  103. """Test that boolean input validation works correctly"""
  104. print("Testing boolean input validation...")
  105. # Boolean values should be valid for boolean type
  106. assert is_input_value_valid(
  107. variable_type=SegmentType.BOOLEAN, operation=Operation.SET, value=True
  108. )
  109. assert is_input_value_valid(
  110. variable_type=SegmentType.BOOLEAN, operation=Operation.SET, value=False
  111. )
  112. assert is_input_value_valid(
  113. variable_type=SegmentType.BOOLEAN, operation=Operation.OVER_WRITE, value=True
  114. )
  115. # Non-boolean values should be invalid for boolean type
  116. assert not is_input_value_valid(
  117. variable_type=SegmentType.BOOLEAN, operation=Operation.SET, value="true"
  118. )
  119. assert not is_input_value_valid(
  120. variable_type=SegmentType.BOOLEAN, operation=Operation.SET, value=1
  121. )
  122. assert not is_input_value_valid(
  123. variable_type=SegmentType.BOOLEAN, operation=Operation.SET, value=0
  124. )
  125. print("✓ Boolean input validation tests passed")
  126. def test_array_boolean_input_validation():
  127. """Test that array boolean input validation works correctly"""
  128. print("Testing array boolean input validation...")
  129. # Boolean values should be valid for array boolean append
  130. assert is_input_value_valid(
  131. variable_type=SegmentType.ARRAY_BOOLEAN, operation=Operation.APPEND, value=True
  132. )
  133. assert is_input_value_valid(
  134. variable_type=SegmentType.ARRAY_BOOLEAN, operation=Operation.APPEND, value=False
  135. )
  136. # Boolean arrays should be valid for extend/overwrite
  137. assert is_input_value_valid(
  138. variable_type=SegmentType.ARRAY_BOOLEAN,
  139. operation=Operation.EXTEND,
  140. value=[True, False, True],
  141. )
  142. assert is_input_value_valid(
  143. variable_type=SegmentType.ARRAY_BOOLEAN,
  144. operation=Operation.OVER_WRITE,
  145. value=[False, False],
  146. )
  147. # Non-boolean values should be invalid
  148. assert not is_input_value_valid(
  149. variable_type=SegmentType.ARRAY_BOOLEAN,
  150. operation=Operation.APPEND,
  151. value="true",
  152. )
  153. assert not is_input_value_valid(
  154. variable_type=SegmentType.ARRAY_BOOLEAN,
  155. operation=Operation.EXTEND,
  156. value=[True, "false"],
  157. )
  158. print("✓ Array boolean input validation tests passed")
  159. def test_empty_value_mapping():
  160. """Test that empty value mapping includes boolean types"""
  161. print("Testing empty value mapping...")
  162. # Check that boolean types have correct empty values
  163. assert SegmentType.BOOLEAN in EMPTY_VALUE_MAPPING
  164. assert EMPTY_VALUE_MAPPING[SegmentType.BOOLEAN] is False
  165. assert SegmentType.ARRAY_BOOLEAN in EMPTY_VALUE_MAPPING
  166. assert EMPTY_VALUE_MAPPING[SegmentType.ARRAY_BOOLEAN] == []
  167. print("✓ Empty value mapping tests passed")
  168. def main():
  169. """Run all tests"""
  170. print("Running VariableAssigner boolean support tests...\n")
  171. try:
  172. test_boolean_operation_support()
  173. test_array_boolean_operation_support()
  174. test_boolean_constant_input_support()
  175. test_boolean_input_validation()
  176. test_array_boolean_input_validation()
  177. test_empty_value_mapping()
  178. print(
  179. "\n🎉 All tests passed! Boolean support has been successfully added to VariableAssigner."
  180. )
  181. except Exception as e:
  182. print(f"\n❌ Test failed: {e}")
  183. import traceback
  184. traceback.print_exc()
  185. sys.exit(1)
  186. if __name__ == "__main__":
  187. main()