Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions pytest_invenio/fixtures.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# SPDX-FileCopyrightText: 2017-2025 CERN.
# SPDX-FileCopyrightText: 2018 Esteban J. G. Garbancho.
# SPDX-FileCopyrightText: 2024-2026 Graz University of Technology.
# SPDX-FileCopyrightText: 2026 CESNET z.s.p.o.
# SPDX-License-Identifier: MIT

"""Pytest fixtures for Invenio."""
Expand Down Expand Up @@ -509,6 +510,37 @@ def search_clear(search):
_search_create_indexes(current_search, current_search_client)


@pytest.fixture(scope="function")
def search_index_names(search):
"""Get the set of names of the indices in the search cluster.

Scope: function

Returns a function that returns the set of index names currently present
in OpenSearch. System indices (names starting with a dot, e.g. those
created by the OpenSearch ML Commons plugin) are left out.

The returned names are concrete index names (including the
``SEARCH_INDEX_PREFIX`` and version suffix), not alias names.

Usage:

.. code-block:: python

def test_something(search_index_names):
assert search_index_names() == {"records-record-v1.0.0"}

Note: the ML Commons plugin cannot easily be disabled (the only option
today is a custom Docker image), which is why its indices are removed
here.
"""

def index_names():
return {name for name in search.indices.get_alias() if not name.startswith(".")}

return index_names


@pytest.fixture(scope="function")
def es_clear(search_clear):
"""Alias for search_clear fixture."""
Expand Down
1 change: 1 addition & 0 deletions pytest_invenio/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
search,
search_clear,
search_hosts,
search_index_names,
set_app_config_fn_scoped,
uow_class,
)
Expand Down
17 changes: 17 additions & 0 deletions tests/test_fixtures.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# SPDX-FileCopyrightText: 2017-2018 CERN.
# SPDX-FileCopyrightText: 2024-2025 Graz University of Technology.
# SPDX-FileCopyrightText: 2025 Northwestern University.
# SPDX-FileCopyrightText: 2026 CESNET z.s.p.o.
# SPDX-License-Identifier: MIT

"""Module tests."""
Expand Down Expand Up @@ -329,6 +330,22 @@ def test_search2(search):
conftest_testdir.runpytest().assert_outcomes(passed=2)


def test_search_index_names(conftest_testdir):
"""Test search index names."""
conftest_testdir.makepyfile(
"""
def test_index_names(search, search_index_names):
assert search_index_names() == set()
search.indices.create(index='demo')
try:
assert search_index_names() == {'demo'}
finally:
search.indices.delete(index='demo')
"""
)
conftest_testdir.runpytest().assert_outcomes(passed=1)


def test_database(conftest_testdir):
"""Test database creation and initialization."""
conftest_testdir.makepyfile(
Expand Down
Loading