mock_request.py 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. local_adress = "http://127.0.0.1:9000"
  10. use_address = local_adress
  11. def time_use(fn):
  12. @wraps(fn)
  13. def cc(*args, **kwargs):
  14. f_time = time.time()
  15. res = fn(*args, **kwargs)
  16. cha = round(time.time() - f_time, 3)
  17. if cha > 0.1:
  18. print(f'函数:{fn.__name__} 一共用时', cha, '秒')
  19. return res
  20. return cc
  21. def test_connect():
  22. """连接测试"""
  23. try:
  24. r = requests.post(use_address)
  25. assert r.text == 'hello world'
  26. r = requests.get(use_address)
  27. assert r.text == 'hello world'
  28. print("测试连接成功")
  29. except AssertionError:
  30. print("测试连接失败")
  31. def get_article():
  32. """同时检验article接口和query_oss_file接口"""
  33. json_data = {"meaning_ids": [300751, 300756, 300757, 300758, 300454, 302096, 302102], "callback_url": "http://localhost/callback",
  34. "student_stage": 1, "vocabulary": 700, "class_id": 123456}
  35. r = requests.post(f"{use_address}/article", json=json_data)
  36. key = r.json()['key']
  37. time.sleep(120)
  38. query_file_content(key)
  39. def query_file_content(key):
  40. json_data = {"key": key}
  41. try:
  42. r = requests.post(f"{use_address}/query_oss_file", json=json_data)
  43. r.raise_for_status()
  44. response_data = r.json()
  45. assert response_data['wordCount'] > 0, "词数为0"
  46. except requests.RequestException as e:
  47. print(f"请求失败: {e}")
  48. except json.JSONDecodeError as e:
  49. print(f"JSON解析错误: {e}")
  50. except AssertionError as e:
  51. print(f"断言错误: {e}")
  52. except Exception as e:
  53. print(f"未知错误: {e}")
  54. def get_audio():
  55. word = "cat"
  56. r1 = requests.post(f"{use_address}/tts", json={"text": word, "type": 0})
  57. r2 = requests.post(f"{use_address}/tts", json={"text": word, "type": 2})
  58. r3 = requests.post(f"{use_address}/tts", json={"text": word, "type": 1})
  59. assert r1.json()['code'] == 200
  60. assert r2.json()['code'] == 200
  61. assert r3.status_code == 200
  62. @time_use
  63. def get_article2():
  64. json_data = {"words": [
  65. {'spell': 'term', 'meaning': '学期'}, {'spell': 'ordinary', 'meaning': '平常的, 普通的, 一般的'},
  66. {'spell': 'discussion', 'meaning': '讨论, 谈论'}, {'spell': 'shine', 'meaning': '照耀, 发光, 闪耀'},
  67. {'spell': 'million', 'meaning': '百万'}, {'spell': 'greet', 'meaning': '问候'}
  68. ], "take_count": 1, "student_stage": 3, "demo_name": "测试项目"}
  69. r = requests.post(f"{use_address}/article/reading-comprehension", json=json_data)
  70. r_json = r.json()
  71. try:
  72. assert len(r_json['articles']) == 1
  73. return r_json
  74. except Exception as e:
  75. print("春笋文章reading-comprehension错误", e)
  76. print("错误数据", r_json)
  77. @time_use
  78. def get_article2_1():
  79. """新的获取文章"""
  80. json_data = {'core_words': [{'spell': 'sudden', 'meaning': '突然的, 意外的', 'word_id': 1114468, 'meaning_id': 1734},
  81. {'spell': 'frighten', 'meaning': '惊吓, 惊恐', 'word_id': 899278, 'meaning_id': 1735},
  82. {'spell': 'relation', 'meaning': '关系, 联系, 亲戚, 亲属', 'word_id': 1061800, 'meaning_id': 1736},
  83. {'spell': 'Japanese', 'meaning': '日本的', 'word_id': 727384, 'meaning_id': 1737},
  84. {'spell': 'trick', 'meaning': '恶作剧, 戏法, 作假, 欺骗', 'word_id': 1140881, 'meaning_id': 1740},
  85. {'spell': 'yours', 'meaning': '你(们)的东西', 'word_id': 1169496, 'meaning_id': 1741},
  86. {'spell': 'panda', 'meaning': '熊猫', 'word_id': 1015908, 'meaning_id': 1742},
  87. {'spell': 'agreement', 'meaning': '协议,协定', 'word_id': 753401, 'meaning_id': 1743},
  88. {'spell': 'pool', 'meaning': '游泳池, 池子', 'word_id': 1035634, 'meaning_id': 1747},
  89. {'spell': 'risk', 'meaning': '冒险, 风险', 'word_id': 1069002, 'meaning_id': 1748},
  90. {'spell': 'centre', 'meaning': '中心', 'word_id': 806629, 'meaning_id': 1749},
  91. {'spell': 'shut', 'meaning': '关上, 关闭', 'word_id': 1088662, 'meaning_id': 1751},
  92. {'spell': 'piano', 'meaning': '钢琴', 'word_id': 1027211, 'meaning_id': 1752},
  93. {'spell': 'trust', 'meaning': '信任, 信赖', 'word_id': 1142977, 'meaning_id': 1753},
  94. {'spell': 'camera', 'meaning': '照相机', 'word_id': 799656, 'meaning_id': 1754},
  95. {'spell': 'course', 'meaning': '课程', 'word_id': 834016, 'meaning_id': 399},
  96. {'spell': 'carry', 'meaning': '携带', 'word_id': 803106, 'meaning_id': 460},
  97. {'spell': 'sometimes', 'meaning': '有时, 间或', 'word_id': 1097431, 'meaning_id': 495},
  98. {'spell': 'interesting', 'meaning': '有趣的, 令人感兴趣的', 'word_id': 944231, 'meaning_id': 600},
  99. {'spell': 'thought', 'meaning': '思想, 想法', 'word_id': 1130826, 'meaning_id': 685}],
  100. 'extend_words': [{'spell': 'destroy', 'meaning': '破坏, 摧毁', 'word_id': 848592, 'meaning_id': 1288},
  101. {'spell': 'project', 'meaning': '放映, 展现', 'word_id': 1044528, 'meaning_id': 1290},
  102. {'spell': 'waste', 'meaning': '浪费, 荒芜, 废物', 'word_id': 1160701, 'meaning_id': 1292},
  103. {'spell': 'environment', 'meaning': '环境, 外界', 'word_id': 873514, 'meaning_id': 1293},
  104. {'spell': 'memory', 'meaning': '记忆, 记忆力, 回忆', 'word_id': 981104, 'meaning_id': 1294}],
  105. 'take_count': 1, 'student_stage': 3, 'demo_name': '春笋英语',"article_difficulty":800}
  106. r = requests.post(f"{use_address}/article/reading-comprehension", json=json_data)
  107. r_json = r.json()
  108. try:
  109. return r_json
  110. except Exception as e:
  111. print("春笋文章reading-comprehension错误", e)
  112. print("错误数据", r_json)
  113. def download_word():
  114. from make_docx_demo.data import test_json1
  115. params = {"document_format": 2, "scanpage_format": 1}
  116. r = requests.post(f"{use_address}/make_word/vocabulary_assault", params=params, json=test_json1)
  117. r.raise_for_status()
  118. suffix = {1: "docx", 2: "pdf"}[params['document_format']]
  119. with open(f"test.{suffix}", "wb") as f:
  120. f.write(r.content)
  121. def spoken_language():
  122. url = f"{use_address}/spoken_language"
  123. with open(r"1.mp3", "rb") as f:
  124. audio_data = {'file': ('file.bin', f, 'application/octet-stream')}
  125. data = {"text": "You must study to be frank with the world"}
  126. r1 = requests.post(url, data=data, files=audio_data)
  127. assert r1.json()['code'] == 0
  128. data = {"text": "apple Life is like a box of chocolates, you never know what you're gonna",
  129. "url": "https://yunzhixue.blob.core.chinacloudapi.cn/download/ttsmaker-file-2025-1-13-10-47-47.mp3"}
  130. r2 = requests.post(url, data=data)
  131. assert r2.json()['code'] == 0
  132. @time_use
  133. def get_article3():
  134. json_data = {"words": [
  135. {'spell': 'term', 'meaning': '学期'}, {'spell': 'ordinary', 'meaning': '平常的, 普通的, 一般的'},
  136. {'spell': 'discussion', 'meaning': '讨论, 谈论'}, {'spell': 'shine', 'meaning': '照耀, 发光, 闪耀'},
  137. {'spell': 'million', 'meaning': '百万'}, {'spell': 'greet', 'meaning': '问候'}
  138. ], "take_count": 1, "student_stage": 3, "demo_name": "测试项目"}
  139. r = requests.post(f"{use_address}/article/reading-comprehension/deepseek", json=json_data)
  140. r_json = r.json()
  141. try:
  142. assert r_json['articles'][0]["english"]
  143. assert r_json['articles'][0]["difficultSentences"]
  144. assert r_json['articles'][0]["questions"]
  145. assert r_json['articles'][0]["allWordAmount"] > 10
  146. return r_json
  147. except Exception as e:
  148. print("deepseek生成文章出错", e)
  149. print(r_json)
  150. @time_use
  151. def run_all_test_cese():
  152. test_connect()
  153. get_audio()
  154. spoken_language()
  155. download_word()
  156. print(get_article2_1())
  157. if __name__ == '__main__':
  158. print(get_article2_1())