mock_request.py 7.7 KB

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