test_indices.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import uuid
  2. import pytest
  3. from r2r import R2RClient, R2RException
  4. @pytest.fixture(scope="session")
  5. def config():
  6. class TestConfig:
  7. base_url = "http://localhost:7272"
  8. superuser_email = "admin@example.com"
  9. superuser_password = "change_me_immediately"
  10. return TestConfig()
  11. @pytest.fixture(scope="session")
  12. def client(config):
  13. """Create a client instance and log in as superuser."""
  14. client = R2RClient(config.base_url)
  15. client.users.login(config.superuser_email, config.superuser_password)
  16. return client
  17. def test_create_and_get_index(client):
  18. index_name = f"test_index_{uuid.uuid4().hex[:8]}"
  19. config = {
  20. "table_name": "chunks",
  21. "index_method": "hnsw",
  22. "index_measure": "cosine_distance",
  23. "index_arguments": {"m": 16, "ef_construction": 64, "ef": 40},
  24. "index_name": index_name,
  25. "index_column": "vec",
  26. "concurrently": True,
  27. }
  28. # Create the index
  29. create_resp = client.indices.create(
  30. config=config, run_with_orchestration=True
  31. )["results"]
  32. assert "message" in create_resp, "No message in create response"
  33. # Get the index details
  34. get_resp = client.indices.retrieve(
  35. index_name=index_name, table_name="chunks"
  36. )["results"]
  37. assert "index" in get_resp, "No index in get response"
  38. assert get_resp["index"]["name"] == index_name, "Index name mismatch"
  39. def test_list_indices(client):
  40. resp = client.indices.list(limit=5)["results"]
  41. assert "indices" in resp, "No indices field in response"
  42. # Just ensure we get a list without error. Detailed checks depend on data availability.
  43. assert isinstance(resp["indices"], list), "Indices field is not a list"
  44. def test_delete_index(client):
  45. # Create an index to delete
  46. index_name = f"test_delete_index_{uuid.uuid4().hex[:8]}"
  47. config = {
  48. "table_name": "chunks",
  49. "index_method": "hnsw",
  50. "index_measure": "cosine_distance",
  51. "index_arguments": {"m": 16, "ef_construction": 64, "ef": 40},
  52. "index_name": index_name,
  53. "index_column": "vec",
  54. "concurrently": True,
  55. }
  56. client.indices.create(config=config, run_with_orchestration=True)[
  57. "results"
  58. ]
  59. # Delete the index
  60. delete_resp = client.indices.delete(
  61. index_name=index_name, table_name="chunks"
  62. )["results"]
  63. assert "message" in delete_resp, "No message in delete response"
  64. # Verify deletion by attempting to retrieve the index
  65. with pytest.raises(R2RException) as exc_info:
  66. client.indices.retrieve(index_name=index_name, table_name="chunks")
  67. assert (
  68. "not found" in str(exc_info.value).lower()
  69. ), "Unexpected error message for deleted index"
  70. def test_error_handling(client):
  71. # Try to get a non-existent index
  72. with pytest.raises(R2RException) as exc_info:
  73. client.indices.retrieve(
  74. index_name="nonexistent_index", table_name="chunks"
  75. )
  76. assert (
  77. "not found" in str(exc_info.value).lower()
  78. ), "Unexpected error message for non-existent index"