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
@@ -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")