Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

conftest.py 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #
  2. # Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. import pytest
  17. import requests
  18. from configs import EMAIL, HOST_ADDRESS, PASSWORD, VERSION, ZHIPU_AI_API_KEY
  19. MARKER_EXPRESSIONS = {
  20. "p1": "p1",
  21. "p2": "p1 or p2",
  22. "p3": "p1 or p2 or p3",
  23. }
  24. def pytest_addoption(parser: pytest.Parser) -> None:
  25. parser.addoption(
  26. "--level",
  27. action="store",
  28. default="p2",
  29. choices=list(MARKER_EXPRESSIONS.keys()),
  30. help=f"Test level ({'/'.join(MARKER_EXPRESSIONS)}): p1=smoke, p2=core, p3=full",
  31. )
  32. parser.addoption(
  33. "--client-type",
  34. action="store",
  35. default="http",
  36. choices=["python_sdk", "http", "web"],
  37. help="Test client type: 'python_sdk', 'http', 'web'",
  38. )
  39. def pytest_configure(config: pytest.Config) -> None:
  40. level = config.getoption("--level")
  41. config.option.markexpr = MARKER_EXPRESSIONS[level]
  42. if config.option.verbose > 0:
  43. print(f"\n[CONFIG] Active test level: {level}")
  44. def register():
  45. url = HOST_ADDRESS + f"/{VERSION}/user/register"
  46. name = "qa"
  47. register_data = {"email": EMAIL, "nickname": name, "password": PASSWORD}
  48. res = requests.post(url=url, json=register_data)
  49. res = res.json()
  50. if res.get("code") != 0 and "has already registered" not in res.get("message"):
  51. raise Exception(res.get("message"))
  52. def login():
  53. url = HOST_ADDRESS + f"/{VERSION}/user/login"
  54. login_data = {"email": EMAIL, "password": PASSWORD}
  55. response = requests.post(url=url, json=login_data)
  56. res = response.json()
  57. if res.get("code") != 0:
  58. raise Exception(res.get("message"))
  59. auth = response.headers["Authorization"]
  60. return auth
  61. @pytest.fixture(scope="session")
  62. def auth():
  63. try:
  64. register()
  65. except Exception as e:
  66. print(e)
  67. auth = login()
  68. return auth
  69. @pytest.fixture(scope="session")
  70. def token(auth):
  71. url = HOST_ADDRESS + f"/{VERSION}/system/new_token"
  72. auth = {"Authorization": auth}
  73. response = requests.post(url=url, headers=auth)
  74. res = response.json()
  75. if res.get("code") != 0:
  76. raise Exception(res.get("message"))
  77. return res["data"].get("token")
  78. def get_my_llms(auth, name):
  79. url = HOST_ADDRESS + f"/{VERSION}/llm/my_llms"
  80. authorization = {"Authorization": auth}
  81. response = requests.get(url=url, headers=authorization)
  82. res = response.json()
  83. if res.get("code") != 0:
  84. raise Exception(res.get("message"))
  85. if name in res.get("data"):
  86. return True
  87. return False
  88. def add_models(auth):
  89. url = HOST_ADDRESS + f"/{VERSION}/llm/set_api_key"
  90. authorization = {"Authorization": auth}
  91. models_info = {
  92. "ZHIPU-AI": {"llm_factory": "ZHIPU-AI", "api_key": ZHIPU_AI_API_KEY},
  93. }
  94. for name, model_info in models_info.items():
  95. if not get_my_llms(auth, name):
  96. response = requests.post(url=url, headers=authorization, json=model_info)
  97. res = response.json()
  98. if res.get("code") != 0:
  99. pytest.exit(f"Critical error in add_models: {res.get('message')}")
  100. def get_tenant_info(auth):
  101. url = HOST_ADDRESS + f"/{VERSION}/user/tenant_info"
  102. authorization = {"Authorization": auth}
  103. response = requests.get(url=url, headers=authorization)
  104. res = response.json()
  105. if res.get("code") != 0:
  106. raise Exception(res.get("message"))
  107. return res["data"].get("tenant_id")
  108. @pytest.fixture(scope="session", autouse=True)
  109. def set_tenant_info(auth):
  110. try:
  111. add_models(auth)
  112. tenant_id = get_tenant_info(auth)
  113. except Exception as e:
  114. pytest.exit(f"Error in set_tenant_info: {str(e)}")
  115. url = HOST_ADDRESS + f"/{VERSION}/user/set_tenant_info"
  116. authorization = {"Authorization": auth}
  117. tenant_info = {
  118. "tenant_id": tenant_id,
  119. "llm_id": "glm-4-flash@ZHIPU-AI",
  120. "embd_id": "BAAI/bge-large-zh-v1.5@BAAI",
  121. "img2txt_id": "",
  122. "asr_id": "",
  123. "tts_id": None,
  124. }
  125. response = requests.post(url=url, headers=authorization, json=tenant_info)
  126. res = response.json()
  127. if res.get("code") != 0:
  128. raise Exception(res.get("message"))