Expanded type coverage to benchmarks, samples and tests. (#566)

* Renamed json samples to fix duplicate module name.

Signed-off-by: dblock <[email protected]>

* Enabled mypy on all source files.

Signed-off-by: dblock <[email protected]>

* Added missing types.

Signed-off-by: dblock <[email protected]>

* Added CHANGELOG.

Signed-off-by: dblock <[email protected]>

* Move type: ignore to fix untyped decorator makes function untyped.

Signed-off-by: dblock <[email protected]>

* Fix nox -rs lint-3.7.

Signed-off-by: dblock <[email protected]>

* Fixed incorrect import.

Signed-off-by: dblock <[email protected]>

* Fix broken test.

Signed-off-by: dblock <[email protected]>

* Fixed TestBulk::test_bulk_works_with_bytestring_body.

Signed-off-by: dblock <[email protected]>

---------

Signed-off-by: dblock <[email protected]>
This commit is contained in:
Daniel (dB.) Doubrovkine
2023-11-09 10:51:20 -05:00
committed by GitHub
parent dcb79cc322
commit 56c96d7c4f
101 changed files with 1234 additions and 1019 deletions
+3 -2
View File
@@ -26,6 +26,7 @@
# under the License.
from typing import Any
from unittest import SkipTest
from opensearchpy.helpers import test
@@ -34,7 +35,7 @@ from opensearchpy.helpers.test import OpenSearchTestCase as BaseTestCase
client = None
def get_client(**kwargs):
def get_client(**kwargs: Any) -> Any:
global client
if client is False:
raise SkipTest("No client is available")
@@ -66,5 +67,5 @@ def setup_module() -> None:
class OpenSearchTestCase(BaseTestCase):
@staticmethod
def _get_client(**kwargs):
def _get_client(**kwargs: Any) -> Any:
return get_client(**kwargs)
+7 -6
View File
@@ -28,6 +28,7 @@
import os
import time
from typing import Any
import pytest
@@ -40,11 +41,11 @@ from ..utils import wipe_cluster
# Used for
OPENSEARCH_VERSION = ""
OPENSEARCH_BUILD_HASH = ""
OPENSEARCH_REST_API_TESTS = []
OPENSEARCH_REST_API_TESTS: Any = []
@pytest.fixture(scope="session")
def sync_client_factory():
@pytest.fixture(scope="session") # type: ignore
def sync_client_factory() -> Any:
client = None
try:
# Configure the client optionally with an HTTP conn class
@@ -63,7 +64,7 @@ def sync_client_factory():
# We do this little dance with the URL to force
# Requests to respect 'headers: None' within rest API spec tests.
client = opensearchpy.OpenSearch(
OPENSEARCH_URL.replace("elastic:changeme@", ""), **kw
OPENSEARCH_URL.replace("elastic:changeme@", ""), **kw # type: ignore
)
# Wait for the cluster to report a status of 'yellow'
@@ -83,8 +84,8 @@ def sync_client_factory():
client.close()
@pytest.fixture(scope="function")
def sync_client(sync_client_factory):
@pytest.fixture(scope="function") # type: ignore
def sync_client(sync_client_factory: Any) -> Any:
try:
yield sync_client_factory
finally:
@@ -27,10 +27,10 @@
import re
from datetime import datetime
from typing import Any
from pytest import fixture
from opensearchpy.client import OpenSearch
from opensearchpy.connection.connections import add_connection
from opensearchpy.helpers import bulk
from opensearchpy.helpers.test import get_test_client
@@ -45,32 +45,32 @@ from .test_data import (
from .test_document import Comment, History, PullRequest, User
@fixture(scope="session")
def client() -> OpenSearch:
@fixture(scope="session") # type: ignore
def client() -> Any:
client = get_test_client(verify_certs=False, http_auth=("admin", "admin"))
add_connection("default", client)
return client
@fixture(scope="session")
def opensearch_version(client):
@fixture(scope="session") # type: ignore
def opensearch_version(client: Any) -> Any:
info = client.info()
print(info)
yield tuple(
int(x)
for x in re.match(r"^([0-9.]+)", info["version"]["number"]).group(1).split(".")
for x in re.match(r"^([0-9.]+)", info["version"]["number"]).group(1).split(".") # type: ignore
)
@fixture
def write_client(client):
@fixture # type: ignore
def write_client(client: Any) -> Any:
yield client
client.indices.delete("test-*", ignore=404)
client.indices.delete_template("test-template", ignore=404)
@fixture(scope="session")
def data_client(client):
@fixture(scope="session") # type: ignore
def data_client(client: Any) -> Any:
# create mappings
create_git_index(client, "git")
create_flat_git_index(client, "flat-git")
@@ -82,8 +82,8 @@ def data_client(client):
client.indices.delete("flat-git", ignore=404)
@fixture
def pull_request(write_client):
@fixture # type: ignore
def pull_request(write_client: Any) -> Any:
PullRequest.init()
pr = PullRequest(
_id=42,
@@ -106,8 +106,8 @@ def pull_request(write_client):
return pr
@fixture
def setup_ubq_tests(client) -> str:
@fixture # type: ignore
def setup_ubq_tests(client: Any) -> str:
index = "test-git"
create_git_index(client, index)
bulk(client, TEST_GIT_DATA, raise_on_error=True, refresh=True)
@@ -26,7 +26,7 @@
# under the License.
from typing import Tuple
from typing import Any
from mock import patch
@@ -40,9 +40,9 @@ from .. import OpenSearchTestCase
class FailingBulkClient(object):
def __init__(
self,
client,
fail_at: Tuple[int] = (2,),
fail_with=TransportError(599, "Error!", {}),
client: Any,
fail_at: Any = (2,),
fail_with: Any = TransportError(599, "Error!", {}),
) -> None:
self.client = client
self._called = 0
@@ -50,7 +50,7 @@ class FailingBulkClient(object):
self.transport = client.transport
self._fail_with = fail_with
def bulk(self, *args, **kwargs):
def bulk(self, *args: Any, **kwargs: Any) -> Any:
self._called += 1
if self._called in self._fail_at:
raise self._fail_with
@@ -98,7 +98,7 @@ class TestStreamingBulk(OpenSearchTestCase):
else:
assert False, "exception should have been raised"
def test_different_op_types(self):
def test_different_op_types(self) -> Any:
if self.opensearch_version() < (0, 90, 1):
raise SkipTest("update supported since 0.90.1")
self.client.index(index="i", id=45, body={})
@@ -218,7 +218,7 @@ class TestStreamingBulk(OpenSearchTestCase):
fail_with=TransportError(429, "Rejected!", {}),
)
def streaming_bulk():
def streaming_bulk() -> Any:
results = list(
helpers.streaming_bulk(
failing_client,
@@ -271,7 +271,7 @@ class TestBulk(OpenSearchTestCase):
self.assertEqual(0, failed)
self.assertEqual(100, self.client.count(index="test-index")["count"])
def test_errors_are_reported_correctly(self):
def test_errors_are_reported_correctly(self) -> None:
self.client.indices.create(
"i",
{
@@ -316,7 +316,7 @@ class TestBulk(OpenSearchTestCase):
index="i",
)
def test_ignore_error_if_raised(self):
def test_ignore_error_if_raised(self) -> None:
# ignore the status code 400 in tuple
helpers.bulk(
self.client, [{"a": 42}, {"a": "c"}], index="i", ignore_status=(400,)
@@ -349,7 +349,7 @@ class TestBulk(OpenSearchTestCase):
failing_client = FailingBulkClient(self.client)
helpers.bulk(failing_client, [{"a": 42}], index="i", ignore_status=(599,))
def test_errors_are_collected_properly(self):
def test_errors_are_collected_properly(self) -> None:
self.client.indices.create(
"i",
{
@@ -384,12 +384,12 @@ class TestScan(OpenSearchTestCase):
},
]
def teardown_method(self, m) -> None:
def teardown_method(self, m: Any) -> None:
self.client.transport.perform_request("DELETE", "/_search/scroll/_all")
super(TestScan, self).teardown_method(m)
def test_order_can_be_preserved(self):
bulk = []
def test_order_can_be_preserved(self) -> None:
bulk: Any = []
for x in range(100):
bulk.append({"index": {"_index": "test_index", "_id": x}})
bulk.append({"answer": x, "correct": x == 42})
@@ -408,8 +408,8 @@ class TestScan(OpenSearchTestCase):
self.assertEqual(list(map(str, range(100))), list(d["_id"] for d in docs))
self.assertEqual(list(range(100)), list(d["_source"]["answer"] for d in docs))
def test_all_documents_are_read(self):
bulk = []
def test_all_documents_are_read(self) -> None:
bulk: Any = []
for x in range(100):
bulk.append({"index": {"_index": "test_index", "_id": x}})
bulk.append({"answer": x, "correct": x == 42})
@@ -421,8 +421,8 @@ class TestScan(OpenSearchTestCase):
self.assertEqual(set(map(str, range(100))), set(d["_id"] for d in docs))
self.assertEqual(set(range(100)), set(d["_source"]["answer"] for d in docs))
def test_scroll_error(self):
bulk = []
def test_scroll_error(self) -> None:
bulk: Any = []
for x in range(4):
bulk.append({"index": {"_index": "test_index"}})
bulk.append({"value": x})
@@ -456,7 +456,7 @@ class TestScan(OpenSearchTestCase):
self.assertEqual(len(data), 3)
self.assertEqual(data[-1], {"scroll_data": 42})
def test_initial_search_error(self):
def test_initial_search_error(self) -> None:
with patch.object(self, "client") as client_mock:
client_mock.search.return_value = {
"_scroll_id": "dummy_id",
@@ -491,7 +491,7 @@ class TestScan(OpenSearchTestCase):
client_mock.scroll.assert_not_called()
client_mock.clear_scroll.assert_not_called()
def test_scan_auth_kwargs_forwarded(self):
def test_scan_auth_kwargs_forwarded(self) -> None:
for key, val in {
"api_key": ("name", "value"),
"http_auth": ("username", "password"),
@@ -510,7 +510,7 @@ class TestScan(OpenSearchTestCase):
}
client_mock.clear_scroll.return_value = {}
data = list(helpers.scan(self.client, index="test_index", **{key: val}))
data = list(helpers.scan(self.client, index="test_index", **{key: val})) # type: ignore
self.assertEqual(data, [{"search_data": 1}])
@@ -523,7 +523,7 @@ class TestScan(OpenSearchTestCase):
):
self.assertEqual(api_mock.call_args[1][key], val)
def test_scan_auth_kwargs_favor_scroll_kwargs_option(self):
def test_scan_auth_kwargs_favor_scroll_kwargs_option(self) -> None:
with patch.object(self, "client") as client_mock:
client_mock.search.return_value = {
"_scroll_id": "scroll_id",
@@ -555,8 +555,8 @@ class TestScan(OpenSearchTestCase):
self.assertEqual(client_mock.scroll.call_args[1]["sort"], "asc")
@patch("opensearchpy.helpers.actions.logger")
def test_logger(self, logger_mock):
bulk = []
def test_logger(self, logger_mock: Any) -> None:
bulk: Any = []
for x in range(4):
bulk.append({"index": {"_index": "test_index"}})
bulk.append({"value": x})
@@ -590,8 +590,8 @@ class TestScan(OpenSearchTestCase):
pass
logger_mock.warning.assert_called()
def test_clear_scroll(self):
bulk = []
def test_clear_scroll(self) -> None:
bulk: Any = []
for x in range(4):
bulk.append({"index": {"_index": "test_index"}})
bulk.append({"value": x})
@@ -617,7 +617,7 @@ class TestScan(OpenSearchTestCase):
)
spy.assert_not_called()
def test_shards_no_skipped_field(self):
def test_shards_no_skipped_field(self) -> None:
with patch.object(self, "client") as client_mock:
client_mock.search.return_value = {
"_scroll_id": "dummy_id",
@@ -646,8 +646,8 @@ class TestScan(OpenSearchTestCase):
class TestReindex(OpenSearchTestCase):
def setup_method(self, _):
bulk = []
def setup_method(self, _: Any) -> None:
bulk: Any = []
for x in range(100):
bulk.append({"index": {"_index": "test_index", "_id": x}})
bulk.append(
@@ -716,7 +716,7 @@ class TestReindex(OpenSearchTestCase):
class TestParentChildReindex(OpenSearchTestCase):
def setup_method(self, _):
def setup_method(self, _: Any) -> None:
body = {
"settings": {"number_of_shards": 1, "number_of_replicas": 0},
"mappings": {
@@ -25,10 +25,12 @@
# specific language governing permissions and limitations
# under the License.
from typing import Any
from opensearchpy import analyzer, token_filter, tokenizer
def test_simulate_with_just__builtin_tokenizer(client) -> None:
def test_simulate_with_just__builtin_tokenizer(client: Any) -> None:
a = analyzer("my-analyzer", tokenizer="keyword")
tokens = a.simulate("Hello World!", using=client).tokens
@@ -36,7 +38,7 @@ def test_simulate_with_just__builtin_tokenizer(client) -> None:
assert tokens[0].token == "Hello World!"
def test_simulate_complex(client) -> None:
def test_simulate_complex(client: Any) -> None:
a = analyzer(
"my-analyzer",
tokenizer=tokenizer("split_words", "simple_pattern_split", pattern=":"),
@@ -49,7 +51,7 @@ def test_simulate_complex(client) -> None:
assert ["this", "works"] == [t.token for t in tokens]
def test_simulate_builtin(client) -> None:
def test_simulate_builtin(client: Any) -> None:
a = analyzer("my-analyzer", "english")
tokens = a.simulate("fixes running").tokens
@@ -25,15 +25,17 @@
# specific language governing permissions and limitations
# under the License.
from typing import Any
from opensearchpy.helpers.search import Q, Search
def test_count_all(data_client) -> None:
def test_count_all(data_client: Any) -> None:
s = Search(using=data_client).index("git")
assert 53 == s.count()
def test_count_prefetch(data_client, mocker) -> None:
def test_count_prefetch(data_client: Any, mocker: Any) -> None:
mocker.spy(data_client, "count")
search = Search(using=data_client).index("git")
@@ -46,7 +48,7 @@ def test_count_prefetch(data_client, mocker) -> None:
assert data_client.count.call_count == 1
def test_count_filter(data_client) -> None:
def test_count_filter(data_client: Any) -> None:
s = Search(using=data_client).index("git").filter(~Q("exists", field="parent_shas"))
# initial commit + repo document
assert 2 == s.count()
@@ -30,7 +30,7 @@ from __future__ import unicode_literals
from typing import Any, Dict
def create_flat_git_index(client, index):
def create_flat_git_index(client: Any, index: Any) -> None:
# we will use user on several places
user_mapping = {
"properties": {"name": {"type": "text", "fields": {"raw": {"type": "keyword"}}}}
@@ -73,7 +73,7 @@ def create_flat_git_index(client, index):
)
def create_git_index(client, index):
def create_git_index(client: Any, index: Any) -> None:
# we will use user on several places
user_mapping = {
"properties": {"name": {"type": "text", "fields": {"raw": {"type": "keyword"}}}}
@@ -1095,7 +1095,7 @@ DATA = [
]
def flatten_doc(d) -> Dict[str, Any]:
def flatten_doc(d: Any) -> Dict[str, Any]:
src = d["_source"].copy()
del src["commit_repo"]
return {"_index": "flat-git", "_id": d["_id"], "_source": src}
@@ -1104,7 +1104,7 @@ def flatten_doc(d) -> Dict[str, Any]:
FLAT_DATA = [flatten_doc(d) for d in DATA if "routing" in d]
def create_test_git_data(d) -> Dict[str, Any]:
def create_test_git_data(d: Any) -> Dict[str, Any]:
src = d["_source"].copy()
return {
"_index": "test-git",
@@ -27,6 +27,7 @@
from datetime import datetime
from ipaddress import ip_address
from typing import Any
import pytest
from pytest import raises
@@ -78,7 +79,7 @@ class Repository(Document):
tags = Keyword()
@classmethod
def search(cls):
def search(cls, using: Any = None, index: Any = None) -> Any:
return super(Repository, cls).search().filter("term", commit_repo="repo")
class Index:
@@ -131,7 +132,7 @@ class SerializationDoc(Document):
name = "test-serialization"
def test_serialization(write_client):
def test_serialization(write_client: Any) -> None:
SerializationDoc.init()
write_client.index(
index="test-serialization",
@@ -161,7 +162,7 @@ def test_serialization(write_client):
}
def test_nested_inner_hits_are_wrapped_properly(pull_request) -> None:
def test_nested_inner_hits_are_wrapped_properly(pull_request: Any) -> None:
history_query = Q(
"nested",
path="comments.history",
@@ -189,7 +190,7 @@ def test_nested_inner_hits_are_wrapped_properly(pull_request) -> None:
assert "score" in history.meta
def test_nested_inner_hits_are_deserialized_properly(pull_request) -> None:
def test_nested_inner_hits_are_deserialized_properly(pull_request: Any) -> None:
s = PullRequest.search().query(
"nested",
inner_hits={},
@@ -204,7 +205,7 @@ def test_nested_inner_hits_are_deserialized_properly(pull_request) -> None:
assert isinstance(pr.comments[0].created_at, datetime)
def test_nested_top_hits_are_wrapped_properly(pull_request) -> None:
def test_nested_top_hits_are_wrapped_properly(pull_request: Any) -> None:
s = PullRequest.search()
s.aggs.bucket("comments", "nested", path="comments").metric(
"hits", "top_hits", size=1
@@ -216,7 +217,7 @@ def test_nested_top_hits_are_wrapped_properly(pull_request) -> None:
assert isinstance(r.aggregations.comments.hits.hits[0], Comment)
def test_update_object_field(write_client) -> None:
def test_update_object_field(write_client: Any) -> None:
Wiki.init()
w = Wiki(
owner=User(name="Honza Kral"),
@@ -236,7 +237,7 @@ def test_update_object_field(write_client) -> None:
assert w.ranked == {"test1": 0.1, "topic2": 0.2}
def test_update_script(write_client) -> None:
def test_update_script(write_client: Any) -> None:
Wiki.init()
w = Wiki(owner=User(name="Honza Kral"), _id="opensearch-py", views=42)
w.save()
@@ -246,7 +247,7 @@ def test_update_script(write_client) -> None:
assert w.views == 47
def test_update_retry_on_conflict(write_client) -> None:
def test_update_retry_on_conflict(write_client: Any) -> None:
Wiki.init()
w = Wiki(owner=User(name="Honza Kral"), _id="opensearch-py", views=42)
w.save()
@@ -260,8 +261,8 @@ def test_update_retry_on_conflict(write_client) -> None:
assert w.views == 52
@pytest.mark.parametrize("retry_on_conflict", [None, 0])
def test_update_conflicting_version(write_client, retry_on_conflict) -> None:
@pytest.mark.parametrize("retry_on_conflict", [None, 0]) # type: ignore
def test_update_conflicting_version(write_client: Any, retry_on_conflict: Any) -> None:
Wiki.init()
w = Wiki(owner=User(name="Honza Kral"), _id="opensearch-py", views=42)
w.save()
@@ -278,7 +279,7 @@ def test_update_conflicting_version(write_client, retry_on_conflict) -> None:
)
def test_save_and_update_return_doc_meta(write_client) -> None:
def test_save_and_update_return_doc_meta(write_client: Any) -> None:
Wiki.init()
w = Wiki(owner=User(name="Honza Kral"), _id="opensearch-py", views=42)
resp = w.save(return_doc_meta=True)
@@ -302,31 +303,33 @@ def test_save_and_update_return_doc_meta(write_client) -> None:
assert resp.keys().__contains__("_version")
def test_init(write_client) -> None:
def test_init(write_client: Any) -> None:
Repository.init(index="test-git")
assert write_client.indices.exists(index="test-git")
def test_get_raises_404_on_index_missing(data_client) -> None:
def test_get_raises_404_on_index_missing(data_client: Any) -> None:
with raises(NotFoundError):
Repository.get("opensearch-dsl-php", index="not-there")
def test_get_raises_404_on_non_existent_id(data_client) -> None:
def test_get_raises_404_on_non_existent_id(data_client: Any) -> None:
with raises(NotFoundError):
Repository.get("opensearch-dsl-php")
def test_get_returns_none_if_404_ignored(data_client) -> None:
def test_get_returns_none_if_404_ignored(data_client: Any) -> None:
assert None is Repository.get("opensearch-dsl-php", ignore=404)
def test_get_returns_none_if_404_ignored_and_index_doesnt_exist(data_client) -> None:
def test_get_returns_none_if_404_ignored_and_index_doesnt_exist(
data_client: Any,
) -> None:
assert None is Repository.get("42", index="not-there", ignore=404)
def test_get(data_client) -> None:
def test_get(data_client: Any) -> None:
opensearch_repo = Repository.get("opensearch-py")
assert isinstance(opensearch_repo, Repository)
@@ -334,15 +337,15 @@ def test_get(data_client) -> None:
assert datetime(2014, 3, 3) == opensearch_repo.created_at
def test_exists_return_true(data_client) -> None:
def test_exists_return_true(data_client: Any) -> None:
assert Repository.exists("opensearch-py")
def test_exists_false(data_client) -> None:
def test_exists_false(data_client: Any) -> None:
assert not Repository.exists("opensearch-dsl-php")
def test_get_with_tz_date(data_client) -> None:
def test_get_with_tz_date(data_client: Any) -> None:
first_commit = Commit.get(
id="3ca6e1e73a071a705b4babd2f581c91a2a3e5037", routing="opensearch-py"
)
@@ -354,7 +357,7 @@ def test_get_with_tz_date(data_client) -> None:
)
def test_save_with_tz_date(data_client) -> None:
def test_save_with_tz_date(data_client: Any) -> None:
tzinfo = timezone("Europe/Prague")
first_commit = Commit.get(
id="3ca6e1e73a071a705b4babd2f581c91a2a3e5037", routing="opensearch-py"
@@ -381,7 +384,7 @@ COMMIT_DOCS_WITH_MISSING = [
]
def test_mget(data_client) -> None:
def test_mget(data_client: Any) -> None:
commits = Commit.mget(COMMIT_DOCS_WITH_MISSING)
assert commits[0] is None
assert commits[1].meta.id == "3ca6e1e73a071a705b4babd2f581c91a2a3e5037"
@@ -389,23 +392,23 @@ def test_mget(data_client) -> None:
assert commits[3].meta.id == "eb3e543323f189fd7b698e66295427204fff5755"
def test_mget_raises_exception_when_missing_param_is_invalid(data_client) -> None:
def test_mget_raises_exception_when_missing_param_is_invalid(data_client: Any) -> None:
with raises(ValueError):
Commit.mget(COMMIT_DOCS_WITH_MISSING, missing="raj")
def test_mget_raises_404_when_missing_param_is_raise(data_client) -> None:
def test_mget_raises_404_when_missing_param_is_raise(data_client: Any) -> None:
with raises(NotFoundError):
Commit.mget(COMMIT_DOCS_WITH_MISSING, missing="raise")
def test_mget_ignores_missing_docs_when_missing_param_is_skip(data_client) -> None:
def test_mget_ignores_missing_docs_when_missing_param_is_skip(data_client: Any) -> None:
commits = Commit.mget(COMMIT_DOCS_WITH_MISSING, missing="skip")
assert commits[0].meta.id == "3ca6e1e73a071a705b4babd2f581c91a2a3e5037"
assert commits[1].meta.id == "eb3e543323f189fd7b698e66295427204fff5755"
def test_update_works_from_search_response(data_client) -> None:
def test_update_works_from_search_response(data_client: Any) -> None:
opensearch_repo = Repository.search().execute()[0]
opensearch_repo.update(owner={"other_name": "opensearchpy"})
@@ -416,7 +419,7 @@ def test_update_works_from_search_response(data_client) -> None:
assert "opensearch" == new_version.owner.name
def test_update(data_client) -> None:
def test_update(data_client: Any) -> None:
opensearch_repo = Repository.get("opensearch-py")
v = opensearch_repo.meta.version
@@ -440,7 +443,7 @@ def test_update(data_client) -> None:
assert "primary_term" in new_version.meta
def test_save_updates_existing_doc(data_client) -> None:
def test_save_updates_existing_doc(data_client: Any) -> None:
opensearch_repo = Repository.get("opensearch-py")
opensearch_repo.new_field = "testing-save"
@@ -453,7 +456,7 @@ def test_save_updates_existing_doc(data_client) -> None:
assert new_repo["_seq_no"] == opensearch_repo.meta.seq_no
def test_save_automatically_uses_seq_no_and_primary_term(data_client) -> None:
def test_save_automatically_uses_seq_no_and_primary_term(data_client: Any) -> None:
opensearch_repo = Repository.get("opensearch-py")
opensearch_repo.meta.seq_no += 1
@@ -461,7 +464,7 @@ def test_save_automatically_uses_seq_no_and_primary_term(data_client) -> None:
opensearch_repo.save()
def test_delete_automatically_uses_seq_no_and_primary_term(data_client) -> None:
def test_delete_automatically_uses_seq_no_and_primary_term(data_client: Any) -> None:
opensearch_repo = Repository.get("opensearch-py")
opensearch_repo.meta.seq_no += 1
@@ -469,13 +472,13 @@ def test_delete_automatically_uses_seq_no_and_primary_term(data_client) -> None:
opensearch_repo.delete()
def assert_doc_equals(expected, actual) -> None:
def assert_doc_equals(expected: Any, actual: Any) -> None:
for f in expected:
assert f in actual
assert actual[f] == expected[f]
def test_can_save_to_different_index(write_client):
def test_can_save_to_different_index(write_client: Any) -> None:
test_repo = Repository(description="testing", meta={"id": 42})
assert test_repo.save(index="test-document")
@@ -490,7 +493,7 @@ def test_can_save_to_different_index(write_client):
)
def test_save_without_skip_empty_will_include_empty_fields(write_client) -> None:
def test_save_without_skip_empty_will_include_empty_fields(write_client: Any) -> None:
test_repo = Repository(field_1=[], field_2=None, field_3={}, meta={"id": 42})
assert test_repo.save(index="test-document", skip_empty=False)
@@ -505,7 +508,7 @@ def test_save_without_skip_empty_will_include_empty_fields(write_client) -> None
)
def test_delete(write_client) -> None:
def test_delete(write_client: Any) -> None:
write_client.create(
index="test-document",
id="opensearch-py",
@@ -526,11 +529,11 @@ def test_delete(write_client) -> None:
)
def test_search(data_client) -> None:
def test_search(data_client: Any) -> None:
assert Repository.search().count() == 1
def test_search_returns_proper_doc_classes(data_client) -> None:
def test_search_returns_proper_doc_classes(data_client: Any) -> None:
result = Repository.search().execute()
opensearch_repo = result.hits[0]
@@ -539,11 +542,13 @@ def test_search_returns_proper_doc_classes(data_client) -> None:
assert opensearch_repo.owner.name == "opensearch"
def test_refresh_mapping(data_client) -> None:
def test_refresh_mapping(data_client: Any) -> None:
class Commit(Document):
class Index:
name = "git"
_index: Any
Commit._index.load_mappings()
assert "stats" in Commit._index._mapping
@@ -553,7 +558,7 @@ def test_refresh_mapping(data_client) -> None:
assert isinstance(Commit._index._mapping["committed_date"], Date)
def test_highlight_in_meta(data_client) -> None:
def test_highlight_in_meta(data_client: Any) -> None:
commit = (
Commit.search()
.query("match", description="inverting")
@@ -26,6 +26,7 @@
# under the License.
from datetime import datetime
from typing import Any
import pytest
@@ -66,8 +67,8 @@ class MetricSearch(FacetedSearch):
}
@pytest.fixture(scope="session")
def commit_search_cls(opensearch_version):
@pytest.fixture(scope="session") # type: ignore
def commit_search_cls(opensearch_version: Any) -> Any:
interval_kwargs = {"fixed_interval": "1d"}
class CommitSearch(FacetedSearch):
@@ -91,8 +92,8 @@ def commit_search_cls(opensearch_version):
return CommitSearch
@pytest.fixture(scope="session")
def repo_search_cls(opensearch_version):
@pytest.fixture(scope="session") # type: ignore
def repo_search_cls(opensearch_version: Any) -> Any:
interval_type = "calendar_interval"
class RepoSearch(FacetedSearch):
@@ -105,15 +106,15 @@ def repo_search_cls(opensearch_version):
),
}
def search(self):
def search(self) -> Any:
s = super(RepoSearch, self).search()
return s.filter("term", commit_repo="repo")
return RepoSearch
@pytest.fixture(scope="session")
def pr_search_cls(opensearch_version):
@pytest.fixture(scope="session") # type: ignore
def pr_search_cls(opensearch_version: Any) -> Any:
interval_type = "calendar_interval"
class PRSearch(FacetedSearch):
@@ -131,7 +132,7 @@ def pr_search_cls(opensearch_version):
return PRSearch
def test_facet_with_custom_metric(data_client) -> None:
def test_facet_with_custom_metric(data_client: Any) -> None:
ms = MetricSearch()
r = ms.execute()
@@ -140,7 +141,7 @@ def test_facet_with_custom_metric(data_client) -> None:
assert dates[0] == 1399038439000
def test_nested_facet(pull_request, pr_search_cls) -> None:
def test_nested_facet(pull_request: Any, pr_search_cls: Any) -> None:
prs = pr_search_cls()
r = prs.execute()
@@ -148,7 +149,7 @@ def test_nested_facet(pull_request, pr_search_cls) -> None:
assert [(datetime(2018, 1, 1, 0, 0), 1, False)] == r.facets.comments
def test_nested_facet_with_filter(pull_request, pr_search_cls) -> None:
def test_nested_facet_with_filter(pull_request: Any, pr_search_cls: Any) -> None:
prs = pr_search_cls(filters={"comments": datetime(2018, 1, 1, 0, 0)})
r = prs.execute()
@@ -160,7 +161,7 @@ def test_nested_facet_with_filter(pull_request, pr_search_cls) -> None:
assert not r.hits
def test_datehistogram_facet(data_client, repo_search_cls) -> None:
def test_datehistogram_facet(data_client: Any, repo_search_cls: Any) -> None:
rs = repo_search_cls()
r = rs.execute()
@@ -168,7 +169,7 @@ def test_datehistogram_facet(data_client, repo_search_cls) -> None:
assert [(datetime(2014, 3, 1, 0, 0), 1, False)] == r.facets.created
def test_boolean_facet(data_client, repo_search_cls) -> None:
def test_boolean_facet(data_client: Any, repo_search_cls: Any) -> None:
rs = repo_search_cls()
r = rs.execute()
@@ -179,7 +180,7 @@ def test_boolean_facet(data_client, repo_search_cls) -> None:
def test_empty_search_finds_everything(
data_client, opensearch_version, commit_search_cls
data_client: Any, opensearch_version: Any, commit_search_cls: Any
) -> None:
cs = commit_search_cls()
r = cs.execute()
@@ -225,7 +226,7 @@ def test_empty_search_finds_everything(
def test_term_filters_are_shown_as_selected_and_data_is_filtered(
data_client, commit_search_cls
data_client: Any, commit_search_cls: Any
) -> None:
cs = commit_search_cls(filters={"files": "test_opensearchpy/test_dsl"})
@@ -271,7 +272,7 @@ def test_term_filters_are_shown_as_selected_and_data_is_filtered(
def test_range_filters_are_shown_as_selected_and_data_is_filtered(
data_client, commit_search_cls
data_client: Any, commit_search_cls: Any
) -> None:
cs = commit_search_cls(filters={"deletions": "better"})
@@ -280,7 +281,7 @@ def test_range_filters_are_shown_as_selected_and_data_is_filtered(
assert 19 == r.hits.total.value
def test_pagination(data_client, commit_search_cls) -> None:
def test_pagination(data_client: Any, commit_search_cls: Any) -> None:
cs = commit_search_cls()
cs = cs[0:20]
@@ -25,6 +25,8 @@
# specific language governing permissions and limitations
# under the License.
from typing import Any
from opensearchpy import Date, Document, Index, IndexTemplate, Text
from opensearchpy.helpers import analysis
@@ -34,7 +36,7 @@ class Post(Document):
published_from = Date()
def test_index_template_works(write_client) -> None:
def test_index_template_works(write_client: Any) -> None:
it = IndexTemplate("test-template", "test-*")
it.document(Post)
it.settings(number_of_replicas=0, number_of_shards=1)
@@ -55,7 +57,7 @@ def test_index_template_works(write_client) -> None:
} == write_client.indices.get_mapping(index="test-blog")
def test_index_can_be_saved_even_with_settings(write_client) -> None:
def test_index_can_be_saved_even_with_settings(write_client: Any) -> None:
i = Index("test-blog", using=write_client)
i.settings(number_of_shards=3, number_of_replicas=0)
i.save()
@@ -67,12 +69,12 @@ def test_index_can_be_saved_even_with_settings(write_client) -> None:
)
def test_index_exists(data_client) -> None:
def test_index_exists(data_client: Any) -> None:
assert Index("git").exists()
assert not Index("not-there").exists()
def test_index_can_be_created_with_settings_and_mappings(write_client) -> None:
def test_index_can_be_created_with_settings_and_mappings(write_client: Any) -> None:
i = Index("test-blog", using=write_client)
i.document(Post)
i.settings(number_of_replicas=0, number_of_shards=1)
@@ -97,7 +99,7 @@ def test_index_can_be_created_with_settings_and_mappings(write_client) -> None:
}
def test_delete(write_client) -> None:
def test_delete(write_client: Any) -> None:
write_client.indices.create(
index="test-index",
body={"settings": {"number_of_replicas": 0, "number_of_shards": 1}},
@@ -108,7 +110,7 @@ def test_delete(write_client) -> None:
assert not write_client.indices.exists(index="test-index")
def test_multiple_indices_with_same_doc_type_work(write_client) -> None:
def test_multiple_indices_with_same_doc_type_work(write_client: Any) -> None:
i1 = Index("test-index-1", using=write_client)
i2 = Index("test-index-2", using=write_client)
@@ -116,8 +118,8 @@ def test_multiple_indices_with_same_doc_type_work(write_client) -> None:
i.document(Post)
i.create()
for i in ("test-index-1", "test-index-2"):
settings = write_client.indices.get_settings(index=i)
assert settings[i]["settings"]["index"]["analysis"] == {
for j in ("test-index-1", "test-index-2"):
settings = write_client.indices.get_settings(index=j)
assert settings[j]["settings"]["index"]["analysis"] == {
"analyzer": {"my_analyzer": {"type": "custom", "tokenizer": "keyword"}}
}
@@ -25,13 +25,15 @@
# specific language governing permissions and limitations
# under the License.
from typing import Any
from pytest import raises
from opensearchpy import exceptions
from opensearchpy.helpers import analysis, mapping
def test_mapping_saved_into_opensearch(write_client) -> None:
def test_mapping_saved_into_opensearch(write_client: Any) -> None:
m = mapping.Mapping()
m.field(
"name", "text", analyzer=analysis.analyzer("my_analyzer", tokenizer="keyword")
@@ -52,7 +54,7 @@ def test_mapping_saved_into_opensearch(write_client) -> None:
def test_mapping_saved_into_opensearch_when_index_already_exists_closed(
write_client,
write_client: Any,
) -> None:
m = mapping.Mapping()
m.field(
@@ -77,7 +79,7 @@ def test_mapping_saved_into_opensearch_when_index_already_exists_closed(
def test_mapping_saved_into_opensearch_when_index_already_exists_with_analysis(
write_client,
write_client: Any,
) -> None:
m = mapping.Mapping()
analyzer = analysis.analyzer("my_analyzer", tokenizer="keyword")
@@ -107,7 +109,7 @@ def test_mapping_saved_into_opensearch_when_index_already_exists_with_analysis(
} == write_client.indices.get_mapping(index="test-mapping")
def test_mapping_gets_updated_from_opensearch(write_client):
def test_mapping_gets_updated_from_opensearch(write_client: Any) -> None:
write_client.indices.create(
index="test-mapping",
body={
@@ -27,6 +27,8 @@
from __future__ import unicode_literals
from typing import Any
from pytest import raises
from opensearchpy import (
@@ -50,7 +52,7 @@ class Repository(Document):
tags = Keyword()
@classmethod
def search(cls):
def search(cls, using: Any = None, index: Any = None) -> Any:
return super(Repository, cls).search().filter("term", commit_repo="repo")
class Index:
@@ -62,7 +64,7 @@ class Commit(Document):
name = "flat-git"
def test_filters_aggregation_buckets_are_accessible(data_client) -> None:
def test_filters_aggregation_buckets_are_accessible(data_client: Any) -> None:
has_tests_query = Q("term", files="test_opensearchpy/test_dsl")
s = Commit.search()[0:0]
s.aggs.bucket("top_authors", "terms", field="author.name.raw").bucket(
@@ -83,7 +85,7 @@ def test_filters_aggregation_buckets_are_accessible(data_client) -> None:
)
def test_top_hits_are_wrapped_in_response(data_client) -> None:
def test_top_hits_are_wrapped_in_response(data_client: Any) -> None:
s = Commit.search()[0:0]
s.aggs.bucket("top_authors", "terms", field="author.name.raw").metric(
"top_commits", "top_hits", size=5
@@ -99,7 +101,7 @@ def test_top_hits_are_wrapped_in_response(data_client) -> None:
assert isinstance(hits[0], Commit)
def test_inner_hits_are_wrapped_in_response(data_client) -> None:
def test_inner_hits_are_wrapped_in_response(data_client: Any) -> None:
s = Search(index="git")[0:1].query(
"has_parent", parent_type="repo", inner_hits={}, query=Q("match_all")
)
@@ -110,7 +112,7 @@ def test_inner_hits_are_wrapped_in_response(data_client) -> None:
assert repr(commit.meta.inner_hits.repo[0]).startswith("<Hit(git/opensearch-py): ")
def test_scan_respects_doc_types(data_client) -> None:
def test_scan_respects_doc_types(data_client: Any) -> None:
repos = list(Repository.search().scan())
assert 1 == len(repos)
@@ -118,7 +120,7 @@ def test_scan_respects_doc_types(data_client) -> None:
assert repos[0].organization == "opensearch"
def test_scan_iterates_through_all_docs(data_client) -> None:
def test_scan_iterates_through_all_docs(data_client: Any) -> None:
s = Search(index="flat-git")
commits = list(s.scan())
@@ -127,7 +129,7 @@ def test_scan_iterates_through_all_docs(data_client) -> None:
assert {d["_id"] for d in FLAT_DATA} == {c.meta.id for c in commits}
def test_response_is_cached(data_client) -> None:
def test_response_is_cached(data_client: Any) -> None:
s = Repository.search()
repos = list(s)
@@ -135,7 +137,7 @@ def test_response_is_cached(data_client) -> None:
assert s._response.hits == repos
def test_multi_search(data_client) -> None:
def test_multi_search(data_client: Any) -> None:
s1 = Repository.search()
s2 = Search(index="flat-git")
@@ -152,7 +154,7 @@ def test_multi_search(data_client) -> None:
assert r2._search is s2
def test_multi_missing(data_client) -> None:
def test_multi_missing(data_client: Any) -> None:
s1 = Repository.search()
s2 = Search(index="flat-git")
s3 = Search(index="does_not_exist")
@@ -175,7 +177,7 @@ def test_multi_missing(data_client) -> None:
assert r3 is None
def test_raw_subfield_can_be_used_in_aggs(data_client) -> None:
def test_raw_subfield_can_be_used_in_aggs(data_client: Any) -> None:
s = Search(index="git")[0:0]
s.aggs.bucket("authors", "terms", field="author.name.raw", size=1)
@@ -25,11 +25,13 @@
# specific language governing permissions and limitations
# under the License.
from typing import Any
from opensearchpy.helpers.search import Q
from opensearchpy.helpers.update_by_query import UpdateByQuery
def test_update_by_query_no_script(write_client, setup_ubq_tests) -> None:
def test_update_by_query_no_script(write_client: Any, setup_ubq_tests: Any) -> None:
index = setup_ubq_tests
ubq = (
@@ -48,7 +50,7 @@ def test_update_by_query_no_script(write_client, setup_ubq_tests) -> None:
assert response.success()
def test_update_by_query_with_script(write_client, setup_ubq_tests) -> None:
def test_update_by_query_with_script(write_client: Any, setup_ubq_tests: Any) -> None:
index = setup_ubq_tests
ubq = (
@@ -65,7 +67,7 @@ def test_update_by_query_with_script(write_client, setup_ubq_tests) -> None:
assert response.version_conflicts == 0
def test_delete_by_query_with_script(write_client, setup_ubq_tests) -> None:
def test_delete_by_query_with_script(write_client: Any, setup_ubq_tests: Any) -> None:
index = setup_ubq_tests
ubq = (
@@ -23,7 +23,7 @@ class TestAlertingPlugin(OpenSearchTestCase):
(OPENSEARCH_VERSION) and (OPENSEARCH_VERSION < (2, 0, 0)),
"Plugin not supported for opensearch version",
)
def test_create_destination(self):
def test_create_destination(self) -> None:
# Test to create alert destination
dummy_destination = {
"name": "my-destination",
@@ -54,7 +54,7 @@ class TestAlertingPlugin(OpenSearchTestCase):
(OPENSEARCH_VERSION) and (OPENSEARCH_VERSION < (2, 0, 0)),
"Plugin not supported for opensearch version",
)
def test_create_monitor(self):
def test_create_monitor(self) -> None:
# Create a dummy destination
self.test_create_destination()
@@ -36,6 +36,7 @@ import os
import re
import warnings
import zipfile
from typing import Any
import pytest
import urllib3
@@ -142,23 +143,23 @@ FALSEY_VALUES = ("", None, False, 0, 0.0)
class YamlRunner:
def __init__(self, client) -> None:
def __init__(self, client: Any) -> None:
self.client = client
self.last_response = None
self.last_response: Any = None
self._run_code = None
self._setup_code = None
self._teardown_code = None
self._state = {}
self._run_code: Any = None
self._setup_code: Any = None
self._teardown_code: Any = None
self._state: Any = {}
def use_spec(self, test_spec) -> None:
def use_spec(self, test_spec: Any) -> None:
self._setup_code = test_spec.pop("setup", None)
self._run_code = test_spec.pop("run", None)
self._teardown_code = test_spec.pop("teardown", None)
def setup(self):
def setup(self) -> Any:
# Pull skips from individual tests to not do unnecessary setup.
skip_code = []
skip_code: Any = []
for action in self._run_code:
assert len(action) == 1
action_type, _ = list(action.items())[0]
@@ -174,12 +175,12 @@ class YamlRunner:
if self._setup_code:
self.run_code(self._setup_code)
def teardown(self) -> None:
def teardown(self) -> Any:
if self._teardown_code:
self.section("teardown")
self.run_code(self._teardown_code)
def opensearch_version(self):
def opensearch_version(self) -> Any:
global OPENSEARCH_VERSION
if OPENSEARCH_VERSION is None:
version_string = (self.client.info())["version"]["number"]
@@ -189,10 +190,10 @@ class YamlRunner:
OPENSEARCH_VERSION = tuple(int(v) if v.isdigit() else 99 for v in version)
return OPENSEARCH_VERSION
def section(self, name) -> None:
def section(self, name: str) -> None:
print(("=" * 10) + " " + name + " " + ("=" * 10))
def run(self) -> None:
def run(self) -> Any:
try:
self.setup()
self.section("test")
@@ -203,7 +204,7 @@ class YamlRunner:
except Exception:
pass
def run_code(self, test) -> None:
def run_code(self, test: Any) -> Any:
"""Execute an instruction based on its type."""
for action in test:
assert len(action) == 1
@@ -215,7 +216,7 @@ class YamlRunner:
else:
raise RuntimeError("Invalid action type %r" % (action_type,))
def run_do(self, action) -> None:
def run_do(self, action: Any) -> Any:
api = self.client
headers = action.pop("headers", None)
catch = action.pop("catch", None)
@@ -267,7 +268,7 @@ class YamlRunner:
# Filter out warnings raised by other components.
caught_warnings = [
str(w.message)
str(w.message) # type: ignore
for w in caught_warnings
if w.category == OpenSearchWarning
and str(w.message) not in allowed_warnings
@@ -275,13 +276,13 @@ class YamlRunner:
# Sorting removes the issue with order raised. We only care about
# if all warnings are raised in the single API call.
if warn and sorted(warn) != sorted(caught_warnings):
if warn and sorted(warn) != sorted(caught_warnings): # type: ignore
raise AssertionError(
"Expected warnings not equal to actual warnings: expected=%r actual=%r"
% (warn, caught_warnings)
)
def run_catch(self, catch, exception) -> None:
def run_catch(self, catch: Any, exception: Any) -> None:
if catch == "param":
assert isinstance(exception, TypeError)
return
@@ -296,7 +297,7 @@ class YamlRunner:
) is not None
self.last_response = exception.info
def run_skip(self, skip) -> None:
def run_skip(self, skip: Any) -> Any:
global IMPLEMENTED_FEATURES
if "features" in skip:
@@ -318,32 +319,32 @@ class YamlRunner:
if min_version <= (self.opensearch_version()) <= max_version:
pytest.skip(reason)
def run_gt(self, action) -> None:
def run_gt(self, action: Any) -> None:
for key, value in action.items():
value = self._resolve(value)
assert self._lookup(key) > value
def run_gte(self, action) -> None:
def run_gte(self, action: Any) -> None:
for key, value in action.items():
value = self._resolve(value)
assert self._lookup(key) >= value
def run_lt(self, action) -> None:
def run_lt(self, action: Any) -> None:
for key, value in action.items():
value = self._resolve(value)
assert self._lookup(key) < value
def run_lte(self, action) -> None:
def run_lte(self, action: Any) -> None:
for key, value in action.items():
value = self._resolve(value)
assert self._lookup(key) <= value
def run_set(self, action) -> None:
def run_set(self, action: Any) -> None:
for key, value in action.items():
value = self._resolve(value)
self._state[value] = self._lookup(key)
def run_is_false(self, action) -> None:
def run_is_false(self, action: Any) -> None:
try:
value = self._lookup(action)
except AssertionError:
@@ -351,23 +352,23 @@ class YamlRunner:
else:
assert value in FALSEY_VALUES
def run_is_true(self, action) -> None:
def run_is_true(self, action: Any) -> None:
value = self._lookup(action)
assert value not in FALSEY_VALUES
def run_length(self, action) -> None:
def run_length(self, action: Any) -> None:
for path, expected in action.items():
value = self._lookup(path)
expected = self._resolve(expected)
assert expected == len(value)
def run_match(self, action) -> None:
def run_match(self, action: Any) -> None:
for path, expected in action.items():
value = self._lookup(path)
expected = self._resolve(expected)
if (
isinstance(expected, string_types)
isinstance(expected, str)
and expected.startswith("/")
and expected.endswith("/")
):
@@ -379,7 +380,7 @@ class YamlRunner:
else:
self._assert_match_equals(value, expected)
def run_contains(self, action) -> None:
def run_contains(self, action: Any) -> None:
for path, expected in action.items():
value = self._lookup(path) # list[dict[str,str]] is returned
expected = self._resolve(expected) # dict[str, str]
@@ -387,7 +388,7 @@ class YamlRunner:
if expected not in value:
raise AssertionError("%s is not contained by %s" % (expected, value))
def run_transform_and_set(self, action) -> None:
def run_transform_and_set(self, action: Any) -> None:
for key, value in action.items():
# Convert #base64EncodeCredentials(id,api_key) to ["id", "api_key"]
if "#base64EncodeCredentials" in value:
@@ -397,7 +398,7 @@ class YamlRunner:
(self._lookup(value[0]), self._lookup(value[1]))
)
def _resolve(self, value):
def _resolve(self, value: Any) -> Any:
# resolve variables
if isinstance(value, string_types) and "$" in value:
for k, v in self._state.items():
@@ -422,12 +423,13 @@ class YamlRunner:
value = list(map(self._resolve, value))
return value
def _lookup(self, path):
def _lookup(self, path: str) -> Any:
# fetch the possibly nested value from last_response
value = self.last_response
value: Any = self.last_response
if path == "$body":
return value
path = path.replace(r"\.", "\1")
step: Any
for step in path.split("."):
if not step:
continue
@@ -449,10 +451,10 @@ class YamlRunner:
value = value[step]
return value
def _feature_enabled(self, name) -> bool:
def _feature_enabled(self, name: str) -> Any:
return False
def _assert_match_equals(self, a, b) -> None:
def _assert_match_equals(self, a: Any, b: Any) -> None:
# Handle for large floating points with 'E'
if isinstance(b, string_types) and isinstance(a, float) and "e" in repr(a):
a = repr(a).replace("e+", "E")
@@ -460,8 +462,8 @@ class YamlRunner:
assert a == b, "%r does not match %r" % (a, b)
@pytest.fixture(scope="function")
def sync_runner(sync_client):
@pytest.fixture(scope="function") # type: ignore
def sync_runner(sync_client: Any) -> Any:
return YamlRunner(sync_client)
@@ -532,8 +534,8 @@ except Exception as e:
if not RUN_ASYNC_REST_API_TESTS:
@pytest.mark.parametrize("test_spec", YAML_TEST_SPECS)
def test_rest_api_spec(test_spec, sync_runner) -> None:
@pytest.mark.parametrize("test_spec", YAML_TEST_SPECS) # type: ignore
def test_rest_api_spec(test_spec: Any, sync_runner: Any) -> None:
if test_spec.get("skip", False):
pytest.skip("Manually skipped in 'SKIP_TESTS'")
sync_runner.use_spec(test_spec)