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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. from time import sleep
  17. import pytest
  18. from common import (
  19. batch_add_chunks,
  20. batch_create_datasets,
  21. bulk_upload_documents,
  22. delete_chunks,
  23. list_chunks,
  24. list_documents,
  25. list_kbs,
  26. parse_documents,
  27. rm_kb,
  28. )
  29. from libs.auth import RAGFlowWebApiAuth
  30. from pytest import FixtureRequest
  31. from utils import wait_for
  32. from 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. @wait_for(30, 1, "Document parsing timeout")
  45. def condition(_auth, _kb_id):
  46. res = list_documents(_auth, {"kb_id": _kb_id})
  47. for doc in res["data"]["docs"]:
  48. if doc["run"] != "3":
  49. return False
  50. return True
  51. @pytest.fixture
  52. def generate_test_files(request: FixtureRequest, tmp_path):
  53. file_creators = {
  54. "docx": (tmp_path / "ragflow_test.docx", create_docx_file),
  55. "excel": (tmp_path / "ragflow_test.xlsx", create_excel_file),
  56. "ppt": (tmp_path / "ragflow_test.pptx", create_ppt_file),
  57. "image": (tmp_path / "ragflow_test.png", create_image_file),
  58. "pdf": (tmp_path / "ragflow_test.pdf", create_pdf_file),
  59. "txt": (tmp_path / "ragflow_test.txt", create_txt_file),
  60. "md": (tmp_path / "ragflow_test.md", create_md_file),
  61. "json": (tmp_path / "ragflow_test.json", create_json_file),
  62. "eml": (tmp_path / "ragflow_test.eml", create_eml_file),
  63. "html": (tmp_path / "ragflow_test.html", create_html_file),
  64. }
  65. files = {}
  66. for file_type, (file_path, creator_func) in file_creators.items():
  67. if request.param in ["", file_type]:
  68. creator_func(file_path)
  69. files[file_type] = file_path
  70. return files
  71. @pytest.fixture(scope="class")
  72. def ragflow_tmp_dir(request, tmp_path_factory):
  73. class_name = request.cls.__name__
  74. return tmp_path_factory.mktemp(class_name)
  75. @pytest.fixture(scope="session")
  76. def WebApiAuth(auth):
  77. return RAGFlowWebApiAuth(auth)
  78. @pytest.fixture(scope="function")
  79. def clear_datasets(request: FixtureRequest, WebApiAuth: RAGFlowWebApiAuth):
  80. def cleanup():
  81. res = list_kbs(WebApiAuth, params={"page_size": 1000})
  82. for kb in res["data"]["kbs"]:
  83. rm_kb(WebApiAuth, {"kb_id": kb["id"]})
  84. request.addfinalizer(cleanup)
  85. @pytest.fixture(scope="class")
  86. def add_dataset(request: FixtureRequest, WebApiAuth: RAGFlowWebApiAuth) -> str:
  87. def cleanup():
  88. res = list_kbs(WebApiAuth, params={"page_size": 1000})
  89. for kb in res["data"]["kbs"]:
  90. rm_kb(WebApiAuth, {"kb_id": kb["id"]})
  91. request.addfinalizer(cleanup)
  92. return batch_create_datasets(WebApiAuth, 1)[0]
  93. @pytest.fixture(scope="function")
  94. def add_dataset_func(request: FixtureRequest, WebApiAuth: RAGFlowWebApiAuth) -> str:
  95. def cleanup():
  96. res = list_kbs(WebApiAuth, params={"page_size": 1000})
  97. for kb in res["data"]["kbs"]:
  98. rm_kb(WebApiAuth, {"kb_id": kb["id"]})
  99. request.addfinalizer(cleanup)
  100. return batch_create_datasets(WebApiAuth, 1)[0]
  101. @pytest.fixture(scope="class")
  102. def add_document(request, WebApiAuth, add_dataset, ragflow_tmp_dir):
  103. # def cleanup():
  104. # res = list_documents(WebApiAuth, {"kb_id": dataset_id})
  105. # for doc in res["data"]["docs"]:
  106. # delete_document(WebApiAuth, {"doc_id": doc["id"]})
  107. # request.addfinalizer(cleanup)
  108. dataset_id = add_dataset
  109. return dataset_id, bulk_upload_documents(WebApiAuth, dataset_id, 1, ragflow_tmp_dir)[0]
  110. @pytest.fixture(scope="class")
  111. def add_chunks(request, WebApiAuth, add_document):
  112. def cleanup():
  113. res = list_chunks(WebApiAuth, {"doc_id": document_id})
  114. if res["code"] == 0:
  115. chunk_ids = [chunk["chunk_id"] for chunk in res["data"]["chunks"]]
  116. delete_chunks(WebApiAuth, {"doc_id": document_id, "chunk_ids": chunk_ids})
  117. request.addfinalizer(cleanup)
  118. kb_id, document_id = add_document
  119. parse_documents(WebApiAuth, {"doc_ids": [document_id], "run": "1"})
  120. condition(WebApiAuth, kb_id)
  121. chunk_ids = batch_add_chunks(WebApiAuth, document_id, 4)
  122. # issues/6487
  123. sleep(1)
  124. return kb_id, document_id, chunk_ids