test_segment.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. from core.app.segments import SecretVariable, parser
  2. from core.helper import encrypter
  3. from core.workflow.entities.node_entities import SystemVariable
  4. from core.workflow.entities.variable_pool import VariablePool
  5. def test_segment_group_to_text():
  6. variable_pool = VariablePool(
  7. system_variables={
  8. SystemVariable('user_id'): 'fake-user-id',
  9. },
  10. user_inputs={},
  11. environment_variables=[
  12. SecretVariable(name='secret_key', value='fake-secret-key'),
  13. ],
  14. )
  15. variable_pool.add(('node_id', 'custom_query'), 'fake-user-query')
  16. template = (
  17. 'Hello, {{#sys.user_id#}}! Your query is {{#node_id.custom_query#}}. And your key is {{#env.secret_key#}}.'
  18. )
  19. segments_group = parser.convert_template(template=template, variable_pool=variable_pool)
  20. assert segments_group.text == 'Hello, fake-user-id! Your query is fake-user-query. And your key is fake-secret-key.'
  21. assert (
  22. segments_group.log
  23. == f"Hello, fake-user-id! Your query is fake-user-query. And your key is {encrypter.obfuscated_token('fake-secret-key')}."
  24. )
  25. def test_convert_constant_to_segment_group():
  26. variable_pool = VariablePool(
  27. system_variables={},
  28. user_inputs={},
  29. environment_variables=[],
  30. )
  31. template = 'Hello, world!'
  32. segments_group = parser.convert_template(template=template, variable_pool=variable_pool)
  33. assert segments_group.text == 'Hello, world!'
  34. assert segments_group.log == 'Hello, world!'
  35. def test_convert_variable_to_segment_group():
  36. variable_pool = VariablePool(
  37. system_variables={
  38. SystemVariable('user_id'): 'fake-user-id',
  39. },
  40. user_inputs={},
  41. environment_variables=[],
  42. )
  43. template = '{{#sys.user_id#}}'
  44. segments_group = parser.convert_template(template=template, variable_pool=variable_pool)
  45. assert segments_group.text == 'fake-user-id'
  46. assert segments_group.log == 'fake-user-id'