Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

conftest.py 5.0KB

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