Changeset View
Changeset View
Standalone View
Standalone View
swh/search/tests/test_init.py
- This file was added.
# Copyright (C) 2020 The Software Heritage developers | |||||
# See the AUTHORS file at the top-level directory of this distribution | |||||
# License: GNU General Public License version 3, or any later version | |||||
# See top-level LICENSE file for more information | |||||
import pytest | |||||
from swh.search import get_search | |||||
from swh.search.elasticsearch import ElasticSearch | |||||
from swh.search.api.client import RemoteSearch | |||||
from swh.search.in_memory import InMemorySearch | |||||
SEARCH_IMPLEMENTATIONS_KWARGS = [ | |||||
("remote", RemoteSearch, {"url": "localhost"}), | |||||
("elasticsearch", ElasticSearch, {"hosts": ["localhost"]}), | |||||
] | |||||
SEARCH_IMPLEMENTATIONS = SEARCH_IMPLEMENTATIONS_KWARGS + [ | |||||
("memory", InMemorySearch, None), | |||||
] | |||||
def test_get_search_failure(): | |||||
with pytest.raises(ValueError, match="Unknown search class"): | |||||
vlorentz: you can use `pytest.parametrize` instead, but that's ok too | |||||
Done Inline Actionsthanks! ardumont: thanks! | |||||
get_search("unknown-search") | |||||
Not Done Inline Actions"If a function argument's name clashes with a reserved keyword, it is generally better to append a single trailing underscore rather than use an abbreviation or spelling corruption. Thus class_ is better than clss. (Perhaps better is to avoid such clashes by using a synonym.)" vlorentz: "If a function argument's name clashes with a reserved keyword, it is generally better to… | |||||
@pytest.mark.parametrize("class_,expected_class,kwargs", SEARCH_IMPLEMENTATIONS) | |||||
def test_get_search(mocker, class_, expected_class, kwargs): | |||||
mocker.patch("swh.search.elasticsearch.Elasticsearch") | |||||
if kwargs: | |||||
concrete_search = get_search(class_, **kwargs) | |||||
else: | |||||
concrete_search = get_search(class_) | |||||
assert isinstance(concrete_search, expected_class) | |||||
@pytest.mark.parametrize("class_,expected_class,kwargs", SEARCH_IMPLEMENTATIONS_KWARGS) | |||||
def test_get_search_deprecation_warning(mocker, class_, expected_class, kwargs): | |||||
with pytest.warns(DeprecationWarning): | |||||
concrete_search = get_search(class_, args=kwargs) | |||||
assert isinstance(concrete_search, expected_class) |
you can use pytest.parametrize instead, but that's ok too