Skip to main content

Python Elasticsearch Mock for test purposes with new features

Project description

ElasticMockNew

Python Elasticsearch Mock for test purposes

Build Status Coverage Status PyPI version GitHub license PyPI - Python Version ElasticSearch Version

Libraries.io dependency status for latest release Downloads

Installation

pip install ElasticMockNew

Usage

To use ElasticMockNew, decorate your test method with @elasticmock decorator:

from unittest import TestCase

from elasticmock import elasticmock


class TestClass(TestCase):

    @elasticmock
    def test_should_return_something_from_elasticsearch(self):
        self.assertIsNotNone(some_function_that_uses_elasticsearch())

Custom Behaviours

You can also force the behaviour of the ElasticSearch instance by importing the elasticmock.behaviour module:

from unittest import TestCase

from elasticmock 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)

Available Behaviours

  • server_failure: Will make all calls to ElasticSearch returns the following error message:
    {
        'status_code': 500,
        'error': 'Internal Server Error'
    }
    

Code example

Let's say you have a prod code snippet like this one:

import elasticsearch

class FooService:

    def __init__(self):
        self.es = elasticsearch.Elasticsearch(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')

Than you should be able to test this class by mocking ElasticSearch using the following test class:

from unittest import TestCase
from elasticmock import elasticmock
from foo.bar import FooService

class FooServiceTest(TestCase):

    @elasticmock
    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 ElasticSearch
        id = service.create(index, expected_document)
        self.assertIsNotNone(id)

        # Retrive dpcument from ElasticSearch
        document = service.read(index, id)
        self.assertEquals(expected_document, document)

Notes:

  • The mocked search method returns all available documents indexed on the index with the requested document type.
  • The mocked suggest method returns the exactly suggestions dictionary passed as body serialized in Elasticsearch.suggest response. Atention: If the term is an int, the suggestion will be python term + 1. If not, the suggestion will be formatted as python {0}_suggestion.format(term) . Example:
    • Suggestion Body:
     suggestion_body = {
         'suggestion-string': {
             'text': 'test_text',
             'term': {
                 'field': 'string'
             }
         },
         'suggestion-id': {
             'text': 1234567,
             'term': {
                 'field': 'id'
             }
         }
     }
    
    • Suggestion Response:
    {
        '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
            }
        ],
    }
    

Testing

python setup.py test

Changelog

1.8.1:

  • Add must not to query type
  • Add sort support

1.8.0:

1.7.0:

1.6.2:

1.6.1:

  • Fix Twine README.md

1.6.0:

1.5.1:

1.5.0:

1.4.0

1.3.7

1.3.6

1.3.5

1.3.4

1.3.3

1.3.2

1.3.1

  • elasticmock: Allow the same arguments to the mock that elasticsearch.Elasticsearch allows (Thanks @mattbreeden)

1.3.0:

1.2.0:

  • FakeElasticSearch: Mocked suggest method

1.1.1:

  • elasticmock: Changing the cleanup older FakeElasticSearch's instances order
  • FakeElasticSearch.index: Changing the method signature to correctly overrides the Elasticsearch.index method

1.1.0:

  • FakeElasticSearch: Mocked delete method

1.0.1:

  • setup.py: Fixed GitHub link

1.0.0:

  • elasticmock: Created @elasticmock decorator
  • FakeElasticSearch: Mocked exists, get, get_source, index, info, search and ping method

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ElasticMockNew-1.8.1.tar.gz (20.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ElasticMockNew-1.8.1-py3-none-any.whl (28.9 kB view details)

Uploaded Python 3

File details

Details for the file ElasticMockNew-1.8.1.tar.gz.

File metadata

  • Download URL: ElasticMockNew-1.8.1.tar.gz
  • Upload date:
  • Size: 20.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.4.2 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.8.0 tqdm/4.30.0 CPython/3.8.2

File hashes

Hashes for ElasticMockNew-1.8.1.tar.gz
Algorithm Hash digest
SHA256 7c4e696b2a32306ac256b4cffc818dc03de3436b61de3d0429363ce90f319bd9
MD5 c7dd544897672560b6d6b21d5e94eba7
BLAKE2b-256 e95a40b0ccd6af4636decd1cef3eea2ca56f2cc3cf60d3936a284b099cfe84b4

See more details on using hashes here.

File details

Details for the file ElasticMockNew-1.8.1-py3-none-any.whl.

File metadata

  • Download URL: ElasticMockNew-1.8.1-py3-none-any.whl
  • Upload date:
  • Size: 28.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.4.2 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.8.0 tqdm/4.30.0 CPython/3.8.2

File hashes

Hashes for ElasticMockNew-1.8.1-py3-none-any.whl
Algorithm Hash digest
SHA256 4b1917fc7a8ddb3aaf5794c47d30f4b60e9ac54310b84f5c83cc74aa6453ae9c
MD5 8fe9743da90d53d8b4f71d752a2f301a
BLAKE2b-256 3efbc48d4582727ea958562b613a8f4cc2b8d8ef07a0177e72a4de1a9112e28b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page