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 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. batch_create_datasets,
  19. list_kbs,
  20. rm_kb,
  21. )
  22. # from configs import HOST_ADDRESS, VERSION
  23. from libs.auth import RAGFlowWebApiAuth
  24. from pytest import FixtureRequest
  25. # from ragflow_sdk import RAGFlow
  26. from utils.file_utils import (
  27. create_docx_file,
  28. create_eml_file,
  29. create_excel_file,
  30. create_html_file,
  31. create_image_file,
  32. create_json_file,
  33. create_md_file,
  34. create_pdf_file,
  35. create_ppt_file,
  36. create_txt_file,
  37. )
  38. @pytest.fixture
  39. def generate_test_files(request: FixtureRequest, tmp_path):
  40. file_creators = {
  41. "docx": (tmp_path / "ragflow_test.docx", create_docx_file),
  42. "excel": (tmp_path / "ragflow_test.xlsx", create_excel_file),
  43. "ppt": (tmp_path / "ragflow_test.pptx", create_ppt_file),
  44. "image": (tmp_path / "ragflow_test.png", create_image_file),
  45. "pdf": (tmp_path / "ragflow_test.pdf", create_pdf_file),
  46. "txt": (tmp_path / "ragflow_test.txt", create_txt_file),
  47. "md": (tmp_path / "ragflow_test.md", create_md_file),
  48. "json": (tmp_path / "ragflow_test.json", create_json_file),
  49. "eml": (tmp_path / "ragflow_test.eml", create_eml_file),
  50. "html": (tmp_path / "ragflow_test.html", create_html_file),
  51. }
  52. files = {}
  53. for file_type, (file_path, creator_func) in file_creators.items():
  54. if request.param in ["", file_type]:
  55. creator_func(file_path)
  56. files[file_type] = file_path
  57. return files
  58. @pytest.fixture(scope="class")
  59. def ragflow_tmp_dir(request, tmp_path_factory):
  60. class_name = request.cls.__name__
  61. return tmp_path_factory.mktemp(class_name)
  62. @pytest.fixture(scope="session")
  63. def WebApiAuth(auth):
  64. return RAGFlowWebApiAuth(auth)
  65. # @pytest.fixture(scope="session")
  66. # def client(token: str) -> RAGFlow:
  67. # return RAGFlow(api_key=token, base_url=HOST_ADDRESS, version=VERSION)
  68. @pytest.fixture(scope="function")
  69. def clear_datasets(request: FixtureRequest, WebApiAuth: RAGFlowWebApiAuth):
  70. def cleanup():
  71. res = list_kbs(WebApiAuth, params={"page_size": 1000})
  72. for kb in res["data"]["kbs"]:
  73. rm_kb(WebApiAuth, {"kb_id": kb["id"]})
  74. request.addfinalizer(cleanup)
  75. @pytest.fixture(scope="class")
  76. def add_dataset(request: FixtureRequest, WebApiAuth: RAGFlowWebApiAuth) -> str:
  77. def cleanup():
  78. res = list_kbs(WebApiAuth, params={"page_size": 1000})
  79. for kb in res["data"]["kbs"]:
  80. rm_kb(WebApiAuth, {"kb_id": kb["id"]})
  81. request.addfinalizer(cleanup)
  82. return batch_create_datasets(WebApiAuth, 1)[0]
  83. @pytest.fixture(scope="function")
  84. def add_dataset_func(request: FixtureRequest, WebApiAuth: RAGFlowWebApiAuth) -> str:
  85. def cleanup():
  86. res = list_kbs(WebApiAuth, params={"page_size": 1000})
  87. for kb in res["data"]["kbs"]:
  88. rm_kb(WebApiAuth, {"kb_id": kb["id"]})
  89. request.addfinalizer(cleanup)
  90. return batch_create_datasets(WebApiAuth, 1)[0]