All Projects → vrcmarcos → elasticmock

vrcmarcos / elasticmock

Licence: MIT License
Python Elasticsearch Mock for test purposes

Programming Languages

python
139335 projects - #7 most used programming language
Makefile
30231 projects

Projects that are alternatives of or similar to elasticmock

Httpretty
Intercept HTTP requests at the Python socket level. Fakes the whole socket module
Stars: ✭ 1,930 (+1869.39%)
Mutual labels:  mock, decorators
jsxmock
使用 JSX 来定义 Mock Server
Stars: ✭ 31 (-68.37%)
Mutual labels:  mock
emock
🐞 下一代C/C++跨平台mock库 (Next generation cross-platform mock library for C/C++)
Stars: ✭ 73 (-25.51%)
Mutual labels:  mock
Retromock
Java library for mocking responses in a Retrofit service.
Stars: ✭ 48 (-51.02%)
Mutual labels:  mock
jest-launchdarkly-mock
Easily unit test LaunchDarkly feature flagged components with jest
Stars: ✭ 14 (-85.71%)
Mutual labels:  mock
ng-apimock
Node plugin that provides the ability to use scenario based api mocking: for local development for protractor testing
Stars: ✭ 102 (+4.08%)
Mutual labels:  mock
webapi
WAI based library for web api
Stars: ✭ 27 (-72.45%)
Mutual labels:  mock
httpmock
Lightweight HTTP mocking in Go (aka golang)
Stars: ✭ 75 (-23.47%)
Mutual labels:  mock
stub-server
Stub server for REST APIs
Stars: ✭ 14 (-85.71%)
Mutual labels:  mock
polog
Логирование должно быть красивым
Stars: ✭ 26 (-73.47%)
Mutual labels:  decorators
elixir mock
Creates clean, concurrent, inspectable mocks from elixir modules
Stars: ✭ 21 (-78.57%)
Mutual labels:  mock
mountebank-api-php
Working with mountebank api it's easy!
Stars: ✭ 17 (-82.65%)
Mutual labels:  mock
toxic-decorators
Library of Javascript decorators
Stars: ✭ 26 (-73.47%)
Mutual labels:  decorators
mockafka
A testing DSL for kafka-streams
Stars: ✭ 14 (-85.71%)
Mutual labels:  mock
Lol-Mock-API
Mock API Tool
Stars: ✭ 15 (-84.69%)
Mutual labels:  mock
jest-how-do-i-mock-x
Runnable examples for common testing situations that often prove challenging
Stars: ✭ 63 (-35.71%)
Mutual labels:  mock
mswjs.io
Official website and documentation for the Mock Service Worker library.
Stars: ✭ 77 (-21.43%)
Mutual labels:  mock
msw-storybook-addon
Mock API requests in Storybook with Mock Service Worker.
Stars: ✭ 168 (+71.43%)
Mutual labels:  mock
Raccoon
Raccoon is a lightweight response mocking framework that can be easily integrated into the Android UI tests.
Stars: ✭ 47 (-52.04%)
Mutual labels:  mock
kiwi
Built using only nodejs core. Lightweight, intuitive together with great performance and response timing.
Stars: ✭ 45 (-54.08%)
Mutual labels:  decorators

ElasticMock

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 ElasticMock

Usage

To use ElasticMock, 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:

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
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].