""" Tests for user_id database indexes. This test suite verifies that the database indexes for user authentication are created correctly and function as expected. """ import pytest import asyncio from analytics.database import get_database, connect_to_database from analytics.create_indexes import ( create_user_id_indexes, verify_indexes, list_all_indexes, drop_user_id_indexes ) class TestUserIndexes: """Test class for user_id database indexes""" @pytest.fixture(autouse=True) async def setup_database(self): """Setup database connection for tests""" await connect_to_database() self.db = await get_database() if self.db is None: pytest.skip("Database not available for testing") async def test_create_indexes_success(self): """Test that indexes are created successfully""" # Clean up any existing indexes first await drop_user_id_indexes() # Create indexes success = await create_user_id_indexes() assert success, "Index creation should succeed" # Verify they exist verified = await verify_indexes() assert verified, "All indexes should be verified" async def test_create_indexes_idempotent(self): """Test that creating indexes multiple times is safe""" # Create indexes first time success1 = await create_user_id_indexes() assert success1, "First index creation should succeed" # Create indexes second time (should be idempotent) success2 = await create_user_id_indexes() assert success2, "Second index creation should succeed" # Verify they still exist verified = await verify_indexes() assert verified, "All indexes should still be verified" async def test_verify_indexes_missing(self): """Test verification when indexes are missing""" # Drop all indexes first await drop_user_id_indexes() # Verification should fail verified = await verify_indexes() assert not verified, "Verification should fail when indexes are missing" async def test_list_indexes(self): """Test listing all indexes""" # Ensure indexes exist await create_user_id_indexes() # List indexes indexes = await list_all_indexes() # Should have entries for all collections expected_collections = ["sessions", "messages", "search_analytics"] for collection in expected_collections: assert collection in indexes, f"Should have indexes for {collection}" assert len(indexes[collection]) > 0, f"Should have at least one index for {collection}" async def test_rollback_indexes(self): """Test dropping user_id indexes""" # Create indexes first await create_user_id_indexes() # Verify they exist verified_before = await verify_indexes() assert verified_before, "Indexes should exist before rollback" # Drop indexes success = await drop_user_id_indexes() assert success, "Rollback should succeed" # Verify they're gone verified_after = await verify_indexes() assert not verified_after, "Indexes should be gone after rollback" async def test_index_properties(self): """Test that indexes have correct properties""" # Create indexes await create_user_id_indexes() # Get index details indexes = await list_all_indexes() # Check sessions collection indexes sessions_indexes = {idx["name"]: idx for idx in indexes["sessions"]} # Check user_id sparse index if "user_id_sparse" in sessions_indexes: user_id_idx = sessions_indexes["user_id_sparse"] assert user_id_idx.get("sparse") is True, "user_id index should be sparse" # Check compound index if "user_id_start_time_compound" in sessions_indexes: compound_idx = sessions_indexes["user_id_start_time_compound"] assert compound_idx.get("sparse") is True, "compound index should be sparse" # Check key structure key = compound_idx.get("key", {}) assert "user_id" in key, "compound index should include user_id" assert "start_time" in key, "compound index should include start_time" # Async test runner for pytest @pytest.mark.asyncio async def test_create_indexes_integration(): """Integration test for index creation""" test_instance = TestUserIndexes() await test_instance.setup_database() await test_instance.test_create_indexes_success() @pytest.mark.asyncio async def test_idempotent_creation(): """Test idempotent index creation""" test_instance = TestUserIndexes() await test_instance.setup_database() await test_instance.test_create_indexes_idempotent() @pytest.mark.asyncio async def test_rollback_functionality(): """Test rollback functionality""" test_instance = TestUserIndexes() await test_instance.setup_database() await test_instance.test_rollback_indexes() if __name__ == "__main__": # Run tests directly async def run_tests(): test_instance = TestUserIndexes() await test_instance.setup_database() print("Running user index tests...") try: await test_instance.test_create_indexes_success() print("āœ… Index creation test passed") await test_instance.test_create_indexes_idempotent() print("āœ… Idempotent creation test passed") await test_instance.test_rollback_indexes() print("āœ… Rollback test passed") await test_instance.test_list_indexes() print("āœ… List indexes test passed") print("\nšŸŽ‰ All tests passed!") except Exception as e: print(f"āŒ Test failed: {e}") raise asyncio.run(run_tests())