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_*.pyfiles.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(**kwargs)¶ Creates an index with specified settings
Uses
cls.index_nameas the index to create.Parameters: kwargs – Any additional args to put in the body like “settings”, “mappings”, etc.
-
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_settingsto configure the Elasticsearch object. Usescls.index_namefor the index andcls.mapping_type_namefor 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_nameas the index to index into. Usescls.mapping_type_nameas 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_nameThis will create the index named
cls.index_namewith the mapping specified incls.mappingand indexes any data specified incls.data.If you need something different, then override this.
-
classmethod
teardown_class()¶ Removes the index specified by
cls.index_nameThis should clean up anything created in
cls.setup_class()and anything created by the tests.
-
classmethod