You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

conftest.py 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 add_chunk, batch_create_datasets, bulk_upload_documents, delete_chat_assistants, delete_datasets, list_documnets, parse_documnets
  18. from libs.utils import wait_for
  19. from libs.utils.file_utils import (
  20. create_docx_file,
  21. create_eml_file,
  22. create_excel_file,
  23. create_html_file,
  24. create_image_file,
  25. create_json_file,
  26. create_md_file,
  27. create_pdf_file,
  28. create_ppt_file,
  29. create_txt_file,
  30. )
  31. @wait_for(30, 1, "Document parsing timeout")
  32. def condition(_auth, _dataset_id):
  33. res = list_documnets(_auth, _dataset_id)
  34. for doc in res["data"]["docs"]:
  35. if doc["run"] != "DONE":
  36. return False
  37. return True
  38. @pytest.fixture(scope="function")
  39. def clear_datasets(get_http_api_auth):
  40. yield
  41. delete_datasets(get_http_api_auth)
  42. @pytest.fixture(scope="function")
  43. def clear_chat_assistants(get_http_api_auth):
  44. yield
  45. delete_chat_assistants(get_http_api_auth)
  46. @pytest.fixture
  47. def generate_test_files(request, tmp_path):
  48. file_creators = {
  49. "docx": (tmp_path / "ragflow_test.docx", create_docx_file),
  50. "excel": (tmp_path / "ragflow_test.xlsx", create_excel_file),
  51. "ppt": (tmp_path / "ragflow_test.pptx", create_ppt_file),
  52. "image": (tmp_path / "ragflow_test.png", create_image_file),
  53. "pdf": (tmp_path / "ragflow_test.pdf", create_pdf_file),
  54. "txt": (tmp_path / "ragflow_test.txt", create_txt_file),
  55. "md": (tmp_path / "ragflow_test.md", create_md_file),
  56. "json": (tmp_path / "ragflow_test.json", create_json_file),
  57. "eml": (tmp_path / "ragflow_test.eml", create_eml_file),
  58. "html": (tmp_path / "ragflow_test.html", create_html_file),
  59. }
  60. files = {}
  61. for file_type, (file_path, creator_func) in file_creators.items():
  62. if request.param in ["", file_type]:
  63. creator_func(file_path)
  64. files[file_type] = file_path
  65. return files
  66. @pytest.fixture(scope="class")
  67. def ragflow_tmp_dir(request, tmp_path_factory):
  68. class_name = request.cls.__name__
  69. return tmp_path_factory.mktemp(class_name)
  70. @pytest.fixture(scope="class")
  71. def add_dataset(request, get_http_api_auth):
  72. def cleanup():
  73. delete_datasets(get_http_api_auth)
  74. request.addfinalizer(cleanup)
  75. dataset_ids = batch_create_datasets(get_http_api_auth, 1)
  76. return dataset_ids[0]
  77. @pytest.fixture(scope="function")
  78. def add_dataset_func(request, get_http_api_auth):
  79. def cleanup():
  80. delete_datasets(get_http_api_auth)
  81. request.addfinalizer(cleanup)
  82. dataset_ids = batch_create_datasets(get_http_api_auth, 1)
  83. return dataset_ids[0]
  84. @pytest.fixture(scope="class")
  85. def add_document(get_http_api_auth, add_dataset, ragflow_tmp_dir):
  86. dataset_id = add_dataset
  87. document_ids = bulk_upload_documents(get_http_api_auth, dataset_id, 1, ragflow_tmp_dir)
  88. return dataset_id, document_ids[0]
  89. @pytest.fixture(scope="class")
  90. def add_chunks(get_http_api_auth, add_document):
  91. dataset_id, document_id = add_document
  92. parse_documnets(get_http_api_auth, dataset_id, {"document_ids": [document_id]})
  93. condition(get_http_api_auth, dataset_id)
  94. chunk_ids = []
  95. for i in range(4):
  96. res = add_chunk(get_http_api_auth, dataset_id, document_id, {"content": f"chunk test {i}"})
  97. chunk_ids.append(res["data"]["chunk"]["id"])
  98. # issues/6487
  99. from time import sleep
  100. sleep(1)
  101. return dataset_id, document_id, chunk_ids