Running and writing tests

Running the tests

You can run the tests with:

./run_tests.py

This will run all the tests.

Note

If you need to adjust the settings, copy test_settings.py to a new file (like test_settings_local.py), edit the file, and specify that as the value for the environment variable DJANGO_SETTINGS_MODULE.

DJANGO_SETTINGS_MODULE=test_settings_local ./run_tests.py

This is helpful if you need to change the value of ES_HOSTS to match the ip address or port that elasticsearch is listening on.

Writing tests

Tests are located in elasticutils/tests/.

We use nose for test utilities and running tests.

ElasticTestCase

If you’re testing things in ElasticUtils that require hitting an Elasticsearch cluster, then you should subclass elasticutils.tests.ESTestCase which has code in it for making things easier.

class elasticutils.tests.ESTestCase(methodName='runTest')

Superclass for Elasticsearch-using test cases.

Property es_settings:
 settings to use to build a elasticsearch Elasticsearch object
Property index_name:
 Name of the index to use for theses tests
Property mapping_type_name:
 The mapping type name for the mapping you want created
Property mapping:
 The mapping to use when creating the index
Property data:Any documents to index during setup_class()

For examples of usage, see tests/test_*.py files.

You probably want to subclass this and at least set relevant class properties. Then use that subclass as the superclass for your tests.

classmethod cleanup_index()

Cleans up the index

This deletes the index named by cls.index_name.

classmethod create_index(settings=None)

Creates an index with specified settings

Uses cls.index_name as the index to create.

Parameters:settings – Any additional settings to use to create the index.
classmethod get_es()

Returns the Elasticsearch object specified by cls.es_settings

classmethod get_s(mapping_type=None)

Returns an S for the settings on this class

Uses cls.es_settings to configure the Elasticsearch object. Uses cls.index_name for the index and cls.mapping_type_name for the MappingType to search.

Parameters:mapping_type – The MappingType class to use to create the S
classmethod index_data(documents, id_field='id')

Indexes specified data

Uses cls.index_name as the index to index into. Uses cls.mapping_type_name as the doctype to index these documents as.

Parameters:
  • documents – List of documents as Python dicts
  • id_field – The field of the document that represents the id
classmethod refresh()

Refresh index after indexing

This refreshes the index specified by cls.index_name.

classmethod setup_class()

Sets up the index specified by cls.index_name

This will create the index named cls.index_name with the mapping specified in cls.mapping and indexes any data specified in cls.data.

If you need something different, then override this.

classmethod teardown_class()

Removes the index specified by cls.index_name

This should clean up anything created in cls.setup_class() and anything created by the tests.