您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

conftest.py 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. from common import (
  19. add_chunk,
  20. batch_create_datasets,
  21. bulk_upload_documents,
  22. create_chat_assistant,
  23. delete_chat_assistants,
  24. delete_datasets,
  25. delete_session_with_chat_assistants,
  26. list_documnets,
  27. parse_documnets,
  28. )
  29. from libs.auth import RAGFlowHttpApiAuth
  30. from libs.utils import wait_for
  31. from libs.utils.file_utils import (
  32. create_docx_file,
  33. create_eml_file,
  34. create_excel_file,
  35. create_html_file,
  36. create_image_file,
  37. create_json_file,
  38. create_md_file,
  39. create_pdf_file,
  40. create_ppt_file,
  41. create_txt_file,
  42. )
  43. MARKER_EXPRESSIONS = {
  44. "p1": "p1",
  45. "p2": "p1 or p2",
  46. "p3": "p1 or p2 or p3",
  47. }
  48. HOST_ADDRESS = os.getenv("HOST_ADDRESS", "http://127.0.0.1:9380")
  49. def pytest_addoption(parser: pytest.Parser) -> None:
  50. parser.addoption(
  51. "--level",
  52. action="store",
  53. default="p2",
  54. choices=list(MARKER_EXPRESSIONS.keys()),
  55. help=f"Test level ({'/'.join(MARKER_EXPRESSIONS)}): p1=smoke, p2=core, p3=full",
  56. )
  57. def pytest_configure(config: pytest.Config) -> None:
  58. level = config.getoption("--level")
  59. config.option.markexpr = MARKER_EXPRESSIONS[level]
  60. if config.option.verbose > 0:
  61. print(f"\n[CONFIG] Active test level: {level}")
  62. @wait_for(30, 1, "Document parsing timeout")
  63. def condition(_auth, _dataset_id):
  64. res = list_documnets(_auth, _dataset_id)
  65. for doc in res["data"]["docs"]:
  66. if doc["run"] != "DONE":
  67. return False
  68. return True
  69. @pytest.fixture(scope="session")
  70. def get_http_api_auth(get_api_key_fixture):
  71. return RAGFlowHttpApiAuth(get_api_key_fixture)
  72. @pytest.fixture(scope="function")
  73. def clear_datasets(request, get_http_api_auth):
  74. def cleanup():
  75. delete_datasets(get_http_api_auth, {"ids": None})
  76. request.addfinalizer(cleanup)
  77. @pytest.fixture(scope="function")
  78. def clear_chat_assistants(request, get_http_api_auth):
  79. def cleanup():
  80. delete_chat_assistants(get_http_api_auth)
  81. request.addfinalizer(cleanup)
  82. @pytest.fixture(scope="function")
  83. def clear_session_with_chat_assistants(request, get_http_api_auth, add_chat_assistants):
  84. _, _, chat_assistant_ids = add_chat_assistants
  85. def cleanup():
  86. for chat_assistant_id in chat_assistant_ids:
  87. delete_session_with_chat_assistants(get_http_api_auth, chat_assistant_id)
  88. request.addfinalizer(cleanup)
  89. @pytest.fixture
  90. def generate_test_files(request, tmp_path):
  91. file_creators = {
  92. "docx": (tmp_path / "ragflow_test.docx", create_docx_file),
  93. "excel": (tmp_path / "ragflow_test.xlsx", create_excel_file),
  94. "ppt": (tmp_path / "ragflow_test.pptx", create_ppt_file),
  95. "image": (tmp_path / "ragflow_test.png", create_image_file),
  96. "pdf": (tmp_path / "ragflow_test.pdf", create_pdf_file),
  97. "txt": (tmp_path / "ragflow_test.txt", create_txt_file),
  98. "md": (tmp_path / "ragflow_test.md", create_md_file),
  99. "json": (tmp_path / "ragflow_test.json", create_json_file),
  100. "eml": (tmp_path / "ragflow_test.eml", create_eml_file),
  101. "html": (tmp_path / "ragflow_test.html", create_html_file),
  102. }
  103. files = {}
  104. for file_type, (file_path, creator_func) in file_creators.items():
  105. if request.param in ["", file_type]:
  106. creator_func(file_path)
  107. files[file_type] = file_path
  108. return files
  109. @pytest.fixture(scope="class")
  110. def ragflow_tmp_dir(request, tmp_path_factory):
  111. class_name = request.cls.__name__
  112. return tmp_path_factory.mktemp(class_name)
  113. @pytest.fixture(scope="class")
  114. def add_dataset(request, get_http_api_auth):
  115. def cleanup():
  116. delete_datasets(get_http_api_auth, {"ids": None})
  117. request.addfinalizer(cleanup)
  118. dataset_ids = batch_create_datasets(get_http_api_auth, 1)
  119. return dataset_ids[0]
  120. @pytest.fixture(scope="function")
  121. def add_dataset_func(request, get_http_api_auth):
  122. def cleanup():
  123. delete_datasets(get_http_api_auth, {"ids": None})
  124. request.addfinalizer(cleanup)
  125. return batch_create_datasets(get_http_api_auth, 1)[0]
  126. @pytest.fixture(scope="class")
  127. def add_document(get_http_api_auth, add_dataset, ragflow_tmp_dir):
  128. dataset_id = add_dataset
  129. document_ids = bulk_upload_documents(get_http_api_auth, dataset_id, 1, ragflow_tmp_dir)
  130. return dataset_id, document_ids[0]
  131. @pytest.fixture(scope="class")
  132. def add_chunks(get_http_api_auth, add_document):
  133. dataset_id, document_id = add_document
  134. parse_documnets(get_http_api_auth, dataset_id, {"document_ids": [document_id]})
  135. condition(get_http_api_auth, dataset_id)
  136. chunk_ids = []
  137. for i in range(4):
  138. res = add_chunk(get_http_api_auth, dataset_id, document_id, {"content": f"chunk test {i}"})
  139. chunk_ids.append(res["data"]["chunk"]["id"])
  140. # issues/6487
  141. from time import sleep
  142. sleep(1)
  143. return dataset_id, document_id, chunk_ids
  144. @pytest.fixture(scope="class")
  145. def add_chat_assistants(request, get_http_api_auth, add_document):
  146. def cleanup():
  147. delete_chat_assistants(get_http_api_auth)
  148. request.addfinalizer(cleanup)
  149. dataset_id, document_id = add_document
  150. parse_documnets(get_http_api_auth, dataset_id, {"document_ids": [document_id]})
  151. condition(get_http_api_auth, dataset_id)
  152. chat_assistant_ids = []
  153. for i in range(5):
  154. res = create_chat_assistant(get_http_api_auth, {"name": f"test_chat_assistant_{i}", "dataset_ids": [dataset_id]})
  155. chat_assistant_ids.append(res["data"]["id"])
  156. return dataset_id, document_id, chat_assistant_ids