conftest.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. # tests/conftest.py
  2. import asyncio
  3. import uuid
  4. from typing import AsyncGenerator, Generator
  5. import pytest
  6. from r2r import R2RAsyncClient, R2RClient
  7. class TestConfig:
  8. def __init__(self):
  9. self.base_url = "http://localhost:7272"
  10. self.index_wait_time = 1.0
  11. self.chunk_creation_wait_time = 1.0
  12. self.superuser_email = "admin@example.com"
  13. self.superuser_password = "change_me_immediately"
  14. self.test_timeout = 30 # seconds
  15. @pytest.fixture # (scope="session")
  16. def config() -> TestConfig:
  17. return TestConfig()
  18. @pytest.fixture(scope="session")
  19. async def client(config) -> AsyncGenerator[R2RClient, None]:
  20. """Create a shared client instance for the test session."""
  21. client = R2RClient(config.base_url)
  22. yield client
  23. # Session cleanup if needed
  24. @pytest.fixture # scope="session")
  25. def mutable_client(config) -> R2RClient:
  26. """Create a shared client instance for the test session."""
  27. client = R2RClient(config.base_url)
  28. return client # a client for logging in and what-not
  29. # Session cleanup if needed
  30. @pytest.fixture # (scope="session")
  31. async def aclient(config) -> AsyncGenerator[R2RClient, None]:
  32. """Create a shared client instance for the test session."""
  33. client = R2RAsyncClient(config.base_url)
  34. yield client
  35. # Session cleanup if needed
  36. @pytest.fixture
  37. async def superuser_client(
  38. client: R2RClient, config: TestConfig
  39. ) -> AsyncGenerator[R2RClient, None]:
  40. """Creates a superuser client for tests requiring elevated privileges."""
  41. await client.users.login(config.superuser_email, config.superuser_password)
  42. yield client
  43. await client.users.logout()
  44. import uuid
  45. import pytest
  46. from r2r import Message, R2RClient, R2RException, SearchMode
  47. @pytest.fixture(scope="session")
  48. def config():
  49. class TestConfig:
  50. base_url = "http://localhost:7272"
  51. superuser_email = "admin@example.com"
  52. superuser_password = "change_me_immediately"
  53. return TestConfig()
  54. @pytest.fixture(scope="session")
  55. def client(config):
  56. """Create a client instance and log in as a superuser."""
  57. client = R2RClient(config.base_url)
  58. client.users.login(config.superuser_email, config.superuser_password)
  59. return client
  60. @pytest.fixture(scope="session")
  61. def test_document(client):
  62. """Create and yield a test document, then clean up."""
  63. random_suffix = str(uuid.uuid4())
  64. doc_resp = client.documents.create(
  65. raw_text=f"{random_suffix} Test doc for collections",
  66. run_with_orchestration=False,
  67. )
  68. doc_id = doc_resp["results"]["document_id"]
  69. yield doc_id
  70. # Cleanup: Try deleting the document if it still exists
  71. try:
  72. client.documents.delete(id=doc_id)
  73. except R2RException:
  74. pass
  75. @pytest.fixture(scope="session")
  76. def test_collection(client, test_document):
  77. """Create a test collection with sample documents."""
  78. collection_name = f"Test Collection {uuid.uuid4()}"
  79. collection_id = client.collections.create(name=collection_name)["results"][
  80. "id"
  81. ]
  82. docs = [
  83. {
  84. "text": f"Aristotle was a Greek philosopher who studied under Plato {str(uuid.uuid4())}.",
  85. "metadata": {
  86. "rating": 5,
  87. "tags": ["philosophy", "greek"],
  88. "category": "ancient",
  89. },
  90. },
  91. {
  92. "text": f"Socrates is considered a founder of Western philosophy {str(uuid.uuid4())}.",
  93. "metadata": {
  94. "rating": 3,
  95. "tags": ["philosophy", "classical"],
  96. "category": "ancient",
  97. },
  98. },
  99. {
  100. "text": f"Rene Descartes was a French philosopher. unique_philosopher {str(uuid.uuid4())}",
  101. "metadata": {
  102. "rating": 8,
  103. "tags": ["rationalism", "french"],
  104. "category": "modern",
  105. },
  106. },
  107. {
  108. "text": f"Immanuel Kant, a German philosopher, influenced Enlightenment thought {str(uuid.uuid4())}.",
  109. "metadata": {
  110. "rating": 7,
  111. "tags": ["enlightenment", "german"],
  112. "category": "modern",
  113. },
  114. },
  115. ]
  116. doc_ids = []
  117. for doc in docs:
  118. result = client.documents.create(
  119. raw_text=doc["text"], metadata=doc["metadata"]
  120. )["results"]
  121. doc_id = result["document_id"]
  122. doc_ids.append(doc_id)
  123. client.collections.add_document(collection_id, doc_id)
  124. client.collections.add_document(collection_id, test_document)
  125. return {"collection_id": collection_id, "document_ids": doc_ids}