diff --git a/swh/search/elasticsearch.py b/swh/search/elasticsearch.py --- a/swh/search/elasticsearch.py +++ b/swh/search/elasticsearch.py @@ -69,7 +69,7 @@ class ElasticSearch: - def __init__(self, hosts: List[str], indexes: Dict[str, Dict[str, str]]): + def __init__(self, hosts: List[str], indexes: Dict[str, Dict[str, str]] = {}): self._backend = Elasticsearch(hosts=hosts) # Merge current configuration with default values diff --git a/swh/search/tests/test_server.py b/swh/search/tests/test_server.py --- a/swh/search/tests/test_server.py +++ b/swh/search/tests/test_server.py @@ -18,6 +18,7 @@ def teardown_function(): # Ensure there is no configuration loaded from a previous test server.api_cfg = None + server.search = None def _write_config_file(tmp_path, monkeypatch, content): @@ -138,11 +139,33 @@ assert app is app2 -def test_server_first_call_initialize_elasticsearch( +def test_server_first_call_initialize_elasticsearch_with_indexes( swh_search_config_with_indexes, mocker ): """Test the initialize method is called during the first and first only - request to the server""" + request to the server + The ElasticSearch object is instantiated only when a request is done + """ + mock = mocker.patch("swh.search.elasticsearch.ElasticSearch.initialize") + + app = make_app_from_configfile() + app.config["TESTING"] = True + tc = app.test_client() + + tc.get("/") + assert mock.call_count == 1 + + tc.get("/") + assert mock.call_count == 1 + + +def test_server_first_call_initialize_elasticsearch_without_indexes( + swh_search_config_without_indexes, mocker +): + """Test the initialize method is called during the first and first only + request to the server + The ElasticSearch object is instantiated only when a request is done + """ mock = mocker.patch("swh.search.elasticsearch.ElasticSearch.initialize") app = make_app_from_configfile()