test_preview_api.py 957 B

1234567891011121314151617181920212223242526272829303132
  1. """测试预览API"""
  2. import requests
  3. # 测试简单模板
  4. body1 = """{% set greeting = ['嗨', '哈喽'] %}
  5. {{ greeting | random }},小优来啦!"""
  6. print("测试1: 简单模板")
  7. r = requests.post('http://localhost:8000/api/templates/preview', json={
  8. 'template_body': body1
  9. })
  10. print(r.json())
  11. print()
  12. # 测试完整x11模板
  13. body2 = """{% set program_name_map = {'1': '棉麻'} %}
  14. {% set program_id = program_id | string | trim %}
  15. {% set program_name = program_name_map.get(program_id) %}
  16. {% if program_name %}
  17. {{ period }}好呀,为你推荐{{ program_name }}程序!
  18. {% else %}
  19. 程序不存在
  20. {% endif %}"""
  21. print("测试2: 带变量的模板")
  22. r = requests.post('http://localhost:8000/api/templates/preview', json={
  23. 'template_body': body2
  24. })
  25. result = r.json()
  26. print(f"成功: {result['success']}")
  27. print(f"结果: {result['result']}")
  28. print(f"使用的变量: {list(result['context_used'].keys())}")