Skip to main content

Elasticsearch Fixtures

This tool is heavily inspired by klen's Mixer.

This tool is optimized to be used with Django and pytest.

It allows you to add documents to a test-index in yout Elasticsearch instance when you run your unittests.

With elasticsearch_fixtures you can do something like this:

from elasticsearch_fixtures.es_mixer import ESMixer

es_mixer = ESMixer(host='http://localhost:9200/')

def test_something():
  doc1 = es_mixer.blend('indexname', id=1, title='test')
  assert doc1['source']['title'] == 'test'

Note: If you provide an id and a document with that id already exists, that document will not be updated, but fully replaced.

You can also create documents without providing an id. Elasticsearch will then auto-create an id:

def test_something():
  doc1 = es_mixer.blend('indexname', title='test')
  print(doc1['id'])

And you can update an existing document that is already in the index:

def test_something():
  es_mixer.blend('indexname', id=1, title='test')
  doc1 = es_mixer.update('indexname', id=1, title='new title')
  assert doc1['source']['title'] == 'new_title'

Big word of warning: We are no ES experts. We have only started using ES around June 2019 and one of the first problems that we had was that we did not know how to properly write unittests for our Django views and Graphene resolver functions. There are probably much better ways to do this. If you know one, we would love to hear about it (just open an issue and tell us about it!).

Installation

pip install elasticsearch-fixtures

Configuration

In order to make sure that your tests wipe the index at the very beginning and also after each test, create the following conftest.py:

"""Global settings for pytest."""
import pytest

from django.conf import settings

from elasticsearch_fixtures.es_mixer import ESMixer

es_mixer = ESMixer(host=settings.ELASTICSEARCH_HOST)
index = settings.ELASTICSEARCH_INDEX


@pytest.fixture(scope='session', autouse=True)
def setup_elasticsearch():
    es_mixer.wipe_index(index)


@pytest.fixture(autouse=True)
def cleanup_elasticsearch():
    yield
    es_mixer.wipe_index(index)

As you can see, in this example we also use a Django setting called ELASTICSEARCH_HOST (with a trailing slash) and ELASTICSEARCH_INDEX. Of course you can name your own settings however you like.

Metadata

Release files for elasticsearch-fixtures 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for elasticsearch-fixtures 0.1.1
File Size Uploaded
elasticsearch-fixtures-0.1.1.tar.gz 4.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for elasticsearch-fixtures 0.1.1
File Interpreter ABI Platform
elasticsearch_fixtures-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 8.7 kB

Release files / elasticsearch-fixtures-0.1.1.tar.gz

Download URL elasticsearch-fixtures-0.1.1.tar.gz
Size 4.1 kB
Tags Source
SHA-256 checksum
How to use checksums
85ed14d3c725b7fbd8b655425ffbefe179dc5f761d0513ea17fe4b5e3d80a45f
BLAKE2b-256 checksum
How to use checksums
c823faf424f9aed12e80f40f52a7e3af896629d1480578cf56151f25a561ce35
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/39.2.0 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.6.5

Release files / elasticsearch_fixtures-0.1.1-py3-none-any.whl

Download URL elasticsearch_fixtures-0.1.1-py3-none-any.whl
Size 4.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
159e6959c42812ffcb2b3918e0b6c28d905f564395c98d15ccd6f77178e9edee
BLAKE2b-256 checksum
How to use checksums
eeefe5a4ce2b670f7fd2f839b3c2a59d7a01bdfcfebe73a70c313d2fd2fb58c5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/39.2.0 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.6.5

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page