-
Notifications
You must be signed in to change notification settings - Fork 66
/
__init__.py
34 lines (25 loc) · 898 Bytes
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# -*- coding: utf-8 -*-
from functools import wraps
from elasticsearch.client import _normalize_hosts
from unittest.mock import patch
from elasticmock.fake_elasticsearch import FakeElasticsearch
ELASTIC_INSTANCES = {}
def _get_elasticmock(hosts=None, *args, **kwargs):
host = _normalize_hosts(hosts)[0]
elastic_key = '{0}:{1}'.format(
host.get('host', 'localhost'), host.get('port', 9200)
)
if elastic_key in ELASTIC_INSTANCES:
connection = ELASTIC_INSTANCES.get(elastic_key)
else:
connection = FakeElasticsearch()
ELASTIC_INSTANCES[elastic_key] = connection
return connection
def elasticmock(f):
@wraps(f)
def decorated(*args, **kwargs):
ELASTIC_INSTANCES.clear()
with patch('elasticsearch.Elasticsearch', _get_elasticmock):
result = f(*args, **kwargs)
return result
return decorated