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

conftest.py 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. from common import (
  18. add_chunk,
  19. batch_create_datasets,
  20. bulk_upload_documents,
  21. create_chat_assistant,
  22. delete_chat_assistants,
  23. delete_datasets,
  24. delete_session_with_chat_assistants,
  25. list_documnets,
  26. parse_documnets,
  27. )
  28. from libs.auth import RAGFlowHttpApiAuth
  29. from utils import wait_for
  30. from utils.file_utils import (
  31. create_docx_file,
  32. create_eml_file,
  33. create_excel_file,
  34. create_html_file,
  35. create_image_file,
  36. create_json_file,
  37. create_md_file,
  38. create_pdf_file,
  39. create_ppt_file,
  40. create_txt_file,
  41. )
  42. @wait_for(30, 1, "Document parsing timeout")
  43. def condition(_auth, _dataset_id):
  44. res = list_documnets(_auth, _dataset_id)
  45. for doc in res["data"]["docs"]:
  46. if doc["run"] != "DONE":
  47. return False
  48. return True
  49. @pytest.fixture(scope="session")
  50. def api_key(token):
  51. return RAGFlowHttpApiAuth(token)
  52. @pytest.fixture(scope="function")
  53. def clear_datasets(request, api_key):
  54. def cleanup():
  55. delete_datasets(api_key, {"ids": None})
  56. request.addfinalizer(cleanup)
  57. @pytest.fixture(scope="function")
  58. def clear_chat_assistants(request, api_key):
  59. def cleanup():
  60. delete_chat_assistants(api_key)
  61. request.addfinalizer(cleanup)
  62. @pytest.fixture(scope="function")
  63. def clear_session_with_chat_assistants(request, api_key, add_chat_assistants):
  64. _, _, chat_assistant_ids = add_chat_assistants
  65. def cleanup():
  66. for chat_assistant_id in chat_assistant_ids:
  67. delete_session_with_chat_assistants(api_key, chat_assistant_id)
  68. request.addfinalizer(cleanup)
  69. @pytest.fixture
  70. def generate_test_files(request, tmp_path):
  71. file_creators = {
  72. "docx": (tmp_path / "ragflow_test.docx", create_docx_file),
  73. "excel": (tmp_path / "ragflow_test.xlsx", create_excel_file),
  74. "ppt": (tmp_path / "ragflow_test.pptx", create_ppt_file),
  75. "image": (tmp_path / "ragflow_test.png", create_image_file),
  76. "pdf": (tmp_path / "ragflow_test.pdf", create_pdf_file),
  77. "txt": (tmp_path / "ragflow_test.txt", create_txt_file),
  78. "md": (tmp_path / "ragflow_test.md", create_md_file),
  79. "json": (tmp_path / "ragflow_test.json", create_json_file),
  80. "eml": (tmp_path / "ragflow_test.eml", create_eml_file),
  81. "html": (tmp_path / "ragflow_test.html", create_html_file),
  82. }
  83. files = {}
  84. for file_type, (file_path, creator_func) in file_creators.items():
  85. if request.param in ["", file_type]:
  86. creator_func(file_path)
  87. files[file_type] = file_path
  88. return files
  89. @pytest.fixture(scope="class")
  90. def ragflow_tmp_dir(request, tmp_path_factory):
  91. class_name = request.cls.__name__
  92. return tmp_path_factory.mktemp(class_name)
  93. @pytest.fixture(scope="class")
  94. def add_dataset(request, api_key):
  95. def cleanup():
  96. delete_datasets(api_key, {"ids": None})
  97. request.addfinalizer(cleanup)
  98. dataset_ids = batch_create_datasets(api_key, 1)
  99. return dataset_ids[0]
  100. @pytest.fixture(scope="function")
  101. def add_dataset_func(request, api_key):
  102. def cleanup():
  103. delete_datasets(api_key, {"ids": None})
  104. request.addfinalizer(cleanup)
  105. return batch_create_datasets(api_key, 1)[0]
  106. @pytest.fixture(scope="class")
  107. def add_document(api_key, add_dataset, ragflow_tmp_dir):
  108. dataset_id = add_dataset
  109. document_ids = bulk_upload_documents(api_key, dataset_id, 1, ragflow_tmp_dir)
  110. return dataset_id, document_ids[0]
  111. @pytest.fixture(scope="class")
  112. def add_chunks(api_key, add_document):
  113. dataset_id, document_id = add_document
  114. parse_documnets(api_key, dataset_id, {"document_ids": [document_id]})
  115. condition(api_key, dataset_id)
  116. chunk_ids = []
  117. for i in range(4):
  118. res = add_chunk(api_key, dataset_id, document_id, {"content": f"chunk test {i}"})
  119. chunk_ids.append(res["data"]["chunk"]["id"])
  120. # issues/6487
  121. from time import sleep
  122. sleep(1)
  123. return dataset_id, document_id, chunk_ids
  124. @pytest.fixture(scope="class")
  125. def add_chat_assistants(request, api_key, add_document):
  126. def cleanup():
  127. delete_chat_assistants(api_key)
  128. request.addfinalizer(cleanup)
  129. dataset_id, document_id = add_document
  130. parse_documnets(api_key, dataset_id, {"document_ids": [document_id]})
  131. condition(api_key, dataset_id)
  132. chat_assistant_ids = []
  133. for i in range(5):
  134. res = create_chat_assistant(api_key, {"name": f"test_chat_assistant_{i}", "dataset_ids": [dataset_id]})
  135. chat_assistant_ids.append(res["data"]["id"])
  136. return dataset_id, document_id, chat_assistant_ids