Skip to content

Elasticmock does not mock the Elasticsearch client #82

@wpm

Description

@wpm

I have written the following unit test.

from unittest import TestCase

from elasticmock import elasticmock
from elasticsearch import Elasticsearch


def some_function_that_uses_elasticsearch():
    client = Elasticsearch(hosts=[{"host": "localhost", "port": 9200}])
    id = client.index("test-index", {"a": "b"})
    print(id)
    return True


class TestClass(TestCase):
    @elasticmock
    def test_should_return_something_from_elasticsearch(self):
        self.assertIsNotNone(some_function_that_uses_elasticsearch())

If I run the test without a local instance of Elasticsearch running I get the following error.

elasticsearch.exceptions.ConnectionError: ConnectionError(<urllib3.connection.HTTPConnection object at 0x7ff720769370>: 
Failed to establish a new connection: [Errno 61] Connection refused) caused by: 
NewConnectionError(<urllib3.connection.HTTPConnection object at 0x7ff720769370>:
Failed to establish a new connection: 
[Errno 61] Connection refused)

If I step through in the debugger I see that the client is an ElasticSearch object and not a FakeElasticSearch object. Apparently the @elasticmock decorator has no effect.

I have the same problem with the pytest framework.

from elasticmock import elasticmock
from elasticsearch import Elasticsearch


@elasticmock
def test_mocked_elasticsearch_client():
    client = Elasticsearch(hosts=[{"host": "localhost", "port": 9200}])
    id = client.index("test-index", {"a": "b"})
    print(id)

The "Code Example" with FooService on the project homepage does work. But I can't figure out what is wrong with the variants I have here.

This is elasticmock 1.8.1 and elasticsearch 7.13.3 on OS X with Python 3.9.15.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions