mock_request.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. # -*- coding:utf-8 -*-
  2. #
  3. import requests
  4. import time
  5. import json
  6. from functools import wraps
  7. product_adress = "http://111.231.167.191"
  8. test_address = "http://111.231.167.191:8004"
  9. test_address2 = "http://111.231.167.191:8003"
  10. local_adress = "http://127.0.0.1:9000"
  11. use_address = test_address
  12. def time_use(fn):
  13. @wraps(fn)
  14. def cc(*args, **kwargs):
  15. f_time = time.time()
  16. res = fn(*args, **kwargs)
  17. cha = round(time.time() - f_time, 3)
  18. if cha > 0.1:
  19. print(f'函数:{fn.__name__} 一共用时', cha, '秒')
  20. return res
  21. return cc
  22. def test_connect():
  23. """连接测试"""
  24. try:
  25. r = requests.post(use_address)
  26. assert r.text == 'hello world'
  27. r = requests.get(use_address)
  28. assert r.text == 'hello world'
  29. print("测试连接成功")
  30. except AssertionError:
  31. print("测试连接失败")
  32. def get_article():
  33. """同时检验article接口和query_oss_file接口"""
  34. json_data = {"meaning_ids": [300751, 300756, 300757, 300758, 300454, 302096, 302102], "callback_url": "http://localhost/callback",
  35. "student_stage": 1, "vocabulary": 700, "class_id": 123456}
  36. r = requests.post(f"{use_address}/article", json=json_data)
  37. key = r.json()['key']
  38. time.sleep(120)
  39. query_file_content(key)
  40. def query_file_content(key):
  41. json_data = {"key": key}
  42. try:
  43. r = requests.post(f"{use_address}/query_oss_file", json=json_data)
  44. r.raise_for_status()
  45. response_data = r.json()
  46. assert response_data['wordCount'] > 0, "词数为0"
  47. except requests.RequestException as e:
  48. print(f"请求失败: {e}")
  49. except json.JSONDecodeError as e:
  50. print(f"JSON解析错误: {e}")
  51. except AssertionError as e:
  52. print(f"断言错误: {e}")
  53. except Exception as e:
  54. print(f"未知错误: {e}")
  55. def get_audio():
  56. word = "cat"
  57. r1 = requests.post(f"{use_address}/tts", json={"text": word, "type": 0})
  58. r2 = requests.post(f"{use_address}/tts", json={"text": word, "type": 2})
  59. r3 = requests.post(f"{use_address}/tts", json={"text": word, "type": 1})
  60. assert r1.json()['code'] == 200
  61. assert r2.json()['code'] == 200
  62. assert r3.status_code == 200
  63. @time_use
  64. def get_article2():
  65. json_data = {"words": [
  66. {'spell': 'term', 'meaning': '学期'}, {'spell': 'ordinary', 'meaning': '平常的, 普通的, 一般的'},
  67. {'spell': 'discussion', 'meaning': '讨论, 谈论'}, {'spell': 'shine', 'meaning': '照耀, 发光, 闪耀'},
  68. {'spell': 'million', 'meaning': '百万'}, {'spell': 'greet', 'meaning': '问候'}
  69. ], "take_count": 1, "student_stage": 3, "demo_name": "测试项目"}
  70. r = requests.post(f"{use_address}/article/reading-comprehension", json=json_data)
  71. r_json = r.json()
  72. try:
  73. assert len(r_json['articles']) == 1
  74. return r_json
  75. except Exception as e:
  76. print("春笋文章reading-comprehension错误", e)
  77. print("错误数据", r_json)
  78. @time_use
  79. def get_article2_1():
  80. """新的获取文章"""
  81. json_data = {'core_words': [{'spell': 'sudden', 'meaning': '突然的, 意外的', 'word_id': 1114468, 'meaning_id': 1734},
  82. {'spell': 'frighten', 'meaning': '惊吓, 惊恐', 'word_id': 899278, 'meaning_id': 1735},
  83. {'spell': 'relation', 'meaning': '关系, 联系, 亲戚, 亲属', 'word_id': 1061800, 'meaning_id': 1736},
  84. {'spell': 'agreement', 'meaning': '协议,协定', 'word_id': 753401, 'meaning_id': 1743},
  85. {'spell': 'pool', 'meaning': '游泳池, 池子', 'word_id': 1035634, 'meaning_id': 1747},
  86. {'spell': 'risk', 'meaning': '冒险, 风险', 'word_id': 1069002, 'meaning_id': 1748},
  87. {'spell': 'centre', 'meaning': '中心', 'word_id': 806629, 'meaning_id': 1749},
  88. {'spell': 'shut', 'meaning': '关上, 关闭', 'word_id': 1088662, 'meaning_id': 1751},
  89. {'spell': 'piano', 'meaning': '钢琴', 'word_id': 1027211, 'meaning_id': 1752},
  90. {'spell': 'trust', 'meaning': '信任, 信赖', 'word_id': 1142977, 'meaning_id': 1753},
  91. {'spell': 'camera', 'meaning': '照相机', 'word_id': 799656, 'meaning_id': 1754},
  92. {'spell': 'course', 'meaning': '课程', 'word_id': 834016, 'meaning_id': 399},
  93. {'spell': 'thought', 'meaning': '思想, 想法', 'word_id': 1130826, 'meaning_id': 685}],
  94. 'take_count': 4, 'student_stage': 3, 'demo_name': '春笋英语',"exercise_id":999,"article_length":200}
  95. r = requests.post(f"{use_address}/article/reading-comprehension", json=json_data)
  96. r_json = r.json()
  97. try:
  98. return r_json
  99. except Exception as e:
  100. print("春笋文章reading-comprehension错误", e)
  101. print("错误数据", r_json)
  102. def download_word():
  103. from make_docx_demo.data import test_json2
  104. params = {"document_format": 2, "scanpage_format": 1}
  105. r = requests.post(f"{use_address}/make_word/vocabulary_assault", params=params, json=test_json2)
  106. r.raise_for_status()
  107. suffix = {1: "docx", 2: "pdf"}[params['document_format']]
  108. with open(f"test.{suffix}", "wb") as f:
  109. f.write(r.content)
  110. def spoken_language():
  111. url = f"{use_address}/spoken_language"
  112. with open(r"1.mp3", "rb") as f:
  113. audio_data = {'file': ('file.bin', f, 'application/octet-stream')}
  114. data = {"text": "You must study to be frank with the world"}
  115. r1 = requests.post(url, data=data, files=audio_data)
  116. assert r1.json()['code'] == 0
  117. data = {"text": "apple Life is like a box of chocolates, you never know what you're gonna",
  118. "url": "https://yunzhixue.blob.core.chinacloudapi.cn/download/ttsmaker-file-2025-1-13-10-47-47.mp3"}
  119. r2 = requests.post(url, data=data)
  120. assert r2.json()['code'] == 0
  121. @time_use
  122. def get_article3():
  123. json_data = {"words": [
  124. {'spell': 'term', 'meaning': '学期'}, {'spell': 'ordinary', 'meaning': '平常的, 普通的, 一般的'},
  125. {'spell': 'discussion', 'meaning': '讨论, 谈论'}, {'spell': 'shine', 'meaning': '照耀, 发光, 闪耀'},
  126. {'spell': 'million', 'meaning': '百万'}, {'spell': 'greet', 'meaning': '问候'}
  127. ], "take_count": 1, "student_stage": 3, "demo_name": "测试项目"}
  128. r = requests.post(f"{use_address}/article/reading-comprehension/deepseek", json=json_data)
  129. r_json = r.json()
  130. try:
  131. assert r_json['articles'][0]["english"]
  132. assert r_json['articles'][0]["difficultSentences"]
  133. assert r_json['articles'][0]["questions"]
  134. assert r_json['articles'][0]["allWordAmount"] > 10
  135. return r_json
  136. except Exception as e:
  137. print("deepseek生成文章出错", e)
  138. print(r_json)
  139. @time_use
  140. def run_all_test_cese():
  141. test_connect()
  142. get_audio()
  143. spoken_language()
  144. download_word()
  145. print(get_article2_1())
  146. if __name__ == '__main__':
  147. run_all_test_cese()