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

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