選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

conftest.py 3.2KB

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