test_collections.py 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. import uuid
  2. import pytest
  3. from r2r import R2RClient, R2RException
  4. @pytest.fixture(scope="session")
  5. def test_document_2(client: R2RClient):
  6. """Create and yield a test document, then clean up."""
  7. doc_resp = client.documents.create(
  8. raw_text="Another test doc for collections",
  9. run_with_orchestration=False,
  10. )
  11. doc_id = doc_resp.results.document_id
  12. yield doc_id
  13. # Cleanup: Try deleting the document if it still exists
  14. try:
  15. client.documents.delete(id=doc_id)
  16. except R2RException:
  17. pass
  18. def test_create_collection(client: R2RClient):
  19. collection_id = client.collections.create(name="Test Collection Creation",
  20. description="Desc").results.id
  21. assert collection_id is not None, "No collection_id returned"
  22. # Cleanup
  23. client.collections.delete(collection_id)
  24. def test_list_collections(client: R2RClient, test_collection):
  25. results = client.collections.list(limit=10, offset=0).results
  26. assert len(results) >= 1, "Expected at least one collection, none found"
  27. def test_retrieve_collection(client: R2RClient, test_collection):
  28. # Retrieve the collection just created
  29. retrieved = client.collections.retrieve(
  30. test_collection["collection_id"]).results
  31. assert retrieved.id == test_collection["collection_id"], (
  32. "Retrieved wrong collection ID")
  33. def test_update_collection(client: R2RClient, test_collection):
  34. updated_name = "Updated Test Collection"
  35. updated_desc = "Updated description"
  36. updated = client.collections.update(
  37. test_collection["collection_id"],
  38. name=updated_name,
  39. description=updated_desc,
  40. ).results
  41. assert updated.name == updated_name, "Collection name not updated"
  42. assert updated.description == updated_desc, (
  43. "Collection description not updated")
  44. def test_add_document_to_collection(client: R2RClient, test_collection,
  45. test_document_2):
  46. client.collections.add_document(test_collection["collection_id"],
  47. str(test_document_2))
  48. docs_in_collection = client.collections.list_documents(
  49. test_collection["collection_id"]).results
  50. found = any(
  51. str(doc.id) == str(test_document_2) for doc in docs_in_collection)
  52. assert found, "Added document not found in collection"
  53. def test_list_documents_in_collection(client: R2RClient, test_collection,
  54. test_document):
  55. # Document should be in the collection already from previous test
  56. docs_in_collection = client.collections.list_documents(
  57. test_collection["collection_id"]).results
  58. found = any(
  59. str(doc.id) == str(test_document) for doc in docs_in_collection)
  60. assert found, "Expected document not found in collection"
  61. def test_remove_document_from_collection(client: R2RClient, test_collection,
  62. test_document):
  63. # Remove the document from the collection
  64. client.collections.remove_document(test_collection["collection_id"],
  65. test_document)
  66. docs_in_collection = client.collections.list_documents(
  67. test_collection["collection_id"]).results
  68. found = any(str(doc.id) == test_document for doc in docs_in_collection)
  69. assert not found, "Document still present in collection after removal"
  70. def test_remove_non_member_user_from_collection(mutable_client: R2RClient):
  71. # Create a user and a collection
  72. user_email = f"user_{uuid.uuid4()}@test.com"
  73. password = "pwd123"
  74. mutable_client.users.create(user_email, password)
  75. mutable_client.users.login(user_email, password)
  76. # Create a collection by the same user
  77. collection_id = mutable_client.collections.create(
  78. name="User Owned Collection").results.id
  79. mutable_client.users.logout()
  80. # Create another user who will not be added to the collection
  81. another_user_email = f"user2_{uuid.uuid4()}@test.com"
  82. mutable_client.users.create(another_user_email, password)
  83. mutable_client.users.login(another_user_email, password)
  84. another_user_id = mutable_client.users.me().results.id
  85. mutable_client.users.logout()
  86. # Re-login as collection owner
  87. mutable_client.users.login(user_email, password)
  88. # Attempt to remove the other user (who was never added)
  89. with pytest.raises(R2RException) as exc_info:
  90. mutable_client.collections.remove_user(collection_id, another_user_id)
  91. assert exc_info.value.status_code in [
  92. 400,
  93. 404,
  94. ], "Wrong error code for removing non-member user"
  95. # Cleanup
  96. mutable_client.collections.delete(collection_id)
  97. def test_delete_collection(client: R2RClient):
  98. # Create a collection and delete it
  99. coll_id = client.collections.create(name="Delete Me").results.id
  100. client.collections.delete(coll_id)
  101. # Verify retrieval fails
  102. with pytest.raises(R2RException) as exc_info:
  103. client.collections.retrieve(coll_id)
  104. assert exc_info.value.status_code == 404, (
  105. "Wrong error code retrieving deleted collection")
  106. def test_add_user_to_non_existent_collection(mutable_client: R2RClient):
  107. # Create a regular user
  108. user_email = f"test_user_{uuid.uuid4()}@test.com"
  109. user_password = "test_password"
  110. mutable_client.users.create(user_email, user_password)
  111. mutable_client.users.login(user_email, user_password)
  112. user_id = mutable_client.users.me().results.id
  113. mutable_client.users.logout()
  114. # Re-login as superuser to try adding user to a non-existent collection
  115. # (Assumes superuser credentials are already in the client fixture)
  116. fake_collection_id = str(uuid.uuid4()) # Non-existent collection ID
  117. with pytest.raises(R2RException) as exc_info:
  118. result = mutable_client.collections.add_user(fake_collection_id,
  119. user_id)
  120. assert exc_info.value.status_code == 404, (
  121. "Wrong error code for non-existent collection")
  122. def test_create_collection_without_name(client: R2RClient):
  123. # Attempt to create a collection without a name
  124. with pytest.raises(R2RException) as exc_info:
  125. client.collections.create(name="", description="No name")
  126. # TODO - Error should be a 400 or 422, not 409
  127. assert exc_info.value.status_code in [
  128. 400,
  129. 422,
  130. 409,
  131. ], "Expected validation error for empty name"
  132. def test_filter_collections_by_non_existent_id(client: R2RClient):
  133. # Filter collections by an ID that does not exist
  134. random_id = str(uuid.uuid4())
  135. resp = client.collections.list(ids=[random_id])
  136. assert len(
  137. resp.results) == 0, ("Expected no collections for a non-existent ID")
  138. def test_list_documents_in_empty_collection(client: R2RClient):
  139. # Create a new collection with no documents
  140. empty_coll_id = client.collections.create(
  141. name="Empty Collection").results.id
  142. docs = client.collections.list_documents(empty_coll_id).results
  143. assert len(docs) == 0, "Expected no documents in a new empty collection"
  144. client.collections.delete(empty_coll_id)
  145. def test_remove_document_not_in_collection(client: R2RClient, test_document):
  146. # Create collection without adding the test_document
  147. coll_id = client.collections.create(name="NoDocCollection").results.id
  148. # Try removing the test_document that was never added
  149. with pytest.raises(R2RException) as exc_info:
  150. client.collections.remove_document(coll_id, test_document)
  151. # Expect 404 or 400 since doc not in collection
  152. assert exc_info.value.status_code in [
  153. 400,
  154. 404,
  155. ], "Expected error removing doc not in collection"
  156. client.collections.delete(coll_id)
  157. def test_add_non_existent_document_to_collection(client: R2RClient):
  158. # Create a collection
  159. coll_id = client.collections.create(name="AddNonExistentDoc").results.id
  160. # Try adding a non-existent document
  161. fake_doc_id = str(uuid.uuid4())
  162. with pytest.raises(R2RException) as exc_info:
  163. client.collections.add_document(coll_id, fake_doc_id)
  164. assert exc_info.value.status_code in [
  165. 400,
  166. 404,
  167. ], "Expected error adding non-existent document"
  168. client.collections.delete(coll_id)
  169. def test_delete_non_existent_collection(client: R2RClient):
  170. # Try deleting a collection that doesn't exist
  171. fake_collection_id = str(uuid.uuid4())
  172. with pytest.raises(R2RException) as exc_info:
  173. client.collections.delete(fake_collection_id)
  174. assert exc_info.value.status_code == 404, (
  175. "Expected 404 when deleting non-existent collection")
  176. def test_retrieve_collection_by_name(client: R2RClient):
  177. # Generate a unique collection name
  178. unique_name = f"TestRetrieveByName-{uuid.uuid4()}"
  179. # Create a collection with the unique name
  180. created_resp = client.collections.create(
  181. name=unique_name, description="Collection for retrieval by name test")
  182. created = created_resp.results
  183. assert created.id is not None, (
  184. "Creation did not return a valid collection ID")
  185. # Retrieve the collection by its name
  186. retrieved_resp = client.collections.retrieve_by_name(unique_name)
  187. retrieved = retrieved_resp.results
  188. assert retrieved.id == created.id, (
  189. "Retrieved collection does not match the created collection")
  190. # Cleanup: Delete the created collection
  191. client.collections.delete(created.id)