Skip to content

Commit

Permalink
Lifecycle integration tests.
Browse files Browse the repository at this point in the history
Signed-off-by: dblock <[email protected]>
  • Loading branch information
dblock committed Nov 27, 2024
1 parent bf9add4 commit 0716eda
Show file tree
Hide file tree
Showing 7 changed files with 238 additions and 5 deletions.
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()
47 changes: 47 additions & 0 deletions test_opensearchpy/test_async/test_server/test_clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,53 @@


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
47 changes: 47 additions & 0 deletions test_opensearchpy/test_server/test_clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,53 @@


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

0 comments on commit 0716eda

Please sign in to comment.