Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

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