Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

conftest.py 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 pathlib import Path
  17. from time import sleep
  18. import pytest
  19. from common import (
  20. batch_add_chunks,
  21. batch_create_chat_assistants,
  22. batch_create_datasets,
  23. bulk_upload_documents,
  24. )
  25. from configs import HOST_ADDRESS, VERSION
  26. from pytest import FixtureRequest
  27. from ragflow_sdk import Chat, Chunk, DataSet, Document, RAGFlow
  28. from utils import wait_for
  29. from utils.file_utils import (
  30. create_docx_file,
  31. create_eml_file,
  32. create_excel_file,
  33. create_html_file,
  34. create_image_file,
  35. create_json_file,
  36. create_md_file,
  37. create_pdf_file,
  38. create_ppt_file,
  39. create_txt_file,
  40. )
  41. @wait_for(30, 1, "Document parsing timeout")
  42. def condition(_dataset: DataSet):
  43. documents = _dataset.list_documents(page_size=1000)
  44. for document in documents:
  45. if document.run != "DONE":
  46. return False
  47. return True
  48. @pytest.fixture
  49. def generate_test_files(request: FixtureRequest, tmp_path: Path):
  50. file_creators = {
  51. "docx": (tmp_path / "ragflow_test.docx", create_docx_file),
  52. "excel": (tmp_path / "ragflow_test.xlsx", create_excel_file),
  53. "ppt": (tmp_path / "ragflow_test.pptx", create_ppt_file),
  54. "image": (tmp_path / "ragflow_test.png", create_image_file),
  55. "pdf": (tmp_path / "ragflow_test.pdf", create_pdf_file),
  56. "txt": (tmp_path / "ragflow_test.txt", create_txt_file),
  57. "md": (tmp_path / "ragflow_test.md", create_md_file),
  58. "json": (tmp_path / "ragflow_test.json", create_json_file),
  59. "eml": (tmp_path / "ragflow_test.eml", create_eml_file),
  60. "html": (tmp_path / "ragflow_test.html", create_html_file),
  61. }
  62. files = {}
  63. for file_type, (file_path, creator_func) in file_creators.items():
  64. if request.param in ["", file_type]:
  65. creator_func(file_path)
  66. files[file_type] = file_path
  67. return files
  68. @pytest.fixture(scope="class")
  69. def ragflow_tmp_dir(request: FixtureRequest, tmp_path_factory: Path) -> Path:
  70. class_name = request.cls.__name__
  71. return tmp_path_factory.mktemp(class_name)
  72. @pytest.fixture(scope="session")
  73. def client(token: str) -> RAGFlow:
  74. return RAGFlow(api_key=token, base_url=HOST_ADDRESS, version=VERSION)
  75. @pytest.fixture(scope="function")
  76. def clear_datasets(request: FixtureRequest, client: RAGFlow):
  77. def cleanup():
  78. client.delete_datasets(ids=None)
  79. request.addfinalizer(cleanup)
  80. @pytest.fixture(scope="function")
  81. def clear_chat_assistants(request: FixtureRequest, client: RAGFlow):
  82. def cleanup():
  83. client.delete_chats(ids=None)
  84. request.addfinalizer(cleanup)
  85. @pytest.fixture(scope="function")
  86. def clear_session_with_chat_assistants(request, add_chat_assistants):
  87. def cleanup():
  88. for chat_assistant in chat_assistants:
  89. try:
  90. chat_assistant.delete_sessions(ids=None)
  91. except Exception:
  92. pass
  93. request.addfinalizer(cleanup)
  94. _, _, chat_assistants = add_chat_assistants
  95. @pytest.fixture(scope="class")
  96. def add_dataset(request: FixtureRequest, client: RAGFlow) -> DataSet:
  97. def cleanup():
  98. client.delete_datasets(ids=None)
  99. request.addfinalizer(cleanup)
  100. return batch_create_datasets(client, 1)[0]
  101. @pytest.fixture(scope="function")
  102. def add_dataset_func(request: FixtureRequest, client: RAGFlow) -> DataSet:
  103. def cleanup():
  104. client.delete_datasets(ids=None)
  105. request.addfinalizer(cleanup)
  106. return batch_create_datasets(client, 1)[0]
  107. @pytest.fixture(scope="class")
  108. def add_document(add_dataset: DataSet, ragflow_tmp_dir: Path) -> tuple[DataSet, Document]:
  109. return add_dataset, bulk_upload_documents(add_dataset, 1, ragflow_tmp_dir)[0]
  110. @pytest.fixture(scope="class")
  111. def add_chunks(request: FixtureRequest, add_document: tuple[DataSet, Document]) -> tuple[DataSet, Document, list[Chunk]]:
  112. def cleanup():
  113. try:
  114. document.delete_chunks(ids=[])
  115. except Exception:
  116. pass
  117. request.addfinalizer(cleanup)
  118. dataset, document = add_document
  119. dataset.async_parse_documents([document.id])
  120. condition(dataset)
  121. chunks = batch_add_chunks(document, 4)
  122. # issues/6487
  123. sleep(1)
  124. return dataset, document, chunks
  125. @pytest.fixture(scope="class")
  126. def add_chat_assistants(request, client, add_document) -> tuple[DataSet, Document, list[Chat]]:
  127. def cleanup():
  128. try:
  129. client.delete_chats(ids=None)
  130. except Exception:
  131. pass
  132. request.addfinalizer(cleanup)
  133. dataset, document = add_document
  134. dataset.async_parse_documents([document.id])
  135. condition(dataset)
  136. return dataset, document, batch_create_chat_assistants(client, 5)