conftest.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. # tests/conftest.py
  2. import asyncio
  3. import uuid
  4. from typing import AsyncGenerator, Generator
  5. import pytest
  6. from r2r import 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
  25. async def superuser_client(
  26. client: R2RClient, config: TestConfig
  27. ) -> AsyncGenerator[R2RClient, None]:
  28. """Creates a superuser client for tests requiring elevated privileges."""
  29. await client.users.login(config.superuser_email, config.superuser_password)
  30. yield client
  31. await client.users.logout()
  32. import uuid
  33. import pytest
  34. from r2r import Message, R2RClient, R2RException, SearchMode
  35. @pytest.fixture(scope="session")
  36. def config():
  37. class TestConfig:
  38. base_url = "http://localhost:7272"
  39. superuser_email = "admin@example.com"
  40. superuser_password = "change_me_immediately"
  41. return TestConfig()
  42. @pytest.fixture(scope="session")
  43. def client(config):
  44. """Create a client instance and log in as a superuser."""
  45. client = R2RClient(config.base_url)
  46. client.users.login(config.superuser_email, config.superuser_password)
  47. return client
  48. @pytest.fixture(scope="session")
  49. def test_collection(client):
  50. """Create a test collection with sample documents."""
  51. collection_name = f"Test Collection {uuid.uuid4()}"
  52. collection_id = client.collections.create(name=collection_name)["results"][
  53. "id"
  54. ]
  55. docs = [
  56. {
  57. "text": f"Aristotle was a Greek philosopher who studied under Plato {str(uuid.uuid4())}.",
  58. "metadata": {
  59. "rating": 5,
  60. "tags": ["philosophy", "greek"],
  61. "category": "ancient",
  62. },
  63. },
  64. {
  65. "text": f"Socrates is considered a founder of Western philosophy {str(uuid.uuid4())}.",
  66. "metadata": {
  67. "rating": 3,
  68. "tags": ["philosophy", "classical"],
  69. "category": "ancient",
  70. },
  71. },
  72. {
  73. "text": f"Rene Descartes was a French philosopher. unique_philosopher {str(uuid.uuid4())}",
  74. "metadata": {
  75. "rating": 8,
  76. "tags": ["rationalism", "french"],
  77. "category": "modern",
  78. },
  79. },
  80. {
  81. "text": f"Immanuel Kant, a German philosopher, influenced Enlightenment thought {str(uuid.uuid4())}.",
  82. "metadata": {
  83. "rating": 7,
  84. "tags": ["enlightenment", "german"],
  85. "category": "modern",
  86. },
  87. },
  88. ]
  89. doc_ids = []
  90. for doc in docs:
  91. result = client.documents.create(
  92. raw_text=doc["text"], metadata=doc["metadata"]
  93. )["results"]
  94. doc_id = result["document_id"]
  95. doc_ids.append(doc_id)
  96. client.collections.add_document(collection_id, doc_id)
  97. return {"collection_id": collection_id, "document_ids": doc_ids}