Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Async es client factory function #302

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions requirements-test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ mock==4.0.3
port-for==0.4
mirakuru==2.3.0
elasticsearch==7.12.0
asyncio==3.4.3
-e .[tests]
41 changes: 35 additions & 6 deletions src/pytest_elasticsearch/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
import os.path
import shutil
from tempfile import gettempdir

import asyncio
import pytest
from elasticsearch import Elasticsearch
from elasticsearch import Elasticsearch, AsyncElasticsearch
from mirakuru import ProcessExitedWithError

from pytest_elasticsearch.executor import (
Expand All @@ -42,7 +42,7 @@ def return_config(request):
for option in options:
option_name = 'elasticsearch_' + option
conf = request.config.getoption(option_name) or \
request.config.getini(option_name)
request.config.getini(option_name)
fizyk marked this conversation as resolved.
Show resolved Hide resolved
config[option] = conf
return config

Expand Down Expand Up @@ -86,16 +86,16 @@ def elasticsearch_proc_fixture(request):

elasticsearch_port = get_port(port) or get_port(config['port'])
elasticsearch_transport_port = get_port(transport_tcp_port) or \
get_port(config['transport_tcp_port'])
get_port(config['transport_tcp_port'])

elasticsearch_cluster_name = \
cluster_name or config['cluster_name'] or \
'elasticsearch_cluster_{0}'.format(elasticsearch_port)
elasticsearch_logs_prefix = logs_prefix or config['logs_prefix']
elasticsearch_index_store_type = index_store_type or \
config['index_store_type']
config['index_store_type']
elasticsearch_network_publish_host = network_publish_host or \
config['network_publish_host']
config['network_publish_host']

logsdir = elasticsearch_logsdir or config['logsdir']
logs_path = os.path.join(
Expand Down Expand Up @@ -190,3 +190,32 @@ def drop_indexes():
return client

return elasticsearch_fixture


def async_elasticsearch(process_fixture_name):
fizyk marked this conversation as resolved.
Show resolved Hide resolved
"""
Create Elasticsearch client fixture.

:param str process_fixture_name: elasticsearch process fixture name
"""
@pytest.fixture
async def elasticsearch_fixture(request):
"""Elasticsearch client fixture."""
process = request.getfixturevalue(process_fixture_name)
if not process.running():
process.start()

client = AsyncElasticsearch([{'host': process.host, 'port': process.port}])

async def drop_indexes():
await client.indices.delete(index='*')
await client.close()

def sync_drop_indexes():
asyncio.get_event_loop().run_until_complete(drop_indexes())

request.addfinalizer(sync_drop_indexes)

return client

return elasticsearch_fixture