You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

conftest.py 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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 os
  17. import pytest
  18. import requests
  19. from common import (
  20. add_chunk,
  21. batch_create_datasets,
  22. bulk_upload_documents,
  23. create_chat_assistant,
  24. delete_chat_assistants,
  25. delete_datasets,
  26. delete_session_with_chat_assistants,
  27. list_documnets,
  28. parse_documnets,
  29. )
  30. from libs.auth import RAGFlowHttpApiAuth
  31. from libs.utils import wait_for
  32. from libs.utils.file_utils import (
  33. create_docx_file,
  34. create_eml_file,
  35. create_excel_file,
  36. create_html_file,
  37. create_image_file,
  38. create_json_file,
  39. create_md_file,
  40. create_pdf_file,
  41. create_ppt_file,
  42. create_txt_file,
  43. )
  44. MARKER_EXPRESSIONS = {
  45. "p1": "p1",
  46. "p2": "p1 or p2",
  47. "p3": "p1 or p2 or p3",
  48. }
  49. HOST_ADDRESS = os.getenv("HOST_ADDRESS", "http://127.0.0.1:9380")
  50. ZHIPU_AI_API_KEY = os.getenv("ZHIPU_AI_API_KEY")
  51. print(f"{ZHIPU_AI_API_KEY=}")
  52. if ZHIPU_AI_API_KEY is None:
  53. pytest.exit("Error: Environment variable ZHIPU_AI_API_KEY must be set")
  54. def pytest_addoption(parser: pytest.Parser) -> None:
  55. parser.addoption(
  56. "--level",
  57. action="store",
  58. default="p2",
  59. choices=list(MARKER_EXPRESSIONS.keys()),
  60. help=f"Test level ({'/'.join(MARKER_EXPRESSIONS)}): p1=smoke, p2=core, p3=full",
  61. )
  62. def pytest_configure(config: pytest.Config) -> None:
  63. level = config.getoption("--level")
  64. config.option.markexpr = MARKER_EXPRESSIONS[level]
  65. if config.option.verbose > 0:
  66. print(f"\n[CONFIG] Active test level: {level}")
  67. @wait_for(30, 1, "Document parsing timeout")
  68. def condition(_auth, _dataset_id):
  69. res = list_documnets(_auth, _dataset_id)
  70. for doc in res["data"]["docs"]:
  71. if doc["run"] != "DONE":
  72. return False
  73. return True
  74. @pytest.fixture(scope="session")
  75. def get_http_api_auth(get_api_key_fixture):
  76. return RAGFlowHttpApiAuth(get_api_key_fixture)
  77. def get_my_llms(auth, name):
  78. url = HOST_ADDRESS + "/v1/llm/my_llms"
  79. authorization = {"Authorization": auth}
  80. response = requests.get(url=url, headers=authorization)
  81. res = response.json()
  82. if res.get("code") != 0:
  83. raise Exception(res.get("message"))
  84. if name in res.get("data"):
  85. return True
  86. return False
  87. def add_models(auth):
  88. url = HOST_ADDRESS + "/v1/llm/set_api_key"
  89. authorization = {"Authorization": auth}
  90. models_info = {
  91. "ZHIPU-AI": {"llm_factory": "ZHIPU-AI", "api_key": ZHIPU_AI_API_KEY},
  92. }
  93. for name, model_info in models_info.items():
  94. if not get_my_llms(auth, name):
  95. response = requests.post(url=url, headers=authorization, json=model_info)
  96. res = response.json()
  97. if res.get("code") != 0:
  98. pytest.exit(f"Critical error in add_models: {res.get('message')}")
  99. def get_tenant_info(auth):
  100. url = HOST_ADDRESS + "/v1/user/tenant_info"
  101. authorization = {"Authorization": auth}
  102. response = requests.get(url=url, headers=authorization)
  103. res = response.json()
  104. if res.get("code") != 0:
  105. raise Exception(res.get("message"))
  106. return res["data"].get("tenant_id")
  107. @pytest.fixture(scope="session", autouse=True)
  108. def set_tenant_info(get_auth):
  109. auth = get_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 + "/v1/user/set_tenant_info"
  116. authorization = {"Authorization": get_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": "glm-4v@ZHIPU-AI",
  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"))
  129. @pytest.fixture(scope="function")
  130. def clear_datasets(request, get_http_api_auth):
  131. def cleanup():
  132. delete_datasets(get_http_api_auth, {"ids": None})
  133. request.addfinalizer(cleanup)
  134. @pytest.fixture(scope="function")
  135. def clear_chat_assistants(request, get_http_api_auth):
  136. def cleanup():
  137. delete_chat_assistants(get_http_api_auth)
  138. request.addfinalizer(cleanup)
  139. @pytest.fixture(scope="function")
  140. def clear_session_with_chat_assistants(request, get_http_api_auth, add_chat_assistants):
  141. _, _, chat_assistant_ids = add_chat_assistants
  142. def cleanup():
  143. for chat_assistant_id in chat_assistant_ids:
  144. delete_session_with_chat_assistants(get_http_api_auth, chat_assistant_id)
  145. request.addfinalizer(cleanup)
  146. @pytest.fixture
  147. def generate_test_files(request, tmp_path):
  148. file_creators = {
  149. "docx": (tmp_path / "ragflow_test.docx", create_docx_file),
  150. "excel": (tmp_path / "ragflow_test.xlsx", create_excel_file),
  151. "ppt": (tmp_path / "ragflow_test.pptx", create_ppt_file),
  152. "image": (tmp_path / "ragflow_test.png", create_image_file),
  153. "pdf": (tmp_path / "ragflow_test.pdf", create_pdf_file),
  154. "txt": (tmp_path / "ragflow_test.txt", create_txt_file),
  155. "md": (tmp_path / "ragflow_test.md", create_md_file),
  156. "json": (tmp_path / "ragflow_test.json", create_json_file),
  157. "eml": (tmp_path / "ragflow_test.eml", create_eml_file),
  158. "html": (tmp_path / "ragflow_test.html", create_html_file),
  159. }
  160. files = {}
  161. for file_type, (file_path, creator_func) in file_creators.items():
  162. if request.param in ["", file_type]:
  163. creator_func(file_path)
  164. files[file_type] = file_path
  165. return files
  166. @pytest.fixture(scope="class")
  167. def ragflow_tmp_dir(request, tmp_path_factory):
  168. class_name = request.cls.__name__
  169. return tmp_path_factory.mktemp(class_name)
  170. @pytest.fixture(scope="class")
  171. def add_dataset(request, get_http_api_auth):
  172. def cleanup():
  173. delete_datasets(get_http_api_auth, {"ids": None})
  174. request.addfinalizer(cleanup)
  175. dataset_ids = batch_create_datasets(get_http_api_auth, 1)
  176. return dataset_ids[0]
  177. @pytest.fixture(scope="function")
  178. def add_dataset_func(request, get_http_api_auth):
  179. def cleanup():
  180. delete_datasets(get_http_api_auth, {"ids": None})
  181. request.addfinalizer(cleanup)
  182. return batch_create_datasets(get_http_api_auth, 1)[0]
  183. @pytest.fixture(scope="class")
  184. def add_document(get_http_api_auth, add_dataset, ragflow_tmp_dir):
  185. dataset_id = add_dataset
  186. document_ids = bulk_upload_documents(get_http_api_auth, dataset_id, 1, ragflow_tmp_dir)
  187. return dataset_id, document_ids[0]
  188. @pytest.fixture(scope="class")
  189. def add_chunks(get_http_api_auth, add_document):
  190. dataset_id, document_id = add_document
  191. parse_documnets(get_http_api_auth, dataset_id, {"document_ids": [document_id]})
  192. condition(get_http_api_auth, dataset_id)
  193. chunk_ids = []
  194. for i in range(4):
  195. res = add_chunk(get_http_api_auth, dataset_id, document_id, {"content": f"chunk test {i}"})
  196. chunk_ids.append(res["data"]["chunk"]["id"])
  197. # issues/6487
  198. from time import sleep
  199. sleep(1)
  200. return dataset_id, document_id, chunk_ids
  201. @pytest.fixture(scope="class")
  202. def add_chat_assistants(request, get_http_api_auth, add_document):
  203. def cleanup():
  204. delete_chat_assistants(get_http_api_auth)
  205. request.addfinalizer(cleanup)
  206. dataset_id, document_id = add_document
  207. parse_documnets(get_http_api_auth, dataset_id, {"document_ids": [document_id]})
  208. condition(get_http_api_auth, dataset_id)
  209. chat_assistant_ids = []
  210. for i in range(5):
  211. res = create_chat_assistant(get_http_api_auth, {"name": f"test_chat_assistant_{i}", "dataset_ids": [dataset_id]})
  212. chat_assistant_ids.append(res["data"]["id"])
  213. return dataset_id, document_id, chat_assistant_ids