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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. MARKER_EXPRESSIONS = {
  42. "p1": "p1",
  43. "p2": "p1 or p2",
  44. "p3": "p1 or p2 or p3",
  45. }
  46. def pytest_addoption(parser: pytest.Parser) -> None:
  47. parser.addoption(
  48. "--level",
  49. action="store",
  50. default="p2",
  51. choices=list(MARKER_EXPRESSIONS.keys()),
  52. help=f"Test level ({'/'.join(MARKER_EXPRESSIONS)}): p1=smoke, p2=core, p3=full",
  53. )
  54. def pytest_configure(config: pytest.Config) -> None:
  55. level = config.getoption("--level")
  56. config.option.markexpr = MARKER_EXPRESSIONS[level]
  57. if config.option.verbose > 0:
  58. print(f"\n[CONFIG] Active test level: {level}")
  59. @wait_for(30, 1, "Document parsing timeout")
  60. def condition(_auth, _dataset_id):
  61. res = list_documnets(_auth, _dataset_id)
  62. for doc in res["data"]["docs"]:
  63. if doc["run"] != "DONE":
  64. return False
  65. return True
  66. @pytest.fixture(scope="function")
  67. def clear_datasets(request, get_http_api_auth):
  68. def cleanup():
  69. delete_datasets(get_http_api_auth, {"ids": None})
  70. request.addfinalizer(cleanup)
  71. @pytest.fixture(scope="function")
  72. def clear_chat_assistants(request, get_http_api_auth):
  73. def cleanup():
  74. delete_chat_assistants(get_http_api_auth)
  75. request.addfinalizer(cleanup)
  76. @pytest.fixture(scope="function")
  77. def clear_session_with_chat_assistants(request, get_http_api_auth, add_chat_assistants):
  78. _, _, chat_assistant_ids = add_chat_assistants
  79. def cleanup():
  80. for chat_assistant_id in chat_assistant_ids:
  81. delete_session_with_chat_assistants(get_http_api_auth, chat_assistant_id)
  82. request.addfinalizer(cleanup)
  83. @pytest.fixture
  84. def generate_test_files(request, tmp_path):
  85. file_creators = {
  86. "docx": (tmp_path / "ragflow_test.docx", create_docx_file),
  87. "excel": (tmp_path / "ragflow_test.xlsx", create_excel_file),
  88. "ppt": (tmp_path / "ragflow_test.pptx", create_ppt_file),
  89. "image": (tmp_path / "ragflow_test.png", create_image_file),
  90. "pdf": (tmp_path / "ragflow_test.pdf", create_pdf_file),
  91. "txt": (tmp_path / "ragflow_test.txt", create_txt_file),
  92. "md": (tmp_path / "ragflow_test.md", create_md_file),
  93. "json": (tmp_path / "ragflow_test.json", create_json_file),
  94. "eml": (tmp_path / "ragflow_test.eml", create_eml_file),
  95. "html": (tmp_path / "ragflow_test.html", create_html_file),
  96. }
  97. files = {}
  98. for file_type, (file_path, creator_func) in file_creators.items():
  99. if request.param in ["", file_type]:
  100. creator_func(file_path)
  101. files[file_type] = file_path
  102. return files
  103. @pytest.fixture(scope="class")
  104. def ragflow_tmp_dir(request, tmp_path_factory):
  105. class_name = request.cls.__name__
  106. return tmp_path_factory.mktemp(class_name)
  107. @pytest.fixture(scope="class")
  108. def add_dataset(request, get_http_api_auth):
  109. def cleanup():
  110. delete_datasets(get_http_api_auth, {"ids": None})
  111. request.addfinalizer(cleanup)
  112. dataset_ids = batch_create_datasets(get_http_api_auth, 1)
  113. return dataset_ids[0]
  114. @pytest.fixture(scope="function")
  115. def add_dataset_func(request, get_http_api_auth):
  116. def cleanup():
  117. delete_datasets(get_http_api_auth, {"ids": None})
  118. request.addfinalizer(cleanup)
  119. return batch_create_datasets(get_http_api_auth, 1)[0]
  120. @pytest.fixture(scope="class")
  121. def add_document(get_http_api_auth, add_dataset, ragflow_tmp_dir):
  122. dataset_id = add_dataset
  123. document_ids = bulk_upload_documents(get_http_api_auth, dataset_id, 1, ragflow_tmp_dir)
  124. return dataset_id, document_ids[0]
  125. @pytest.fixture(scope="class")
  126. def add_chunks(get_http_api_auth, add_document):
  127. dataset_id, document_id = add_document
  128. parse_documnets(get_http_api_auth, dataset_id, {"document_ids": [document_id]})
  129. condition(get_http_api_auth, dataset_id)
  130. chunk_ids = []
  131. for i in range(4):
  132. res = add_chunk(get_http_api_auth, dataset_id, document_id, {"content": f"chunk test {i}"})
  133. chunk_ids.append(res["data"]["chunk"]["id"])
  134. # issues/6487
  135. from time import sleep
  136. sleep(1)
  137. return dataset_id, document_id, chunk_ids
  138. @pytest.fixture(scope="class")
  139. def add_chat_assistants(request, get_http_api_auth, add_document):
  140. def cleanup():
  141. delete_chat_assistants(get_http_api_auth)
  142. request.addfinalizer(cleanup)
  143. dataset_id, document_id = add_document
  144. parse_documnets(get_http_api_auth, dataset_id, {"document_ids": [document_id]})
  145. condition(get_http_api_auth, dataset_id)
  146. chat_assistant_ids = []
  147. for i in range(5):
  148. res = create_chat_assistant(get_http_api_auth, {"name": f"test_chat_assistant_{i}", "dataset_ids": [dataset_id]})
  149. chat_assistant_ids.append(res["data"]["id"])
  150. return dataset_id, document_id, chat_assistant_ids