test_indices.py 3.2 KB

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