mock_request.py 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 = local_adress
  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': 'Japanese', 'meaning': '日本的', 'word_id': 727384, 'meaning_id': 1737},
  85. {'spell': 'trick', 'meaning': '恶作剧, 戏法, 作假, 欺骗', 'word_id': 1140881, 'meaning_id': 1740},
  86. {'spell': 'yours', 'meaning': '你(们)的东西', 'word_id': 1169496, 'meaning_id': 1741},
  87. {'spell': 'panda', 'meaning': '熊猫', 'word_id': 1015908, 'meaning_id': 1742},
  88. {'spell': 'agreement', 'meaning': '协议,协定', 'word_id': 753401, 'meaning_id': 1743},
  89. {'spell': 'pool', 'meaning': '游泳池, 池子', 'word_id': 1035634, 'meaning_id': 1747},
  90. {'spell': 'risk', 'meaning': '冒险, 风险', 'word_id': 1069002, 'meaning_id': 1748},
  91. {'spell': 'centre', 'meaning': '中心', 'word_id': 806629, 'meaning_id': 1749},
  92. {'spell': 'shut', 'meaning': '关上, 关闭', 'word_id': 1088662, 'meaning_id': 1751},
  93. {'spell': 'piano', 'meaning': '钢琴', 'word_id': 1027211, 'meaning_id': 1752},
  94. {'spell': 'trust', 'meaning': '信任, 信赖', 'word_id': 1142977, 'meaning_id': 1753},
  95. {'spell': 'camera', 'meaning': '照相机', 'word_id': 799656, 'meaning_id': 1754},
  96. {'spell': 'course', 'meaning': '课程', 'word_id': 834016, 'meaning_id': 399},
  97. {'spell': 'carry', 'meaning': '携带', 'word_id': 803106, 'meaning_id': 460},
  98. {'spell': 'sometimes', 'meaning': '有时, 间或', 'word_id': 1097431, 'meaning_id': 495},
  99. {'spell': 'interesting', 'meaning': '有趣的, 令人感兴趣的', 'word_id': 944231, 'meaning_id': 600},
  100. {'spell': 'thought', 'meaning': '思想, 想法', 'word_id': 1130826, 'meaning_id': 685}],
  101. 'extend_words': [{'spell': 'destroy', 'meaning': '破坏, 摧毁', 'word_id': 848592, 'meaning_id': 1288},
  102. {'spell': 'project', 'meaning': '放映, 展现', 'word_id': 1044528, 'meaning_id': 1290},
  103. {'spell': 'waste', 'meaning': '浪费, 荒芜, 废物', 'word_id': 1160701, 'meaning_id': 1292},
  104. {'spell': 'environment', 'meaning': '环境, 外界', 'word_id': 873514, 'meaning_id': 1293},
  105. {'spell': 'memory', 'meaning': '记忆, 记忆力, 回忆', 'word_id': 981104, 'meaning_id': 1294}],
  106. 'take_count': 1, 'student_stage': 3, 'demo_name': '春笋英语',"article_difficulty":800}
  107. r = requests.post(f"{use_address}/article/reading-comprehension", json=json_data)
  108. r_json = r.json()
  109. try:
  110. return r_json
  111. except Exception as e:
  112. print("春笋文章reading-comprehension错误", e)
  113. print("错误数据", r_json)
  114. def download_word():
  115. from make_docx_demo.data import test_json2
  116. params = {"document_format": 2, "scanpage_format": 1}
  117. r = requests.post(f"{use_address}/make_word/vocabulary_assault", params=params, json=test_json2)
  118. r.raise_for_status()
  119. suffix = {1: "docx", 2: "pdf"}[params['document_format']]
  120. with open(f"test.{suffix}", "wb") as f:
  121. f.write(r.content)
  122. def spoken_language():
  123. url = f"{use_address}/spoken_language"
  124. with open(r"1.mp3", "rb") as f:
  125. audio_data = {'file': ('file.bin', f, 'application/octet-stream')}
  126. data = {"text": "You must study to be frank with the world"}
  127. r1 = requests.post(url, data=data, files=audio_data)
  128. assert r1.json()['code'] == 0
  129. data = {"text": "apple Life is like a box of chocolates, you never know what you're gonna",
  130. "url": "https://yunzhixue.blob.core.chinacloudapi.cn/download/ttsmaker-file-2025-1-13-10-47-47.mp3"}
  131. r2 = requests.post(url, data=data)
  132. assert r2.json()['code'] == 0
  133. @time_use
  134. def get_article3():
  135. json_data = {"words": [
  136. {'spell': 'term', 'meaning': '学期'}, {'spell': 'ordinary', 'meaning': '平常的, 普通的, 一般的'},
  137. {'spell': 'discussion', 'meaning': '讨论, 谈论'}, {'spell': 'shine', 'meaning': '照耀, 发光, 闪耀'},
  138. {'spell': 'million', 'meaning': '百万'}, {'spell': 'greet', 'meaning': '问候'}
  139. ], "take_count": 1, "student_stage": 3, "demo_name": "测试项目"}
  140. r = requests.post(f"{use_address}/article/reading-comprehension/deepseek", json=json_data)
  141. r_json = r.json()
  142. try:
  143. assert r_json['articles'][0]["english"]
  144. assert r_json['articles'][0]["difficultSentences"]
  145. assert r_json['articles'][0]["questions"]
  146. assert r_json['articles'][0]["allWordAmount"] > 10
  147. return r_json
  148. except Exception as e:
  149. print("deepseek生成文章出错", e)
  150. print(r_json)
  151. @time_use
  152. def run_all_test_cese():
  153. test_connect()
  154. get_audio()
  155. spoken_language()
  156. download_word()
  157. print(get_article2_1())
  158. if __name__ == '__main__':
  159. download_word()