From 55cbcf4ef277d4c27266c05f6ca789a2d15377e6 Mon Sep 17 00:00:00 2001 From: Amna Mubashar Date: Fri, 13 Dec 2024 15:07:52 +0100 Subject: [PATCH] Add a new fixture --- .../azure_ai_search/tests/conftest.py | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/integrations/azure_ai_search/tests/conftest.py b/integrations/azure_ai_search/tests/conftest.py index e741e3066..471eccbd4 100644 --- a/integrations/azure_ai_search/tests/conftest.py +++ b/integrations/azure_ai_search/tests/conftest.py @@ -83,3 +83,28 @@ def wait_for_index_deletion(client, index_name): logger.error(f"Index {index_name} was already deleted or not found.") except Exception as e: logger.error(f"Unexpected error when deleting index {index_name}: {e}") + raise + + +@pytest.fixture(scope="session", autouse=True) +def cleanup_indexes(): + """ + Fixture to clean up all remaining indexes at the end of the test session. + Automatically runs after all tests. + """ + azure_endpoint = os.environ["AZURE_SEARCH_SERVICE_ENDPOINT"] + api_key = os.environ["AZURE_SEARCH_API_KEY"] + + client = SearchIndexClient(azure_endpoint, AzureKeyCredential(api_key)) + + yield # Allow tests to run before performing cleanup + + # Cleanup: Delete all remaining indexes + logger.info("Starting session-level cleanup of all Azure Search indexes.") + existing_indexes = client.list_index_names() + for index in existing_indexes: + try: + logger.info(f"Deleting leftover index: {index}") + client.delete_index(index) + except Exception as e: + logger.error(f"Failed to delete index {index}: {e}")