Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

test_document.py 51KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130
  1. from api.settings import RetCode
  2. from test_sdkbase import TestSdk
  3. from ragflow import RAGFlow
  4. import pytest
  5. from common import API_KEY, HOST_ADDRESS
  6. class TestFile(TestSdk):
  7. """
  8. This class contains a suite of tests for the content management functionality within the dataset.
  9. It ensures that the following functionalities as expected:
  10. 1. upload local files
  11. 2. upload remote files
  12. 3. download a file
  13. 4. delete a file
  14. 5. enable rename
  15. 6. list files
  16. 7. start parsing
  17. 8. end parsing
  18. 9. check the status of the file
  19. 10. list the chunks
  20. 11. delete a chunk
  21. 12. insert a new chunk
  22. 13. edit the status of chunk
  23. 14. get the specific chunk
  24. 15. retrieval test
  25. """
  26. # ----------------------------upload local files-----------------------------------------------------
  27. def test_upload_two_files(self):
  28. """
  29. Test uploading two files with success.
  30. """
  31. ragflow = RAGFlow(API_KEY, HOST_ADDRESS)
  32. created_res = ragflow.create_dataset("test_upload_two_files")
  33. dataset_id = created_res["data"]["dataset_id"]
  34. file_paths = ["test_data/test.txt", "test_data/test1.txt"]
  35. res = ragflow.upload_local_file(dataset_id, file_paths)
  36. assert res["code"] == RetCode.SUCCESS and res["message"] == "success"
  37. def test_upload_one_file(self):
  38. """
  39. Test uploading one file with success.
  40. """
  41. ragflow = RAGFlow(API_KEY, HOST_ADDRESS)
  42. created_res = ragflow.create_dataset("test_upload_one_file")
  43. dataset_id = created_res["data"]["dataset_id"]
  44. file_paths = ["test_data/test.txt"]
  45. res = ragflow.upload_local_file(dataset_id, file_paths)
  46. assert res["code"] == RetCode.SUCCESS and res["message"] == "success"
  47. def test_upload_nonexistent_files(self):
  48. """
  49. Test uploading a file which does not exist.
  50. """
  51. ragflow = RAGFlow(API_KEY, HOST_ADDRESS)
  52. created_res = ragflow.create_dataset("test_upload_nonexistent_files")
  53. dataset_id = created_res["data"]["dataset_id"]
  54. file_paths = ["test_data/imagination.txt"]
  55. res = ragflow.upload_local_file(dataset_id, file_paths)
  56. assert res["code"] == RetCode.DATA_ERROR and "does not exist" in res["message"]
  57. def test_upload_file_if_dataset_does_not_exist(self):
  58. """
  59. Test uploading files if the dataset id does not exist.
  60. """
  61. ragflow = RAGFlow(API_KEY, HOST_ADDRESS)
  62. file_paths = ["test_data/test.txt"]
  63. res = ragflow.upload_local_file("111", file_paths)
  64. assert res["code"] == RetCode.DATA_ERROR and res["message"] == "Can't find this dataset"
  65. def test_upload_file_without_name(self):
  66. """
  67. Test uploading files that do not have name.
  68. """
  69. ragflow = RAGFlow(API_KEY, HOST_ADDRESS)
  70. created_res = ragflow.create_dataset("test_upload_file_without_name")
  71. dataset_id = created_res["data"]["dataset_id"]
  72. file_paths = ["test_data/.txt"]
  73. res = ragflow.upload_local_file(dataset_id, file_paths)
  74. assert res["code"] == RetCode.SUCCESS
  75. def test_upload_file_without_name1(self):
  76. """
  77. Test uploading files that do not have name.
  78. """
  79. ragflow = RAGFlow(API_KEY, HOST_ADDRESS)
  80. created_res = ragflow.create_dataset("test_upload_file_without_name")
  81. dataset_id = created_res["data"]["dataset_id"]
  82. file_paths = ["test_data/.txt", "test_data/empty.txt"]
  83. res = ragflow.upload_local_file(dataset_id, file_paths)
  84. assert res["code"] == RetCode.SUCCESS
  85. def test_upload_files_exceeding_the_number_limit(self):
  86. """
  87. Test uploading files whose number exceeds the limit.
  88. """
  89. ragflow = RAGFlow(API_KEY, HOST_ADDRESS)
  90. created_res = ragflow.create_dataset("test_upload_files_exceeding_the_number_limit")
  91. dataset_id = created_res["data"]["dataset_id"]
  92. file_paths = ["test_data/test.txt", "test_data/test1.txt"] * 256
  93. res = ragflow.upload_local_file(dataset_id, file_paths)
  94. assert (res["message"] ==
  95. "You try to upload 512 files, which exceeds the maximum number of uploading files: 256"
  96. and res["code"] == RetCode.DATA_ERROR)
  97. def test_upload_files_without_files(self):
  98. """
  99. Test uploading files without files.
  100. """
  101. ragflow = RAGFlow(API_KEY, HOST_ADDRESS)
  102. created_res = ragflow.create_dataset("test_upload_files_without_files")
  103. dataset_id = created_res["data"]["dataset_id"]
  104. file_paths = [None]
  105. res = ragflow.upload_local_file(dataset_id, file_paths)
  106. assert (res["message"] == "None is not string." and res["code"] == RetCode.ARGUMENT_ERROR)
  107. def test_upload_files_with_two_files_with_same_name(self):
  108. """
  109. Test uploading files with the same name.
  110. """
  111. ragflow = RAGFlow(API_KEY, HOST_ADDRESS)
  112. created_res = ragflow.create_dataset("test_upload_files_with_two_files_with_same_name")
  113. dataset_id = created_res["data"]["dataset_id"]
  114. file_paths = ["test_data/test.txt"] * 2
  115. res = ragflow.upload_local_file(dataset_id, file_paths)
  116. assert (res["message"] == "success" and res["code"] == RetCode.SUCCESS)
  117. def test_upload_files_with_file_paths(self):
  118. """
  119. Test uploading files with only specifying the file path's repo.
  120. """
  121. ragflow = RAGFlow(API_KEY, HOST_ADDRESS)
  122. created_res = ragflow.create_dataset("test_upload_files_with_file_paths")
  123. dataset_id = created_res["data"]["dataset_id"]
  124. file_paths = ["test_data/"]
  125. res = ragflow.upload_local_file(dataset_id, file_paths)
  126. assert (res["message"] == "The file test_data/ does not exist" and res["code"] == RetCode.DATA_ERROR)
  127. def test_upload_files_with_remote_file_path(self):
  128. """
  129. Test uploading files with remote files.
  130. """
  131. ragflow = RAGFlow(API_KEY, HOST_ADDRESS)
  132. created_res = ragflow.create_dataset("test_upload_files_with_remote_file_path")
  133. dataset_id = created_res["data"]["dataset_id"]
  134. file_paths = ["https://github.com/genostack/ragflow"]
  135. res = ragflow.upload_local_file(dataset_id, file_paths)
  136. assert res["code"] == RetCode.ARGUMENT_ERROR and res["message"] == "Remote files have not unsupported."
  137. # ----------------------------delete a file-----------------------------------------------------
  138. def test_delete_one_file(self):
  139. """
  140. Test deleting one file with success.
  141. """
  142. ragflow = RAGFlow(API_KEY, HOST_ADDRESS)
  143. created_res = ragflow.create_dataset("test_delete_one_file")
  144. dataset_id = created_res["data"]["dataset_id"]
  145. file_paths = ["test_data/test.txt"]
  146. res = ragflow.upload_local_file(dataset_id, file_paths)
  147. # get the doc_id
  148. data = res["data"][0]
  149. doc_id = data["id"]
  150. # delete the files
  151. deleted_res = ragflow.delete_files(doc_id, dataset_id)
  152. # assert value
  153. assert deleted_res["code"] == RetCode.SUCCESS and deleted_res["data"] is True
  154. def test_delete_document_with_not_existing_document(self):
  155. """
  156. Test deleting a document that does not exist with failure.
  157. """
  158. ragflow = RAGFlow(API_KEY, HOST_ADDRESS)
  159. created_res = ragflow.create_dataset("test_delete_document_with_not_existing_document")
  160. dataset_id = created_res["data"]["dataset_id"]
  161. res = ragflow.delete_files("111", dataset_id)
  162. assert res["code"] == RetCode.DATA_ERROR and res["message"] == "Document 111 not found!"
  163. def test_delete_document_with_creating_100_documents_and_deleting_100_documents(self):
  164. """
  165. Test deleting documents when uploading 100 docs and deleting 100 docs.
  166. """
  167. # upload 100 docs
  168. ragflow = RAGFlow(API_KEY, HOST_ADDRESS)
  169. created_res = ragflow.create_dataset("test_delete_one_file")
  170. dataset_id = created_res["data"]["dataset_id"]
  171. file_paths = ["test_data/test.txt"] * 100
  172. res = ragflow.upload_local_file(dataset_id, file_paths)
  173. # get the doc_id
  174. data = res["data"]
  175. for d in data:
  176. doc_id = d["id"]
  177. # delete the files
  178. deleted_res = ragflow.delete_files(doc_id, dataset_id)
  179. # assert value
  180. assert deleted_res["code"] == RetCode.SUCCESS and deleted_res["data"] is True
  181. def test_delete_document_from_nonexistent_dataset(self):
  182. """
  183. Test deleting documents from a non-existent dataset
  184. """
  185. ragflow = RAGFlow(API_KEY, HOST_ADDRESS)
  186. created_res = ragflow.create_dataset("test_delete_one_file")
  187. dataset_id = created_res["data"]["dataset_id"]
  188. file_paths = ["test_data/test.txt"]
  189. res = ragflow.upload_local_file(dataset_id, file_paths)
  190. # get the doc_id
  191. data = res["data"][0]
  192. doc_id = data["id"]
  193. # delete the files
  194. deleted_res = ragflow.delete_files(doc_id, "000")
  195. # assert value
  196. assert (deleted_res["code"] == RetCode.ARGUMENT_ERROR and deleted_res["message"] ==
  197. f"The document {doc_id} is not in the dataset: 000, but in the dataset: {dataset_id}.")
  198. def test_delete_document_which_is_located_in_other_dataset(self):
  199. """
  200. Test deleting a document which is located in other dataset.
  201. """
  202. ragflow = RAGFlow(API_KEY, HOST_ADDRESS)
  203. # upload a document
  204. created_res = ragflow.create_dataset("test_delete_document_which_is_located_in_other_dataset")
  205. created_res_id = created_res["data"]["dataset_id"]
  206. file_paths = ["test_data/test.txt"]
  207. res = ragflow.upload_local_file(created_res_id, file_paths)
  208. # other dataset
  209. other_res = ragflow.create_dataset("other_dataset")
  210. other_dataset_id = other_res["data"]["dataset_id"]
  211. # get the doc_id
  212. data = res["data"][0]
  213. doc_id = data["id"]
  214. # delete the files from the other dataset
  215. deleted_res = ragflow.delete_files(doc_id, other_dataset_id)
  216. # assert value
  217. assert (deleted_res["code"] == RetCode.ARGUMENT_ERROR and deleted_res["message"] ==
  218. f"The document {doc_id} is not in the dataset: {other_dataset_id}, but in the dataset: {created_res_id}.")
  219. # ----------------------------list files-----------------------------------------------------
  220. def test_list_documents_with_success(self):
  221. """
  222. Test listing documents with a successful outcome.
  223. """
  224. ragflow = RAGFlow(API_KEY, HOST_ADDRESS)
  225. # upload a document
  226. created_res = ragflow.create_dataset("test_list_documents_with_success")
  227. created_res_id = created_res["data"]["dataset_id"]
  228. file_paths = ["test_data/test.txt"]
  229. ragflow.upload_local_file(created_res_id, file_paths)
  230. # Call the list_document method
  231. response = ragflow.list_files(created_res_id)
  232. assert response["code"] == RetCode.SUCCESS and len(response["data"]["docs"]) == 1
  233. def test_list_documents_with_checking_size(self):
  234. """
  235. Test listing documents and verify the size and names of the documents.
  236. """
  237. ragflow = RAGFlow(API_KEY, HOST_ADDRESS)
  238. # upload 10 documents
  239. created_res = ragflow.create_dataset("test_list_documents_with_checking_size")
  240. created_res_id = created_res["data"]["dataset_id"]
  241. file_paths = ["test_data/test.txt"] * 10
  242. ragflow.upload_local_file(created_res_id, file_paths)
  243. # Call the list_document method
  244. response = ragflow.list_files(created_res_id)
  245. assert response["code"] == RetCode.SUCCESS and len(response["data"]["docs"]) == 10
  246. def test_list_documents_with_getting_empty_result(self):
  247. """
  248. Test listing documents that should be empty.
  249. """
  250. ragflow = RAGFlow(API_KEY, HOST_ADDRESS)
  251. # upload 0 documents
  252. created_res = ragflow.create_dataset("test_list_documents_with_getting_empty_result")
  253. created_res_id = created_res["data"]["dataset_id"]
  254. # Call the list_document method
  255. response = ragflow.list_files(created_res_id)
  256. assert response["code"] == RetCode.SUCCESS and len(response["data"]["docs"]) == 0
  257. def test_list_documents_with_creating_100_documents(self):
  258. """
  259. Test listing 100 documents and verify the size of these documents.
  260. """
  261. ragflow = RAGFlow(API_KEY, HOST_ADDRESS)
  262. # upload 100 documents
  263. created_res = ragflow.create_dataset("test_list_documents_with_creating_100_documents")
  264. created_res_id = created_res["data"]["dataset_id"]
  265. file_paths = ["test_data/test.txt"] * 100
  266. ragflow.upload_local_file(created_res_id, file_paths)
  267. # Call the list_document method
  268. response = ragflow.list_files(created_res_id)
  269. assert response["code"] == RetCode.SUCCESS and len(response["data"]["docs"]) == 100
  270. def test_list_document_with_failure(self):
  271. """
  272. Test listing documents with IndexError.
  273. """
  274. ragflow = RAGFlow(API_KEY, HOST_ADDRESS)
  275. created_res = ragflow.create_dataset("test_list_document_with_failure")
  276. created_res_id = created_res["data"]["dataset_id"]
  277. response = ragflow.list_files(created_res_id, offset=-1, count=-1)
  278. assert "IndexError" in response["message"] and response["code"] == RetCode.EXCEPTION_ERROR
  279. def test_list_document_with_verifying_offset_and_count(self):
  280. """
  281. Test listing documents with verifying the functionalities of offset and count.
  282. """
  283. ragflow = RAGFlow(API_KEY, HOST_ADDRESS)
  284. created_res = ragflow.create_dataset("test_list_document_with_verifying_offset_and_count")
  285. created_res_id = created_res["data"]["dataset_id"]
  286. file_paths = ["test_data/test.txt", "test_data/empty.txt"] * 10
  287. ragflow.upload_local_file(created_res_id, file_paths)
  288. # Call the list_document method
  289. response = ragflow.list_files(created_res_id, offset=2, count=10)
  290. assert response["code"] == RetCode.SUCCESS and len(response["data"]["docs"]) == 10
  291. def test_list_document_with_verifying_keywords(self):
  292. """
  293. Test listing documents with verifying the functionality of searching keywords.
  294. """
  295. ragflow = RAGFlow(API_KEY, HOST_ADDRESS)
  296. created_res = ragflow.create_dataset("test_list_document_with_verifying_keywords")
  297. created_res_id = created_res["data"]["dataset_id"]
  298. file_paths = ["test_data/test.txt", "test_data/empty.txt"]
  299. ragflow.upload_local_file(created_res_id, file_paths)
  300. # Call the list_document method
  301. response = ragflow.list_files(created_res_id, keywords="empty")
  302. assert response["code"] == RetCode.SUCCESS and len(response["data"]["docs"]) == 1
  303. def test_list_document_with_verifying_order_by_and_descend(self):
  304. """
  305. Test listing documents with verifying the functionality of order_by and descend.
  306. """
  307. ragflow = RAGFlow(API_KEY, HOST_ADDRESS)
  308. created_res = ragflow.create_dataset("test_list_document_with_verifying_order_by_and_descend")
  309. created_res_id = created_res["data"]["dataset_id"]
  310. file_paths = ["test_data/test.txt", "test_data/empty.txt"]
  311. ragflow.upload_local_file(created_res_id, file_paths)
  312. # Call the list_document method
  313. response = ragflow.list_files(created_res_id)
  314. assert response["code"] == RetCode.SUCCESS and len(response["data"]["docs"]) == 2
  315. docs = response["data"]["docs"]
  316. # reverse
  317. i = 1
  318. for doc in docs:
  319. assert doc["name"] in file_paths[i]
  320. i -= 1
  321. def test_list_document_with_verifying_order_by_and_ascend(self):
  322. """
  323. Test listing documents with verifying the functionality of order_by and ascend.
  324. """
  325. ragflow = RAGFlow(API_KEY, HOST_ADDRESS)
  326. created_res = ragflow.create_dataset("test_list_document_with_verifying_order_by_and_ascend")
  327. created_res_id = created_res["data"]["dataset_id"]
  328. file_paths = ["test_data/test.txt", "test_data/test1.txt", "test_data/empty.txt"]
  329. ragflow.upload_local_file(created_res_id, file_paths)
  330. # Call the list_document method
  331. response = ragflow.list_files(created_res_id, descend=False)
  332. assert response["code"] == RetCode.SUCCESS and len(response["data"]["docs"]) == 3
  333. docs = response["data"]["docs"]
  334. i = 0
  335. for doc in docs:
  336. assert doc["name"] in file_paths[i]
  337. i += 1
  338. # ----------------------------update files: enable, rename, template_type-------------------------------------------
  339. def test_update_nonexistent_document(self):
  340. """
  341. Test updating a document which does not exist.
  342. """
  343. ragflow = RAGFlow(API_KEY, HOST_ADDRESS)
  344. created_res = ragflow.create_dataset("test_update_nonexistent_document")
  345. created_res_id = created_res["data"]["dataset_id"]
  346. params = {
  347. "name": "new_name"
  348. }
  349. res = ragflow.update_file(created_res_id, "weird_doc_id", **params)
  350. assert res["code"] == RetCode.ARGUMENT_ERROR and res["message"] == f"This document weird_doc_id cannot be found!"
  351. def test_update_document_without_parameters(self):
  352. """
  353. Test updating a document without giving parameters.
  354. """
  355. # create a dataset
  356. ragflow = RAGFlow(API_KEY, HOST_ADDRESS)
  357. created_res = ragflow.create_dataset("test_update_document_without_parameters")
  358. created_res_id = created_res["data"]["dataset_id"]
  359. # upload files
  360. file_paths = ["test_data/test.txt"]
  361. uploading_res = ragflow.upload_local_file(created_res_id, file_paths)
  362. # get the doc_id
  363. data = uploading_res["data"][0]
  364. doc_id = data["id"]
  365. # update file
  366. params = {
  367. }
  368. update_res = ragflow.update_file(created_res_id, doc_id, **params)
  369. assert (update_res["code"] == RetCode.DATA_ERROR and
  370. update_res["message"] == "Please input at least one parameter that you want to update!")
  371. def test_update_document_in_nonexistent_dataset(self):
  372. """
  373. Test updating a document in the nonexistent dataset.
  374. """
  375. # create a dataset
  376. ragflow = RAGFlow(API_KEY, HOST_ADDRESS)
  377. created_res = ragflow.create_dataset("test_update_document_in_nonexistent_dataset")
  378. created_res_id = created_res["data"]["dataset_id"]
  379. # upload files
  380. file_paths = ["test_data/test.txt"]
  381. uploading_res = ragflow.upload_local_file(created_res_id, file_paths)
  382. # get the doc_id
  383. data = uploading_res["data"][0]
  384. doc_id = data["id"]
  385. # update file
  386. params = {
  387. "name": "new_name"
  388. }
  389. update_res = ragflow.update_file("fake_dataset_id", doc_id, **params)
  390. assert (update_res["code"] == RetCode.DATA_ERROR and
  391. update_res["message"] == f"This dataset fake_dataset_id cannot be found!")
  392. def test_update_document_with_different_extension_name(self):
  393. """
  394. Test the updating of a document with an extension name that differs from its original.
  395. """
  396. # create a dataset
  397. ragflow = RAGFlow(API_KEY, HOST_ADDRESS)
  398. created_res = ragflow.create_dataset("test_update_document_with_different_extension_name")
  399. created_res_id = created_res["data"]["dataset_id"]
  400. # upload files
  401. file_paths = ["test_data/test.txt"]
  402. uploading_res = ragflow.upload_local_file(created_res_id, file_paths)
  403. # get the doc_id
  404. data = uploading_res["data"][0]
  405. doc_id = data["id"]
  406. # update file
  407. params = {
  408. "name": "new_name.doc"
  409. }
  410. update_res = ragflow.update_file(created_res_id, doc_id, **params)
  411. assert (update_res["code"] == RetCode.ARGUMENT_ERROR and
  412. update_res["message"] == "The extension of file cannot be changed")
  413. def test_update_document_with_duplicate_name(self):
  414. """
  415. Test the updating of a document with a duplicate name.
  416. """
  417. # create a dataset
  418. ragflow = RAGFlow(API_KEY, HOST_ADDRESS)
  419. created_res = ragflow.create_dataset("test_update_document_with_different_extension_name")
  420. created_res_id = created_res["data"]["dataset_id"]
  421. # upload files
  422. file_paths = ["test_data/test.txt", "test_data/test1.txt"]
  423. uploading_res = ragflow.upload_local_file(created_res_id, file_paths)
  424. # get the doc_id
  425. data = uploading_res["data"][0]
  426. doc_id = data["id"]
  427. # update file
  428. params = {
  429. "name": "test.txt"
  430. }
  431. update_res = ragflow.update_file(created_res_id, doc_id, **params)
  432. assert (update_res["code"] == RetCode.ARGUMENT_ERROR and
  433. update_res["message"] == "Duplicated document name in the same dataset.")
  434. def test_update_document_with_updating_its_name_with_success(self):
  435. """
  436. Test the updating of a document's name with success.
  437. """
  438. # create a dataset
  439. ragflow = RAGFlow(API_KEY, HOST_ADDRESS)
  440. created_res = ragflow.create_dataset("test_update_document_with_updating_its_name_with_success")
  441. created_res_id = created_res["data"]["dataset_id"]
  442. # upload files
  443. file_paths = ["test_data/test.txt", "test_data/test1.txt"]
  444. uploading_res = ragflow.upload_local_file(created_res_id, file_paths)
  445. # get the doc_id
  446. data = uploading_res["data"][0]
  447. doc_id = data["id"]
  448. # update file
  449. params = {
  450. "name": "new_name.txt"
  451. }
  452. update_res = ragflow.update_file(created_res_id, doc_id, **params)
  453. assert (update_res["code"] == RetCode.SUCCESS and
  454. update_res["message"] == "Success" and update_res["data"]["name"] == "new_name.txt")
  455. def test_update_document_with_updating_its_template_type_with_success(self):
  456. """
  457. Test the updating of a document's template type with success.
  458. """
  459. # create a dataset
  460. ragflow = RAGFlow(API_KEY, HOST_ADDRESS)
  461. created_res = ragflow.create_dataset("test_update_document_with_updating_its_template_type_with_success")
  462. created_res_id = created_res["data"]["dataset_id"]
  463. # upload files
  464. file_paths = ["test_data/test.txt", "test_data/test1.txt"]
  465. uploading_res = ragflow.upload_local_file(created_res_id, file_paths)
  466. # get the doc_id
  467. data = uploading_res["data"][0]
  468. doc_id = data["id"]
  469. # update file
  470. params = {
  471. "template_type": "laws"
  472. }
  473. update_res = ragflow.update_file(created_res_id, doc_id, **params)
  474. assert (update_res["code"] == RetCode.SUCCESS and
  475. update_res["message"] == "Success" and update_res["data"]["parser_id"] == "laws")
  476. def test_update_document_with_updating_its_enable_value_with_success(self):
  477. """
  478. Test the updating of a document's enable value with success.
  479. """
  480. # create a dataset
  481. ragflow = RAGFlow(API_KEY, HOST_ADDRESS)
  482. created_res = ragflow.create_dataset("test_update_document_with_updating_its_enable_value_with_success")
  483. created_res_id = created_res["data"]["dataset_id"]
  484. # upload files
  485. file_paths = ["test_data/test.txt", "test_data/test1.txt"]
  486. uploading_res = ragflow.upload_local_file(created_res_id, file_paths)
  487. # get the doc_id
  488. data = uploading_res["data"][0]
  489. doc_id = data["id"]
  490. # update file
  491. params = {
  492. "enable": "0"
  493. }
  494. update_res = ragflow.update_file(created_res_id, doc_id, **params)
  495. assert (update_res["code"] == RetCode.SUCCESS and
  496. update_res["message"] == "Success" and update_res["data"]["status"] == "0")
  497. def test_update_document_with_updating_illegal_parameter(self):
  498. """
  499. Test the updating of a document's illegal parameter.
  500. """
  501. # create a dataset
  502. ragflow = RAGFlow(API_KEY, HOST_ADDRESS)
  503. created_res = ragflow.create_dataset("test_update_document_with_updating_illegal_parameter")
  504. created_res_id = created_res["data"]["dataset_id"]
  505. # upload files
  506. file_paths = ["test_data/test.txt", "test_data/test1.txt"]
  507. uploading_res = ragflow.upload_local_file(created_res_id, file_paths)
  508. # get the doc_id
  509. data = uploading_res["data"][0]
  510. doc_id = data["id"]
  511. # update file
  512. params = {
  513. "illegal_parameter": "0"
  514. }
  515. update_res = ragflow.update_file(created_res_id, doc_id, **params)
  516. assert (update_res["code"] == RetCode.ARGUMENT_ERROR and
  517. update_res["message"] == "illegal_parameter is an illegal parameter.")
  518. def test_update_document_with_giving_its_name_value(self):
  519. """
  520. Test the updating of a document's name without its name value.
  521. """
  522. # create a dataset
  523. ragflow = RAGFlow(API_KEY, HOST_ADDRESS)
  524. created_res = ragflow.create_dataset("test_update_document_with_updating_its_name_with_success")
  525. created_res_id = created_res["data"]["dataset_id"]
  526. # upload files
  527. file_paths = ["test_data/test.txt", "test_data/test1.txt"]
  528. uploading_res = ragflow.upload_local_file(created_res_id, file_paths)
  529. # get the doc_id
  530. data = uploading_res["data"][0]
  531. doc_id = data["id"]
  532. # update file
  533. params = {
  534. "name": ""
  535. }
  536. update_res = ragflow.update_file(created_res_id, doc_id, **params)
  537. assert (update_res["code"] == RetCode.DATA_ERROR and
  538. update_res["message"] == "There is no new name.")
  539. def test_update_document_with_giving_illegal_value_for_enable(self):
  540. """
  541. Test the updating of a document's with giving illegal enable's value.
  542. """
  543. # create a dataset
  544. ragflow = RAGFlow(API_KEY, HOST_ADDRESS)
  545. created_res = ragflow.create_dataset("test_update_document_with_updating_its_name_with_success")
  546. created_res_id = created_res["data"]["dataset_id"]
  547. # upload files
  548. file_paths = ["test_data/test.txt", "test_data/test1.txt"]
  549. uploading_res = ragflow.upload_local_file(created_res_id, file_paths)
  550. # get the doc_id
  551. data = uploading_res["data"][0]
  552. doc_id = data["id"]
  553. # update file
  554. params = {
  555. "enable": "?"
  556. }
  557. update_res = ragflow.update_file(created_res_id, doc_id, **params)
  558. assert (update_res["code"] == RetCode.DATA_ERROR and
  559. update_res["message"] == "Illegal value ? for 'enable' field.")
  560. def test_update_document_with_giving_illegal_value_for_type(self):
  561. """
  562. Test the updating of a document's with giving illegal type's value.
  563. """
  564. # create a dataset
  565. ragflow = RAGFlow(API_KEY, HOST_ADDRESS)
  566. created_res = ragflow.create_dataset("test_update_document_with_updating_its_name_with_success")
  567. created_res_id = created_res["data"]["dataset_id"]
  568. # upload files
  569. file_paths = ["test_data/test.txt", "test_data/test1.txt"]
  570. uploading_res = ragflow.upload_local_file(created_res_id, file_paths)
  571. # get the doc_id
  572. data = uploading_res["data"][0]
  573. doc_id = data["id"]
  574. # update file
  575. params = {
  576. "template_type": "?"
  577. }
  578. update_res = ragflow.update_file(created_res_id, doc_id, **params)
  579. assert (update_res["code"] == RetCode.DATA_ERROR and
  580. update_res["message"] == "Illegal value ? for 'template_type' field.")
  581. # ----------------------------download a file-----------------------------------------------------
  582. def test_download_nonexistent_document(self):
  583. """
  584. Test downloading a document which does not exist.
  585. """
  586. # create a dataset
  587. ragflow = RAGFlow(API_KEY, HOST_ADDRESS)
  588. created_res = ragflow.create_dataset("test_download_nonexistent_document")
  589. created_res_id = created_res["data"]["dataset_id"]
  590. res = ragflow.download_file(created_res_id, "imagination")
  591. assert res["code"] == RetCode.ARGUMENT_ERROR and res["message"] == f"This document 'imagination' cannot be found!"
  592. def test_download_document_in_nonexistent_dataset(self):
  593. """
  594. Test downloading a document whose dataset is nonexistent.
  595. """
  596. # create a dataset
  597. ragflow = RAGFlow(API_KEY, HOST_ADDRESS)
  598. created_res = ragflow.create_dataset("test_download_nonexistent_document")
  599. created_res_id = created_res["data"]["dataset_id"]
  600. # upload files
  601. file_paths = ["test_data/test.txt"]
  602. uploading_res = ragflow.upload_local_file(created_res_id, file_paths)
  603. # get the doc_id
  604. data = uploading_res["data"][0]
  605. doc_id = data["id"]
  606. # download file
  607. res = ragflow.download_file("imagination", doc_id)
  608. assert res["code"] == RetCode.DATA_ERROR and res["message"] == f"This dataset 'imagination' cannot be found!"
  609. def test_download_document_with_success(self):
  610. """
  611. Test the downloading of a document with success.
  612. """
  613. # create a dataset
  614. ragflow = RAGFlow(API_KEY, HOST_ADDRESS)
  615. created_res = ragflow.create_dataset("test_download_nonexistent_document")
  616. created_res_id = created_res["data"]["dataset_id"]
  617. # upload files
  618. file_paths = ["test_data/test.txt"]
  619. uploading_res = ragflow.upload_local_file(created_res_id, file_paths)
  620. # get the doc_id
  621. data = uploading_res["data"][0]
  622. doc_id = data["id"]
  623. # download file
  624. with open("test_data/test.txt", "rb") as file:
  625. binary_data = file.read()
  626. res = ragflow.download_file(created_res_id, doc_id)
  627. assert res["code"] == RetCode.SUCCESS and res["data"] == binary_data
  628. def test_download_an_empty_document(self):
  629. """
  630. Test the downloading of an empty document.
  631. """
  632. # create a dataset
  633. ragflow = RAGFlow(API_KEY, HOST_ADDRESS)
  634. created_res = ragflow.create_dataset("test_download_nonexistent_document")
  635. created_res_id = created_res["data"]["dataset_id"]
  636. # upload files
  637. file_paths = ["test_data/empty.txt"]
  638. uploading_res = ragflow.upload_local_file(created_res_id, file_paths)
  639. # get the doc_id
  640. data = uploading_res["data"][0]
  641. doc_id = data["id"]
  642. # download file
  643. res = ragflow.download_file(created_res_id, doc_id)
  644. assert res["code"] == RetCode.DATA_ERROR and res["message"] == "This file is empty."
  645. # ----------------------------start parsing-----------------------------------------------------
  646. def test_start_parsing_document_with_success(self):
  647. """
  648. Test the parsing of a document with success.
  649. """
  650. # create a dataset
  651. ragflow = RAGFlow(API_KEY, HOST_ADDRESS)
  652. created_res = ragflow.create_dataset("test_start_parsing_document_with_success")
  653. created_res_id = created_res["data"]["dataset_id"]
  654. # upload files
  655. file_paths = ["test_data/lol.txt"]
  656. uploading_res = ragflow.upload_local_file(created_res_id, file_paths)
  657. # get the doc_id
  658. data = uploading_res["data"][0]
  659. doc_id = data["id"]
  660. # parse file
  661. res = ragflow.start_parsing_document(created_res_id, doc_id)
  662. assert res["code"] == RetCode.SUCCESS and res["message"] == ""
  663. def test_start_parsing_nonexistent_document(self):
  664. """
  665. Test the parsing a document which does not exist.
  666. """
  667. # create a dataset
  668. ragflow = RAGFlow(API_KEY, HOST_ADDRESS)
  669. created_res = ragflow.create_dataset("test_start_parsing_nonexistent_document")
  670. created_res_id = created_res["data"]["dataset_id"]
  671. res = ragflow.start_parsing_document(created_res_id, "imagination")
  672. assert res["code"] == RetCode.ARGUMENT_ERROR and res["message"] == "This document 'imagination' cannot be found!"
  673. def test_start_parsing_document_in_nonexistent_dataset(self):
  674. """
  675. Test the parsing a document whose dataset is nonexistent.
  676. """
  677. # create a dataset
  678. ragflow = RAGFlow(API_KEY, HOST_ADDRESS)
  679. created_res = ragflow.create_dataset("test_download_nonexistent_document")
  680. created_res_id = created_res["data"]["dataset_id"]
  681. # upload files
  682. file_paths = ["test_data/test.txt"]
  683. uploading_res = ragflow.upload_local_file(created_res_id, file_paths)
  684. # get the doc_id
  685. data = uploading_res["data"][0]
  686. doc_id = data["id"]
  687. # parse
  688. res = ragflow.start_parsing_document("imagination", doc_id)
  689. assert res["code"] == RetCode.DATA_ERROR and res["message"] == "This dataset 'imagination' cannot be found!"
  690. def test_start_parsing_an_empty_document(self):
  691. """
  692. Test the parsing of an empty document.
  693. """
  694. # create a dataset
  695. ragflow = RAGFlow(API_KEY, HOST_ADDRESS)
  696. created_res = ragflow.create_dataset("test_download_nonexistent_document")
  697. created_res_id = created_res["data"]["dataset_id"]
  698. # upload files
  699. file_paths = ["test_data/empty.txt"]
  700. uploading_res = ragflow.upload_local_file(created_res_id, file_paths)
  701. # get the doc_id
  702. data = uploading_res["data"][0]
  703. doc_id = data["id"]
  704. res = ragflow.start_parsing_document(created_res_id, doc_id)
  705. assert res["code"] == RetCode.SUCCESS and res["message"] == "Empty data in the document: empty.txt; "
  706. # ------------------------parsing multiple documents----------------------------
  707. def test_start_parsing_documents_in_nonexistent_dataset(self):
  708. """
  709. Test the parsing documents whose dataset is nonexistent.
  710. """
  711. # create a dataset
  712. ragflow = RAGFlow(API_KEY, HOST_ADDRESS)
  713. created_res = ragflow.create_dataset("test_download_nonexistent_document")
  714. created_res_id = created_res["data"]["dataset_id"]
  715. # upload files
  716. file_paths = ["test_data/test.txt"]
  717. uploading_res = ragflow.upload_local_file(created_res_id, file_paths)
  718. # parse
  719. res = ragflow.start_parsing_documents("imagination")
  720. assert res["code"] == RetCode.DATA_ERROR and res["message"] == "This dataset 'imagination' cannot be found!"
  721. def test_start_parsing_multiple_documents(self):
  722. """
  723. Test the parsing documents with a success.
  724. """
  725. # create a dataset
  726. ragflow = RAGFlow(API_KEY, HOST_ADDRESS)
  727. created_res = ragflow.create_dataset(" test_start_parsing_multiple_documents")
  728. created_res_id = created_res["data"]["dataset_id"]
  729. # upload files
  730. file_paths = ["test_data/test.txt", "test_data/test1.txt"]
  731. ragflow.upload_local_file(created_res_id, file_paths)
  732. res = ragflow.start_parsing_documents(created_res_id)
  733. assert res["code"] == RetCode.SUCCESS and res["data"] is True and res["message"] == ""
  734. def test_start_parsing_multiple_documents_with_one_empty_file(self):
  735. """
  736. Test the parsing documents, one of which is empty.
  737. """
  738. # create a dataset
  739. ragflow = RAGFlow(API_KEY, HOST_ADDRESS)
  740. created_res = ragflow.create_dataset(" test_start_parsing_multiple_documents")
  741. created_res_id = created_res["data"]["dataset_id"]
  742. # upload files
  743. file_paths = ["test_data/test.txt", "test_data/test1.txt", "test_data/empty.txt"]
  744. ragflow.upload_local_file(created_res_id, file_paths)
  745. res = ragflow.start_parsing_documents(created_res_id)
  746. assert res["code"] == RetCode.SUCCESS and res["message"] == "Empty data in the document: empty.txt; "
  747. def test_start_parsing_multiple_specific_documents(self):
  748. """
  749. Test the parsing documents whose document ids are specified.
  750. """
  751. # create a dataset
  752. ragflow = RAGFlow(API_KEY, HOST_ADDRESS)
  753. created_res = ragflow.create_dataset(" test_start_parsing_multiple_documents")
  754. created_res_id = created_res["data"]["dataset_id"]
  755. # upload files
  756. file_paths = ["test_data/test.txt", "test_data/test1.txt"]
  757. uploading_res = ragflow.upload_local_file(created_res_id, file_paths)
  758. # get the doc_id
  759. data = uploading_res["data"]
  760. doc_ids = []
  761. for d in data:
  762. doc_ids.append(d["id"])
  763. res = ragflow.start_parsing_documents(created_res_id, doc_ids)
  764. assert res["code"] == RetCode.SUCCESS and res["message"] == ""
  765. def test_start_re_parsing_multiple_specific_documents(self):
  766. """
  767. Test the re-parsing documents.
  768. """
  769. # create a dataset
  770. ragflow = RAGFlow(API_KEY, HOST_ADDRESS)
  771. created_res = ragflow.create_dataset(" test_start_parsing_multiple_documents")
  772. created_res_id = created_res["data"]["dataset_id"]
  773. # upload files
  774. file_paths = ["test_data/test.txt", "test_data/test1.txt"]
  775. uploading_res = ragflow.upload_local_file(created_res_id, file_paths)
  776. # get the doc_id
  777. data = uploading_res["data"]
  778. doc_ids = []
  779. for d in data:
  780. doc_ids.append(d["id"])
  781. res = ragflow.start_parsing_documents(created_res_id, doc_ids)
  782. assert res["code"] == RetCode.SUCCESS and res["message"] == ""
  783. # re-parse
  784. res = ragflow.start_parsing_documents(created_res_id, doc_ids)
  785. assert res["code"] == RetCode.SUCCESS and res["message"] == ""
  786. def test_start_re_parsing_multiple_specific_documents_with_changing_parser_id(self):
  787. """
  788. Test the re-parsing documents after changing the parser id.
  789. """
  790. # create a dataset
  791. ragflow = RAGFlow(API_KEY, HOST_ADDRESS)
  792. created_res = ragflow.create_dataset(" test_start_parsing_multiple_documents")
  793. created_res_id = created_res["data"]["dataset_id"]
  794. # upload files
  795. file_paths = ["test_data/test.txt", "test_data/test1.txt"]
  796. uploading_res = ragflow.upload_local_file(created_res_id, file_paths)
  797. # get the doc_id
  798. data = uploading_res["data"]
  799. doc_ids = []
  800. for d in data:
  801. doc_ids.append(d["id"])
  802. res = ragflow.start_parsing_documents(created_res_id, doc_ids)
  803. assert res["code"] == RetCode.SUCCESS and res["message"] == ""
  804. # general -> laws
  805. params = {
  806. "template_type": "laws"
  807. }
  808. ragflow.update_file(created_res_id, doc_ids[0], **params)
  809. # re-parse
  810. res = ragflow.start_parsing_documents(created_res_id, doc_ids)
  811. assert res["code"] == RetCode.SUCCESS and res["message"] == ""
  812. def test_start_re_parsing_multiple_specific_documents_with_changing_illegal_parser_id(self):
  813. """
  814. Test the re-parsing documents after changing an illegal parser id.
  815. """
  816. # create a dataset
  817. ragflow = RAGFlow(API_KEY, HOST_ADDRESS)
  818. created_res = ragflow.create_dataset(" test_start_parsing_multiple_documents")
  819. created_res_id = created_res["data"]["dataset_id"]
  820. # upload files
  821. file_paths = ["test_data/test.txt", "test_data/test1.txt"]
  822. uploading_res = ragflow.upload_local_file(created_res_id, file_paths)
  823. # get the doc_id
  824. data = uploading_res["data"]
  825. doc_ids = []
  826. for d in data:
  827. doc_ids.append(d["id"])
  828. res = ragflow.start_parsing_documents(created_res_id, doc_ids)
  829. assert res["code"] == RetCode.SUCCESS and res["message"] == ""
  830. # general -> illegal
  831. params = {
  832. "template_type": "illegal"
  833. }
  834. res = ragflow.update_file(created_res_id, doc_ids[0], **params)
  835. assert res["code"] == RetCode.DATA_ERROR and res["message"] == "Illegal value illegal for 'template_type' field."
  836. # re-parse
  837. res = ragflow.start_parsing_documents(created_res_id, doc_ids)
  838. assert res["code"] == RetCode.SUCCESS and res["message"] == ""
  839. def test_start_parsing_multiple_specific_documents_with_changing_illegal_parser_id(self):
  840. """
  841. Test the parsing documents after changing an illegal parser id.
  842. """
  843. # create a dataset
  844. ragflow = RAGFlow(API_KEY, HOST_ADDRESS)
  845. created_res = ragflow.create_dataset(" test_start_parsing_multiple_documents")
  846. created_res_id = created_res["data"]["dataset_id"]
  847. # upload files
  848. file_paths = ["test_data/test.txt", "test_data/test1.txt"]
  849. uploading_res = ragflow.upload_local_file(created_res_id, file_paths)
  850. # get the doc_id
  851. data = uploading_res["data"]
  852. doc_ids = []
  853. for d in data:
  854. doc_ids.append(d["id"])
  855. # general -> illegal
  856. params = {
  857. "template_type": "illegal"
  858. }
  859. res = ragflow.update_file(created_res_id, doc_ids[0], **params)
  860. assert res["code"] == RetCode.DATA_ERROR and res["message"] == "Illegal value illegal for 'template_type' field."
  861. # re-parse
  862. res = ragflow.start_parsing_documents(created_res_id, doc_ids)
  863. assert res["code"] == RetCode.SUCCESS and res["message"] == ""
  864. def test_start_parsing_multiple_documents_in_the_dataset_whose_parser_id_is_illegal(self):
  865. """
  866. Test the parsing documents whose dataset's parser id is illegal.
  867. """
  868. # create a dataset
  869. ragflow = RAGFlow(API_KEY, HOST_ADDRESS)
  870. created_res = ragflow.create_dataset("test_start_parsing_multiple_documents_in_the_dataset_whose_parser_id_is_illegal")
  871. created_res_id = created_res["data"]["dataset_id"]
  872. # update the parser id
  873. params = {
  874. "chunk_method": "illegal"
  875. }
  876. res = ragflow.update_dataset("test_start_parsing_multiple_documents_in_the_dataset_whose_parser_id_is_illegal", **params)
  877. assert res["code"] == RetCode.DATA_ERROR and res["message"] == "Illegal value illegal for 'chunk_method' field."
  878. # upload files
  879. file_paths = ["test_data/test.txt", "test_data/test1.txt"]
  880. uploading_res = ragflow.upload_local_file(created_res_id, file_paths)
  881. # get the doc_id
  882. data = uploading_res["data"]
  883. doc_ids = []
  884. for d in data:
  885. doc_ids.append(d["id"])
  886. # parse
  887. res = ragflow.start_parsing_documents(created_res_id, doc_ids)
  888. assert res["code"] == RetCode.SUCCESS and res["message"] == ""
  889. # ----------------------------stop parsing-----------------------------------------------------
  890. def test_stop_parsing_document_with_success(self):
  891. """
  892. Test the stopping parsing of a document with success.
  893. """
  894. # create a dataset
  895. ragflow = RAGFlow(API_KEY, HOST_ADDRESS)
  896. created_res = ragflow.create_dataset("test_start_parsing_document_with_success")
  897. created_res_id = created_res["data"]["dataset_id"]
  898. # upload files
  899. file_paths = ["test_data/lol.txt"]
  900. uploading_res = ragflow.upload_local_file(created_res_id, file_paths)
  901. # get the doc_id
  902. data = uploading_res["data"][0]
  903. doc_id = data["id"]
  904. # parse file
  905. res = ragflow.start_parsing_document(created_res_id, doc_id)
  906. assert res["code"] == RetCode.SUCCESS and res["message"] == ""
  907. res = ragflow.stop_parsing_document(created_res_id, doc_id)
  908. assert res["code"] == RetCode.SUCCESS and res["message"] == ""
  909. def test_stop_parsing_nonexistent_document(self):
  910. """
  911. Test the stopping parsing a document which does not exist.
  912. """
  913. # create a dataset
  914. ragflow = RAGFlow(API_KEY, HOST_ADDRESS)
  915. created_res = ragflow.create_dataset("test_start_parsing_nonexistent_document")
  916. created_res_id = created_res["data"]["dataset_id"]
  917. res = ragflow.stop_parsing_document(created_res_id, "imagination.txt")
  918. assert res["code"] == RetCode.ARGUMENT_ERROR and res["message"] == "This document 'imagination.txt' cannot be found!"
  919. def test_stop_parsing_document_in_nonexistent_dataset(self):
  920. """
  921. Test the stopping parsing a document whose dataset is nonexistent.
  922. """
  923. # create a dataset
  924. ragflow = RAGFlow(API_KEY, HOST_ADDRESS)
  925. created_res = ragflow.create_dataset("test_download_nonexistent_document")
  926. created_res_id = created_res["data"]["dataset_id"]
  927. # upload files
  928. file_paths = ["test_data/test.txt"]
  929. uploading_res = ragflow.upload_local_file(created_res_id, file_paths)
  930. # get the doc_id
  931. data = uploading_res["data"][0]
  932. doc_id = data["id"]
  933. # parse
  934. res = ragflow.stop_parsing_document("imagination", doc_id)
  935. assert res["code"] == RetCode.DATA_ERROR and res["message"] == "This dataset 'imagination' cannot be found!"
  936. # ------------------------stop parsing multiple documents----------------------------
  937. def test_stop_parsing_documents_in_nonexistent_dataset(self):
  938. """
  939. Test the stopping parsing documents whose dataset is nonexistent.
  940. """
  941. # create a dataset
  942. ragflow = RAGFlow(API_KEY, HOST_ADDRESS)
  943. created_res = ragflow.create_dataset("test_download_nonexistent_document")
  944. created_res_id = created_res["data"]["dataset_id"]
  945. # upload files
  946. file_paths = ["test_data/test.txt"]
  947. uploading_res = ragflow.upload_local_file(created_res_id, file_paths)
  948. # parse
  949. res = ragflow.stop_parsing_documents("imagination")
  950. assert res["code"] == RetCode.DATA_ERROR and res["message"] == "This dataset 'imagination' cannot be found!"
  951. def test_stop_parsing_multiple_documents(self):
  952. """
  953. Test the stopping parsing documents with a success.
  954. """
  955. # create a dataset
  956. ragflow = RAGFlow(API_KEY, HOST_ADDRESS)
  957. created_res = ragflow.create_dataset("test_start_parsing_multiple_documents")
  958. created_res_id = created_res["data"]["dataset_id"]
  959. # upload files
  960. file_paths = ["test_data/test.txt", "test_data/test1.txt"]
  961. ragflow.upload_local_file(created_res_id, file_paths)
  962. res = ragflow.start_parsing_documents(created_res_id)
  963. assert res["code"] == RetCode.SUCCESS and res["data"] is True and res["message"] == ""
  964. res = ragflow.stop_parsing_documents(created_res_id)
  965. assert res["code"] == RetCode.SUCCESS and res["data"] is True and res["message"] == ""
  966. def test_stop_parsing_multiple_documents_with_one_empty_file(self):
  967. """
  968. Test the stopping parsing documents, one of which is empty.
  969. """
  970. # create a dataset
  971. ragflow = RAGFlow(API_KEY, HOST_ADDRESS)
  972. created_res = ragflow.create_dataset(" test_start_parsing_multiple_documents")
  973. created_res_id = created_res["data"]["dataset_id"]
  974. # upload files
  975. file_paths = ["test_data/test.txt", "test_data/test1.txt", "test_data/empty.txt"]
  976. ragflow.upload_local_file(created_res_id, file_paths)
  977. res = ragflow.start_parsing_documents(created_res_id)
  978. assert res["code"] == RetCode.SUCCESS and res["message"] == "Empty data in the document: empty.txt; "
  979. res = ragflow.stop_parsing_documents(created_res_id)
  980. assert res["code"] == RetCode.SUCCESS and res["data"] is True and res["message"] == ""
  981. def test_stop_parsing_multiple_specific_documents(self):
  982. """
  983. Test the stopping parsing documents whose document ids are specified.
  984. """
  985. # create a dataset
  986. ragflow = RAGFlow(API_KEY, HOST_ADDRESS)
  987. created_res = ragflow.create_dataset(" test_start_parsing_multiple_documents")
  988. created_res_id = created_res["data"]["dataset_id"]
  989. # upload files
  990. file_paths = ["test_data/test.txt", "test_data/test1.txt"]
  991. uploading_res = ragflow.upload_local_file(created_res_id, file_paths)
  992. # get the doc_id
  993. data = uploading_res["data"]
  994. doc_ids = []
  995. for d in data:
  996. doc_ids.append(d["id"])
  997. res = ragflow.start_parsing_documents(created_res_id, doc_ids)
  998. assert res["code"] == RetCode.SUCCESS and res["message"] == ""
  999. res = ragflow.stop_parsing_documents(created_res_id, doc_ids)
  1000. assert res["code"] == RetCode.SUCCESS and res["data"] is True and res["message"] == ""
  1001. # ----------------------------show the status of the file-----------------------------------------------------
  1002. def test_show_status_with_success(self):
  1003. # create a dataset
  1004. ragflow = RAGFlow(API_KEY, HOST_ADDRESS)
  1005. created_res = ragflow.create_dataset("test_show_status_with_success")
  1006. created_res_id = created_res["data"]["dataset_id"]
  1007. # upload files
  1008. file_paths = ["test_data/lol.txt"]
  1009. uploading_res = ragflow.upload_local_file(created_res_id, file_paths)
  1010. # get the doc_id
  1011. data = uploading_res["data"][0]
  1012. doc_id = data["id"]
  1013. # parse file
  1014. res = ragflow.start_parsing_document(created_res_id, doc_id)
  1015. assert res["code"] == RetCode.SUCCESS and res["message"] == ""
  1016. # show status
  1017. status_res = ragflow.show_parsing_status(created_res_id, doc_id)
  1018. assert status_res["code"] == RetCode.SUCCESS and status_res["data"]["status"] == "RUNNING"
  1019. def test_show_status_nonexistent_document(self):
  1020. """
  1021. Test showing the status of a document which does not exist.
  1022. """
  1023. # create a dataset
  1024. ragflow = RAGFlow(API_KEY, HOST_ADDRESS)
  1025. created_res = ragflow.create_dataset("test_show_status_nonexistent_document")
  1026. created_res_id = created_res["data"]["dataset_id"]
  1027. res = ragflow.show_parsing_status(created_res_id, "imagination")
  1028. assert res["code"] == RetCode.DATA_ERROR and res["message"] == "This document: 'imagination' is not a valid document."
  1029. def test_show_status_document_in_nonexistent_dataset(self):
  1030. """
  1031. Test showing the status of a document whose dataset is nonexistent.
  1032. """
  1033. # create a dataset
  1034. ragflow = RAGFlow(API_KEY, HOST_ADDRESS)
  1035. created_res = ragflow.create_dataset("test_show_status_document_in_nonexistent_dataset")
  1036. created_res_id = created_res["data"]["dataset_id"]
  1037. # upload files
  1038. file_paths = ["test_data/test.txt"]
  1039. uploading_res = ragflow.upload_local_file(created_res_id, file_paths)
  1040. # get the doc_id
  1041. data = uploading_res["data"][0]
  1042. doc_id = data["id"]
  1043. # parse
  1044. res = ragflow.show_parsing_status("imagination", doc_id)
  1045. assert res["code"] == RetCode.DATA_ERROR and res["message"] == "This dataset: 'imagination' cannot be found!"
  1046. # ----------------------------list the chunks of the file-----------------------------------------------------
  1047. # ----------------------------delete the chunk-----------------------------------------------------
  1048. # ----------------------------edit the status of the chunk-----------------------------------------------------
  1049. # ----------------------------insert a new chunk-----------------------------------------------------
  1050. # ----------------------------get a specific chunk-----------------------------------------------------
  1051. # ----------------------------retrieval test-----------------------------------------------------