Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

t_document.py 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. from ragflow import RAGFlow, DataSet, Document
  2. from common import API_KEY, HOST_ADDRESS
  3. from test_sdkbase import TestSdk
  4. class TestDocument(TestSdk):
  5. def test_upload_document_with_success(self):
  6. """
  7. Test ingesting a document into a dataset with success.
  8. """
  9. # Initialize RAGFlow instance
  10. rag = RAGFlow(API_KEY, HOST_ADDRESS)
  11. # Step 1: Create a new dataset
  12. ds = rag.create_dataset(name="God")
  13. # Ensure dataset creation was successful
  14. assert isinstance(ds, DataSet), f"Failed to create dataset, error: {ds}"
  15. assert ds.name == "God", "Dataset name does not match."
  16. # Step 2: Create a new document
  17. # The blob is the actual file content or a placeholder in this case
  18. name = "TestDocument.txt"
  19. blob = b"Sample document content for ingestion test."
  20. res = rag.create_document(ds, name=name, blob=blob)
  21. # Ensure document ingestion was successful
  22. assert res is True, f"Failed to create document, error: {res}"
  23. def test_get_detail_document_with_success(self):
  24. """
  25. Test getting a document's detail with success
  26. """
  27. rag = RAGFlow(API_KEY, HOST_ADDRESS)
  28. doc = rag.get_document(name="TestDocument.txt")
  29. assert isinstance(doc, Document), f"Failed to get dataset, error: {doc}."
  30. assert doc.name == "TestDocument.txt", "Name does not match"
  31. def test_update_document_with_success(self):
  32. """
  33. Test updating a document with success.
  34. """
  35. rag = RAGFlow(API_KEY, HOST_ADDRESS)
  36. doc = rag.get_document(name="TestDocument.txt")
  37. if isinstance(doc, Document):
  38. doc.parser_method = "manual"
  39. res = doc.save()
  40. assert res is True, f"Failed to update document, error: {res}"
  41. else:
  42. assert False, f"Failed to get document, error: {doc}"
  43. def test_download_document_with_success(self):
  44. """
  45. Test downloading a document with success.
  46. """
  47. # Initialize RAGFlow instance
  48. rag = RAGFlow(API_KEY, HOST_ADDRESS)
  49. # Retrieve a document
  50. doc = rag.get_document(name="TestDocument.txt")
  51. # Check if the retrieved document is of type Document
  52. if isinstance(doc, Document):
  53. # Download the document content and save it to a file
  54. try:
  55. with open("ragflow.txt", "wb+") as file:
  56. file.write(doc.download())
  57. # Print the document object for debugging
  58. print(doc)
  59. # Assert that the download was successful
  60. assert True, "Document downloaded successfully."
  61. except Exception as e:
  62. # If an error occurs, raise an assertion error
  63. assert False, f"Failed to download document, error: {str(e)}"
  64. else:
  65. # If the document retrieval fails, assert failure
  66. assert False, f"Failed to get document, error: {doc}"
  67. def test_list_all_documents_in_dataset_with_success(self):
  68. """
  69. Test list all documents into a dataset with success.
  70. """
  71. # Initialize RAGFlow instance
  72. rag = RAGFlow(API_KEY, HOST_ADDRESS)
  73. # Step 1: Create a new dataset
  74. ds = rag.create_dataset(name="God2")
  75. # Ensure dataset creation was successful
  76. assert isinstance(ds, DataSet), f"Failed to create dataset, error: {ds}"
  77. assert ds.name == "God2", "Dataset name does not match."
  78. # Step 2: Create a new document
  79. # The blob is the actual file content or a placeholder in this case
  80. name1 = "Test Document111.txt"
  81. blob1 = b"Sample document content for ingestion test111."
  82. name2 = "Test Document222.txt"
  83. blob2 = b"Sample document content for ingestion test222."
  84. rag.create_document(ds, name=name1, blob=blob1)
  85. rag.create_document(ds, name=name2, blob=blob2)
  86. for d in ds.list_docs(keywords="test", offset=0, limit=12):
  87. assert isinstance(d, Document)
  88. print(d)
  89. def test_delete_documents_in_dataset_with_success(self):
  90. """
  91. Test list all documents into a dataset with success.
  92. """
  93. # Initialize RAGFlow instance
  94. rag = RAGFlow(API_KEY, HOST_ADDRESS)
  95. # Step 1: Create a new dataset
  96. ds = rag.create_dataset(name="God3")
  97. # Ensure dataset creation was successful
  98. assert isinstance(ds, DataSet), f"Failed to create dataset, error: {ds}"
  99. assert ds.name == "God3", "Dataset name does not match."
  100. # Step 2: Create a new document
  101. # The blob is the actual file content or a placeholder in this case
  102. name1 = "Test Document333.txt"
  103. blob1 = b"Sample document content for ingestion test333."
  104. name2 = "Test Document444.txt"
  105. blob2 = b"Sample document content for ingestion test444."
  106. name3='test.txt'
  107. path='test_data/test.txt'
  108. rag.create_document(ds, name=name3, blob=open(path, "rb").read())
  109. rag.create_document(ds, name=name1, blob=blob1)
  110. rag.create_document(ds, name=name2, blob=blob2)
  111. for d in ds.list_docs(keywords="document", offset=0, limit=12):
  112. assert isinstance(d, Document)
  113. d.delete()
  114. print(d)
  115. remaining_docs = ds.list_docs(keywords="rag", offset=0, limit=12)
  116. assert len(remaining_docs) == 0, "Documents were not properly deleted."