{"id":15442902,"url":"https://github.com/vrcmarcos/elasticmock","last_synced_at":"2025-04-04T13:12:27.173Z","repository":{"id":10295316,"uuid":"65153568","full_name":"vrcmarcos/elasticmock","owner":"vrcmarcos","description":"Python Elasticsearch Mock for test purposes","archived":false,"fork":false,"pushed_at":"2024-06-13T17:23:49.000Z","size":159,"stargazers_count":112,"open_issues_count":24,"forks_count":66,"subscribers_count":8,"default_branch":"master","last_synced_at":"2025-03-28T12:08:07.408Z","etag":null,"topics":["decorators","elasticsearch","mock","python","python2","python3"],"latest_commit_sha":null,"homepage":"https://pypi.python.org/pypi/ElasticMock","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/vrcmarcos.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-08-07T20:52:07.000Z","updated_at":"2024-11-28T16:32:50.000Z","dependencies_parsed_at":"2024-06-18T21:27:49.592Z","dependency_job_id":"2b70d69e-003d-4c6c-b84c-fd9afccae0aa","html_url":"https://github.com/vrcmarcos/elasticmock","commit_stats":{"total_commits":154,"total_committers":33,"mean_commits":4.666666666666667,"dds":0.6233766233766234,"last_synced_commit":"0e327eb933a669cc626699bfb60ab14384958dfd"},"previous_names":[],"tags_count":21,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vrcmarcos%2Felasticmock","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vrcmarcos%2Felasticmock/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vrcmarcos%2Felasticmock/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vrcmarcos%2Felasticmock/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/vrcmarcos","download_url":"https://codeload.github.com/vrcmarcos/elasticmock/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247182367,"owners_count":20897380,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["decorators","elasticsearch","mock","python","python2","python3"],"created_at":"2024-10-01T19:31:24.846Z","updated_at":"2025-04-04T13:12:27.157Z","avatar_url":"https://github.com/vrcmarcos.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ElasticMock\n\nPython Elasticsearch Mock for test purposes\n\n[![Build Status](https://travis-ci.org/vrcmarcos/elasticmock.svg?branch=master)](https://travis-ci.org/vrcmarcos/elasticmock) [![Coverage Status](https://coveralls.io/repos/github/vrcmarcos/elasticmock/badge.svg?branch=master)](https://coveralls.io/github/vrcmarcos/elasticmock?branch=master) [![PyPI version](https://badge.fury.io/py/ElasticMock.svg)](https://badge.fury.io/py/ElasticMock) [![GitHub license](https://img.shields.io/github/license/vrcmarcos/elasticmock)](https://github.com/vrcmarcos/elasticmock/blob/master/LICENSE) ![PyPI - Python Version](https://img.shields.io/pypi/pyversions/ElasticMock) ![ElasticSearch Version](https://img.shields.io/badge/elasticsearch-1%20%7C%202%20%7C%205%20%7C%206%20%7C%207-blue) \n\n![Libraries.io dependency status for latest release](https://img.shields.io/librariesio/release/pypi/elasticmock) [![Downloads](https://pepy.tech/badge/elasticmock/month)](https://pepy.tech/project/elasticmock/month)\n\n## Installation\n\n```shell\npip install ElasticMock\n```\n\n## Usage\n\nTo use ElasticMock, decorate your test method with **@elasticmock** decorator:\n\n```python\nfrom unittest import TestCase\n\nfrom elasticmock import elasticmock\n\n\nclass TestClass(TestCase):\n\n    @elasticmock\n    def test_should_return_something_from_elasticsearch(self):\n        self.assertIsNotNone(some_function_that_uses_elasticsearch())\n```\n\n### Custom Behaviours\n\nYou can also force the behaviour of the ElasticSearch instance by importing the `elasticmock.behaviour` module:\n\n```python\nfrom unittest import TestCase\n\nfrom elasticmock import behaviour\n\n\nclass TestClass(TestCase):\n\n    ...\n\n    def test_should_return_internal_server_error_when_simulate_server_error_is_true(self):\n        behaviour.server_failure.enable()\n        ...\n        behaviour.server_failure.disable()\n```\n\nYou can also disable all behaviours by calling `behaviour.disable_all()` (Consider put this in your `def tearDown(self)` method)\n\n#### Available Behaviours\n\n* `server_failure`: Will make all calls to ElasticSearch returns the following error message:\n    ```python\n    {\n        'status_code': 500,\n        'error': 'Internal Server Error'\n    }\n    ```\n\n## Code example\n\nLet's say you have a prod code snippet like this one:\n\n```python\nimport elasticsearch\n\nclass FooService:\n\n    def __init__(self):\n        self.es = elasticsearch.Elasticsearch(hosts=[{'host': 'localhost', 'port': 9200}])\n\n    def create(self, index, body):\n        es_object = self.es.index(index, body)\n        return es_object.get('_id')\n\n    def read(self, index, id):\n        es_object = self.es.get(index, id)\n        return es_object.get('_source')\n\n```\n\nThan you should be able to test this class by mocking ElasticSearch using the following test class:\n\n```python\nfrom unittest import TestCase\nfrom elasticmock import elasticmock\nfrom foo.bar import FooService\n\nclass FooServiceTest(TestCase):\n\n    @elasticmock\n    def should_create_and_read_object(self):\n        # Variables used to test\n        index = 'test-index'\n        expected_document = {\n            'foo': 'bar'\n        }\n\n        # Instantiate service\n        service = FooService()\n\n        # Index document on ElasticSearch\n        id = service.create(index, expected_document)\n        self.assertIsNotNone(id)\n\n        # Retrive dpcument from ElasticSearch\n        document = service.read(index, id)\n        self.assertEquals(expected_document, document)\n\n```\n\n## Notes:\n\n- The mocked **search** method returns **all available documents** indexed on the index with the requested document type.\n- 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) ```.\nExample:\n\t- **Suggestion Body**:\n\t```python\n    suggestion_body = {\n        'suggestion-string': {\n            'text': 'test_text',\n            'term': {\n                'field': 'string'\n            }\n        },\n        'suggestion-id': {\n            'text': 1234567,\n            'term': {\n                'field': 'id'\n            }\n        }\n    }\n    ```\n    - **Suggestion Response**:\n    ```python\n    {\n        'suggestion-string': [\n            {\n                'text': 'test_text',\n                'length': 1,\n                'options': [\n                    {\n                        'text': 'test_text_suggestion',\n                        'freq': 1,\n                        'score': 1.0\n                    }\n                ],\n                'offset': 0\n            }\n        ],\n        'suggestion-id': [\n            {\n                'text': 1234567,\n                'length': 1,\n                'options': [\n                    {\n                        'text': 1234568,\n                        'freq': 1,\n                        'score': 1.0\n                    }\n                ],\n                'offset': 0\n            }\n        ],\n    }\n    ```\n\n## Testing\n\n```bash\npython setup.py test\n```\n\n## Changelog\n\n#### 1.8.1:\n- [Add support for Python 3.9](https://github.com/vrcmarcos/elasticmock/pull/72) (Thanks [@singingwolfboy](https://github.com/singingwolfboy))\n- [use unittest.mock instead of mock](https://github.com/vrcmarcos/elasticmock/pull/71) (Thanks [@singingwolfboy](https://github.com/singingwolfboy))\n- [Add must_not for bool search query](https://github.com/vrcmarcos/elasticmock/pull/70) (Thanks [@t-bittarn](https://github.com/t-bittarn))\n\n\n#### 1.8.0:\n- [Add multi_match](https://github.com/vrcmarcos/elasticmock/pull/63) (Thanks [@carlosgalvez-tiendeo](https://github.com/carlosgalvez-tiendeo))\n- [Add mget](https://github.com/vrcmarcos/elasticmock/pull/64) (Thanks [@carlosgalvez-tiendeo](https://github.com/carlosgalvez-tiendeo))\n- [Add create, update, and delete to bulk API](https://github.com/vrcmarcos/elasticmock/pull/65) (Thanks [@fenimore](https://github.com/fenimore))\n- [Add Should to bool Query](https://github.com/vrcmarcos/elasticmock/pull/67) (Thanks [@fenimore](https://github.com/fenimore))\n- [Update Search API return result](https://github.com/vrcmarcos/elasticmock/pull/68) (Thanks [@fenimore](https://github.com/fenimore))\n\n#### 1.7.0:\n- [Add shards skipped to search and count](https://github.com/vrcmarcos/elasticmock/pull/56) (Thanks [@philtweir](https://github.com/philtweir))\n- [Allow 'match_all' queries in FakeSearch](https://github.com/vrcmarcos/elasticmock/pull/54) (Thanks [@jankislinger](https://github.com/jankislinger))\n- [Query using nested attributes](https://github.com/vrcmarcos/elasticmock/pull/55) (Thanks [@jankislinger](https://github.com/jankislinger))\n- [New features: range, size, aggregations](https://github.com/vrcmarcos/elasticmock/pull/57) (Thanks [@jankislinger](https://github.com/jankislinger))\n- [Adding \"should\" and \"minimum_should_match\" to QueryType](https://github.com/vrcmarcos/elasticmock/pull/62) (Thanks [@lunarie16](https://github.com/lunarie16))\n\n#### 1.6.2:\n- [Add must to query type](https://github.com/vrcmarcos/elasticmock/pull/47) (Thanks [@cuent](https://github.com/cuent))\n- [Add match all query type](https://github.com/vrcmarcos/elasticmock/pull/48) (Thanks [@cuent](https://github.com/cuent))\n\n#### 1.6.1:\n- Fix Twine README.md\n\n#### 1.6.0:\n- [Implements several basic search types](https://github.com/vrcmarcos/elasticmock/pull/42) (Thanks [@KyKoPho](https://github.com/KyKoPho))\n- [Allow ignoring of missing documents (404) for get and delete](https://github.com/vrcmarcos/elasticmock/pull/44) (Thanks [@joosterman](https://github.com/joosterman))\n\n#### 1.5.1:\n- [Fix tests for es \u003e 7](https://github.com/vrcmarcos/elasticmock/pull/38) (Thanks [@chesstrian](https://github.com/chesstrian))\n\n#### 1.5.0:\n- [**FakeElasticSearch**: Mocked **indices** property](https://github.com/vrcmarcos/elasticmock/issues/22)\n  - **FakeIndicesClient**: Mocked **create**, **exists**, **refresh** and **delete** methods\n- [**FakeElasticSearch**: Mocked **cluster** property](https://github.com/vrcmarcos/elasticmock/issues/8)\n  - **FakeClusterClient**: Mocked **health** method\n\n#### 1.4.0\n\n- [Fix es.index regression issue](https://github.com/vrcmarcos/elasticmock/issues/34)\n- [Add 'Force Server Failure' feature as requested](https://github.com/vrcmarcos/elasticmock/issues/28)\n- Reformat code to be compliant with PEP8\n- Add support to Python 3.8\n\n#### 1.3.7\n\n- [Adding fix for updating existing doc using index](https://github.com/vrcmarcos/elasticmock/pull/32) (Thanks [@adityaghosh](https://github.com/adityaghosh))\n- [Added bulk method](https://github.com/vrcmarcos/elasticmock/pull/30) (Thanks [@charl-van-niekerk](https://github.com/charl-van-niekerk))\n- [Add default value to doc_type in index method as it is by default set to '\\_doc'](https://github.com/vrcmarcos/elasticmock/pull/27) (Thanks [@mohantyashish109](https://github.com/mohantyashish109))\n- [Add support for Python 3.7](https://github.com/vrcmarcos/elasticmock/pull/25) (Thanks [@asherf](https://github.com/asherf))\n\n#### 1.3.6\n\n- [Fix installation issue](https://github.com/vrcmarcos/elasticmock/pull/20) (Thanks [@tdhopper](https://github.com/tdhopper))\n\n#### 1.3.5\n\n- [Fix 1.3.4 release](https://github.com/vrcmarcos/elasticmock/pull/19) (Thanks [@infinite-Joy](https://github.com/infinite-Joy))\n\n#### 1.3.4\n\n- [Added aggregations to response if requested](https://github.com/vrcmarcos/elasticmock/pull/15) (Thanks [@snakeye](https://github.com/snakeye))\n- [Implementing new methods for scrolling](https://github.com/vrcmarcos/elasticmock/pull/17) (Thanks [@tcatrain](https://github.com/tcatrain))\n\n#### 1.3.3\n\n- [Search: doc_type can be a list](https://github.com/vrcmarcos/elasticmock/pull/16) (Thanks [@garncarz](https://github.com/garncarz))\n- [Exclude tests package](https://github.com/vrcmarcos/elasticmock/pull/13) (Thanks [@jmlw](https://github.com/jmlw))\n- [Make the FakeElasticsearch __init__ signature match the one from Elasticsearch](https://github.com/vrcmarcos/elasticmock/pull/10) (Thanks [@xrmx](https://github.com/xrmx))\n- [Improve search and count](https://github.com/vrcmarcos/elasticmock/pull/7) (Thanks [@frivoire](https://github.com/frivoire))\n\n#### 1.3.2\n\n- **elasticmock**: Python 3 fixes (Thanks [@barseghyanartur](https://github.com/barseghyanartur))\n- **test**: Add information on testing (Thanks [@barseghyanartur](https://github.com/barseghyanartur))\n- **README.md**: Fixed typo (Thanks [@bowlofstew](https://github.com/bowlofstew))\n\n#### 1.3.1\n\n- **elasticmock**: Allow the same arguments to the mock that elasticsearch.Elasticsearch allows (Thanks [@mattbreeden](https://github.com/mattbreeden))\n\n#### 1.3.0:\n- **FakeElasticSearch**: Mocked **count** method (Thanks [@TheoResources](https://github.com/TheoResources))\n\n#### 1.2.0:\n- **FakeElasticSearch**: Mocked **suggest** method\n\n#### 1.1.1:\n- **elasticmock**: Changing the cleanup older FakeElasticSearch's instances order\n- **FakeElasticSearch.index**: Changing the method signature to correctly overrides the Elasticsearch.index method\n\n#### 1.1.0:\n- **FakeElasticSearch**: Mocked **delete** method\n\n#### 1.0.1:\n- **setup.py**: Fixed GitHub link\n\n#### 1.0.0:\n- **elasticmock**: Created **@elasticmock** decorator\n- **FakeElasticSearch**: Mocked **exists**, **get**, **get_source**, **index**, **info**, **search** and **ping** method\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvrcmarcos%2Felasticmock","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fvrcmarcos%2Felasticmock","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvrcmarcos%2Felasticmock/lists"}