Product
Introducing License Enforcement in Socket
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
Mock/fake of opensearch library, allows you to mock opensearch-py
Fork of Python Elasticsearch(TM) Mock. Sometimes the developers who work with elasticsearch (TM), don't really have any input in choice of host and need to get work done.
pip install openmock
To use Openmock, decorate your test method with @openmock decorator:
from unittest import TestCase
from openmock import openmock
class TestClass(TestCase):
@openmock
def test_should_return_something_from_opensearch(self):
self.assertIsNotNone(some_function_that_uses_opensearch())
You can also force the behaviour of the OpenSearch instance by importing the openmock.behaviour
module:
from unittest import TestCase
from openmock import behaviour
class TestClass(TestCase):
...
def test_should_return_internal_server_error_when_simulate_server_error_is_true(self):
behaviour.server_failure.enable()
...
behaviour.server_failure.disable()
You can also disable all behaviours by calling behaviour.disable_all()
(Consider put this in your def tearDown(self)
method)
server_failure
: Will make all calls to OpenSearch returns the following error message:
{
'status_code': 500,
'error': 'Internal Server Error'
}
Let's say you have a prod code snippet like this one:
import opensearchpy
class FooService:
def __init__(self):
self.es = opensearchpy.OpenSearch(hosts=[{'host': 'localhost', 'port': 9200}])
def create(self, index, body):
es_object = self.es.index(index, body)
return es_object.get('_id')
def read(self, index, id):
es_object = self.es.get(index, id)
return es_object.get('_source')
Then you should be able to test this class by mocking OpenSearch using the following test class:
from unittest import TestCase
from openmock import openmock
from foo.bar import FooService
class FooServiceTest(TestCase):
@openmock
def should_create_and_read_object(self):
# Variables used to test
index = 'test-index'
expected_document = {
'foo': 'bar'
}
# Instantiate service
service = FooService()
# Index document on OpenSearch
id = service.create(index, expected_document)
self.assertIsNotNone(id)
# Retrieve document from OpenSearch
document = service.read(index, id)
self.assertEquals(expected_document, document)
python term + 1
. If not, the suggestion will be formatted as python {0}_suggestion.format(term)
.
Example:
suggestion_body = {
'suggestion-string': {
'text': 'test_text',
'term': {
'field': 'string'
}
},
'suggestion-id': {
'text': 1234567,
'term': {
'field': 'id'
}
}
}
{
'suggestion-string': [
{
'text': 'test_text',
'length': 1,
'options': [
{
'text': 'test_text_suggestion',
'freq': 1,
'score': 1.0
}
],
'offset': 0
}
],
'suggestion-id': [
{
'text': 1234567,
'length': 1,
'options': [
{
'text': 1234568,
'freq': 1,
'score': 1.0
}
],
'offset': 0
}
],
}
Preferred for testing one version of python.
pytest test
Won't catch pytest tests.
python -m unittest
We are trying to support a full matrix of openmock versions and python versions 3.9+. This is slow.
tox
See CHANGELOG.md
MIT with normalize_host.py
being Apache 2 from Elasticsearch.
FAQs
Python OpenSearch Mock for test purposes
We found that openmock demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
Product
We're launching a new set of license analysis and compliance features for analyzing, managing, and complying with licenses across a range of supported languages and ecosystems.
Product
We're excited to introduce Socket Optimize, a powerful CLI command to secure open source dependencies with tested, optimized package overrides.