Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lifecycle integration tests. #856

Closed
Closed
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
6 changes: 5 additions & 1 deletion DEVELOPER_GUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,11 @@ test_opensearchpy/test_connection.py::TestRequestsConnection::test_no_http_compr
test_opensearchpy/test_async/test_connection.py::TestAIOHttpConnection::test_no_http_compression PASSED [100%]
```

Note that integration tests require docker to be installed and running, and downloads quite a bit of data from over the internet and hence take few minutes to complete.
```
./.ci/run-tests false 2.16.0 test_indices_lifecycle
```

Note that integration tests require docker to be installed and running, and downloads quite a bit of data from the internet and hence take few minutes to complete.

## Linter

Expand Down
4 changes: 2 additions & 2 deletions samples/hello/hello.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@

def main() -> None:
"""
an example showing how to create an synchronous connection to
An example showing how to create a synchronous connection to
OpenSearch, create an index, index a document and search to
return the document
return the document.
"""
host = "localhost"
port = 9200
Expand Down
4 changes: 2 additions & 2 deletions samples/hello/hello_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@

async def main() -> None:
"""
an example showing how to create an asynchronous connection
An example showing how to create an asynchronous connection
to OpenSearch, create an index, index a document and
search to return the document
search to return the document.
"""
# connect to OpenSearch
host = "localhost"
Expand Down
63 changes: 63 additions & 0 deletions samples/hello/unicode.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#!/usr/bin/env python

# SPDX-License-Identifier: Apache-2.0
#
# The OpenSearch Contributors require contributions made to
# this file be licensed under the Apache-2.0 license or a
# compatible open source license.
#
# Modifications Copyright OpenSearch Contributors. See
# GitHub history for details.


import os

from opensearchpy import OpenSearch

# connect to OpenSearch


def main() -> None:
"""
An example showing how to create a synchronous connection to
OpenSearch, create an index, index a document and search to
return the document.
"""
host = "localhost"
port = 9200
auth = (
"admin",
os.getenv("OPENSEARCH_PASSWORD", "admin"),
) # For testing only. Don't store credentials in code.

client = OpenSearch(
hosts=[{"host": host, "port": port}],
http_auth=auth,
use_ssl=True,
verify_certs=False,
ssl_show_warn=False,
)

info = client.info()
print(f"Welcome to {info['version']['distribution']} {info['version']['number']}!")

index_name = "кино"
index_create_result = client.indices.create(index=index_name)
print(index_create_result)

document = {"название": "Солярис", "автор": "Андрей Тарковский", "год": "2011"}
id = "соларис@2011"
doc_insert_result = client.index(
index=index_name, body=document, id=id, refresh=True
)
print(doc_insert_result)

doc_delete_result = client.delete(index=index_name, id=id)
print(doc_delete_result)

index_delete_result = client.indices.delete(index=index_name)
print(index_delete_result)


if __name__ == "__main__":
main()
72 changes: 72 additions & 0 deletions samples/hello/unicode_async.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#!/usr/bin/env python

# SPDX-License-Identifier: Apache-2.0
#
# The OpenSearch Contributors require contributions made to
# this file be licensed under the Apache-2.0 license or a
# compatible open source license.
#
# Modifications Copyright OpenSearch Contributors. See
# GitHub history for details.


import asyncio
import os

from opensearchpy import AsyncOpenSearch


async def main() -> None:
"""
An example showing how to create an asynchronous connection
to OpenSearch, create an index, index a document and
search to return the document.
"""
# connect to OpenSearch
host = "localhost"
port = 9200
auth = (
"admin",
os.getenv("OPENSEARCH_PASSWORD", "admin"),
) # For testing only. Don't store credentials in code.

client = AsyncOpenSearch(
hosts=[{"host": host, "port": port}],
http_auth=auth,
use_ssl=True,
verify_certs=False,
ssl_show_warn=False,
)

try:
info = await client.info()
print(
f"Welcome to {info['version']['distribution']} {info['version']['number']}!"
)

index_name = "кино"
index_create_result = await client.indices.create(index=index_name)
print(index_create_result)

document = {"название": "Солярис", "автор": "Андрей Тарковский", "год": "2011"}
id = "соларис@2011"
doc_insert_result = await client.index(
index=index_name, body=document, id=id, refresh=True
)
print(doc_insert_result)

doc_delete_result = await client.delete(index=index_name, id=id)
print(doc_delete_result)

index_delete_result = await client.indices.delete(index=index_name)
print(index_delete_result)

finally:
await client.close()


if __name__ == "__main__":
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
loop.run_until_complete(main())
loop.close()
60 changes: 60 additions & 0 deletions test_opensearchpy/test_async/test_server/test_clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,70 @@
import pytest
from _pytest.mark.structures import MarkDecorator

from opensearchpy.exceptions import RequestError

pytestmark: MarkDecorator = pytest.mark.asyncio


class TestSpecialCharacters:
async def test_index_with_slash(self, async_client: Any) -> None:
index_name = "movies/shmovies"
with pytest.raises(RequestError) as e:
await async_client.indices.create(index=index_name)
assert (
str(e.value)
== "RequestError(400, 'invalid_index_name_exception', 'Invalid index name [movies/shmovies], must not contain the following characters [ , \", *, \\\\, <, |, ,, >, /, ?]')"
)


class TestUnicode:
async def test_indices_lifecycle_english(self, async_client: Any) -> None:
index_name = "movies"

index_create_result = await async_client.indices.create(index=index_name)
assert index_create_result["acknowledged"] is True
assert index_name == index_create_result["index"]

document = {"name": "Solaris", "director": "Andrei Tartakovsky", "year": "2011"}
id = "solaris@2011"
doc_insert_result = await async_client.index(
index=index_name, body=document, id=id, refresh=True
)
assert "created" == doc_insert_result["result"]
assert index_name == doc_insert_result["_index"]
assert id == doc_insert_result["_id"]

doc_delete_result = await async_client.delete(index=index_name, id=id)
assert "deleted" == doc_delete_result["result"]
assert index_name == doc_delete_result["_index"]
assert id == doc_delete_result["_id"]

index_delete_result = await async_client.indices.delete(index=index_name)
assert index_delete_result["acknowledged"] is True

async def test_indices_lifecycle_russian(self, async_client: Any) -> None:
index_name = "кино"
index_create_result = await async_client.indices.create(index=index_name)
assert index_create_result["acknowledged"] is True
assert index_name == index_create_result["index"]

document = {"название": "Солярис", "автор": "Андрей Тарковский", "год": "2011"}
id = "соларис@2011"
doc_insert_result = await async_client.index(
index=index_name, body=document, id=id, refresh=True
)
assert "created" == doc_insert_result["result"]
assert index_name == doc_insert_result["_index"]
assert id == doc_insert_result["_id"]

doc_delete_result = await async_client.delete(index=index_name, id=id)
assert "deleted" == doc_delete_result["result"]
assert index_name == doc_delete_result["_index"]
assert id == doc_delete_result["_id"]

index_delete_result = await async_client.indices.delete(index=index_name)
assert index_delete_result["acknowledged"] is True

async def test_indices_analyze(self, async_client: Any) -> None:
await async_client.indices.analyze(body='{"text": "привет"}')

Expand Down
62 changes: 62 additions & 0 deletions test_opensearchpy/test_server/test_clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,72 @@
# under the License.


import pytest

from opensearchpy.exceptions import RequestError

from . import OpenSearchTestCase


class TestSpecialCharacters(OpenSearchTestCase):
def test_index_with_slash(self) -> None:
index_name = "movies/shmovies"
with pytest.raises(RequestError) as e:
self.client.indices.create(index=index_name)
self.assertEqual(
str(e.value),
"RequestError(400, 'invalid_index_name_exception', 'Invalid index name [movies/shmovies], must not contain the following characters [ , \", *, \\\\, <, |, ,, >, /, ?]')",
)


class TestUnicode(OpenSearchTestCase):
def test_indices_lifecycle_english(self) -> None:
index_name = "movies"

index_create_result = self.client.indices.create(index=index_name)
self.assertTrue(index_create_result["acknowledged"])
self.assertEqual(index_name, index_create_result["index"])

document = {"name": "Solaris", "director": "Andrei Tartakovsky", "year": "2011"}
id = "solaris@2011"
doc_insert_result = self.client.index(
index=index_name, body=document, id=id, refresh=True
)
self.assertEqual("created", doc_insert_result["result"])
self.assertEqual(index_name, doc_insert_result["_index"])
self.assertEqual(id, doc_insert_result["_id"])

doc_delete_result = self.client.delete(index=index_name, id=id)
self.assertEqual("deleted", doc_delete_result["result"])
self.assertEqual(index_name, doc_delete_result["_index"])
self.assertEqual(id, doc_delete_result["_id"])

index_delete_result = self.client.indices.delete(index=index_name)
self.assertTrue(index_delete_result["acknowledged"])

def test_indices_lifecycle_russian(self) -> None:
index_name = "кино"
index_create_result = self.client.indices.create(index=index_name)
self.assertTrue(index_create_result["acknowledged"])
self.assertEqual(index_name, index_create_result["index"])

document = {"название": "Солярис", "автор": "Андрей Тарковский", "год": "2011"}
id = "соларис@2011"
doc_insert_result = self.client.index(
index=index_name, body=document, id=id, refresh=True
)
self.assertEqual("created", doc_insert_result["result"])
self.assertEqual(index_name, doc_insert_result["_index"])
self.assertEqual(id, doc_insert_result["_id"])

doc_delete_result = self.client.delete(index=index_name, id=id)
self.assertEqual("deleted", doc_delete_result["result"])
self.assertEqual(index_name, doc_delete_result["_index"])
self.assertEqual(id, doc_delete_result["_id"])

index_delete_result = self.client.indices.delete(index=index_name)
self.assertTrue(index_delete_result["acknowledged"])

def test_indices_analyze(self) -> None:
self.client.indices.analyze(body='{"text": "привет"}')

Expand Down
Loading