def test_type_on_model(request, cls, value, loaded_val, es_val): indices = prepare_es_for_model(cls) try: instance = cls.create(value=value) print("class", cls, cls.get_index()) assert instance.id is not None # cls.refresh() es = elastic_connect.get_es() es_result = es.get(index=cls.get_index(), doc_type=cls.get_doctype(), id=instance.id) print("es: ", es_result) loaded = cls.get(instance.id) print("load: ", loaded.value) if loaded_val is None: assert value == loaded.value else: assert loaded_val == loaded.value if es_val is None: assert value == es_result['_source']['value'] else: assert es_val == es_result['_source']['value'] except Exception as e: raise e finally: clean_es_for_model(indices, cls, request)
def prepare_es_for_model(cls): es = elastic_connect.get_es() indices = elastic_connect.create_mappings(model_classes=[cls]) assert es.indices.exists(index=cls.get_index()) print("before", cls.get_index()) return indices
def test_delete_indices(request): es = elastic_connect.get_es() indices = elastic_connect.create_mappings(model_classes=[One, Two]) assert es.indices.exists(index=request.config.getoption("--es-prefix") + '_' + 'model_one') elastic_connect.delete_indices(indices=indices) assert not es.indices.exists( index=request.config.getoption("--es-prefix") + '_' + 'model_one')
def clean_es_for_model(indices, cls, request): print("after") if request.config.getoption("--index-noclean"): print("** not cleaning") return elastic_connect.delete_indices(indices=indices) es = elastic_connect.get_es() assert not es.indices.exists(index=cls.get_index())
def fix_template(request): es = elastic_connect.get_es() template = { "template": "*", "settings": { "number_of_shards": 1, "number_of_replicas": 1 } } es.indices.put_template(name="all", body=template, order=1) logger.info("templates %s", es.indices.get_template(name='*'))
def mappings_one_two(request): es = elastic_connect.get_es() indices = elastic_connect.create_mappings(model_classes=[One, Two]) assert len(indices) == 2 yield if request.config.getoption("--index-noclean"): print("** not cleaning") else: elastic_connect.delete_indices(indices=indices) assert not es.indices.exists(index=One.get_index()) assert not es.indices.exists(index=Two.get_index())
def fix_one_many(request): es = elastic_connect.get_es() indices = elastic_connect.create_mappings(model_classes=[One, Many]) assert es.indices.exists(index=One.get_index()) assert es.indices.exists(index=Many.get_index()) yield if request.config.getoption("--index-noclean"): print("** not cleaning") return elastic_connect.delete_indices(indices=indices) assert not es.indices.exists(index=One.get_index()) assert not es.indices.exists(index=Many.get_index())
def test_get(fix_model_one_save): cls = fix_model_one_save es = elastic_connect.get_es() es_result = es.index(index=cls.get_index(), doc_type=cls.get_doctype(), body={'value': 'pokus'}, refresh=True) assert es_result['created'] == True instance = cls.get(es_result['_id']) assert instance.id == es_result['_id'] assert instance.value == 'pokus'
def fix_id_one_many_with_reference(request): es = elastic_connect.get_es() indices = elastic_connect.create_mappings( model_classes=[IdOneWithReference, IdManyWithReference]) assert es.indices.exists(index=IdOneWithReference.get_index()) assert es.indices.exists(index=IdManyWithReference.get_index()) yield if request.config.getoption("--index-noclean"): print("** not cleaning") return elastic_connect.delete_indices(indices=indices) assert not es.indices.exists(index=IdOneWithReference.get_index()) assert not es.indices.exists(index=IdManyWithReference.get_index())
def fix_parent_child(request): es = elastic_connect.get_es() indices = elastic_connect.create_mappings(model_classes=[Parent, Child]) assert es.indices.exists(index=Parent.get_index()) assert es.indices.exists(index=Child.get_index()) yield if request.config.getoption("--index-noclean"): print("** not cleaning") return elastic_connect.delete_indices(indices=indices) assert not es.indices.exists(index=Parent.get_index()) assert not es.indices.exists(index=Child.get_index())
def test_create_with_id(fix_model_one_save): cls = fix_model_one_save instance = cls.create(id='100', value='value1') # type: OneSave assert instance.id == '100' cls.refresh() es = elastic_connect.get_es() es_result = es.get(index=cls.get_index(), doc_type=cls.get_doctype(), id=instance.id) assert es_result['found'] is True assert es_result['_source'] == {'value': 'value1'}
def test_multi_get(fix_model_one_save): cls = fix_model_one_save es = elastic_connect.get_es() es_result1 = es.index(index=cls.get_index(), doc_type=cls.get_doctype(), body={'value': 'pokus'}, refresh=False) es_result2 = es.index(index=cls.get_index(), doc_type=cls.get_doctype(), body={'value': 'pokus2'}, refresh=True) instances = cls.get([es_result1['_id'], es_result2['_id']]) print(instances)
def test_create(fix_model_one_save): cls = fix_model_one_save instance = cls.create(value='value1') # type: Model assert instance.id is not None cls.refresh() es = elastic_connect.get_es() es_result = es.get(index=cls.get_index(), doc_type=cls.get_doctype(), id=instance.id) assert es_result['found'] == True assert es_result['_source'] == {'value': 'value1'}
def fix_user_key(request): es = elastic_connect.get_es() indices = elastic_connect.create_mappings( model_classes=[User, UserNoLoad, Key]) assert es.indices.exists(index=User.get_index()) assert es.indices.exists(index=UserNoLoad.get_index()) assert es.indices.exists(index=Key.get_index()) yield if request.config.getoption("--index-noclean"): print("** not cleaning") return elastic_connect.delete_indices(indices=indices) assert not es.indices.exists(index=User.get_index()) assert not es.indices.exists(index=UserNoLoad.get_index()) assert not es.indices.exists(index=Key.get_index())
def fix_model_one_save(request): class OneSave(Model): __slots__ = ('value', ) _meta = {'_doc_type': 'model_save_one'} _mapping = {'id': Keyword(name='id'), 'value': Keyword(name='value')} es = elastic_connect.get_es() indices = elastic_connect.create_mappings(model_classes=[OneSave]) assert es.indices.exists(index=OneSave.get_index()) yield OneSave if request.config.getoption("--index-noclean"): print("** not cleaning") return elastic_connect.delete_indices(indices=indices) assert not es.indices.exists(index=OneSave.get_index())
def test_create_indices(mappings_one_two): es = elastic_connect.get_es() # explicitly check for proper index and doc_type names expect_one = { 'test_model_one': { 'mappings': { 'model_one': { 'properties': { 'single': { 'type': 'keyword' } } } } } } assert es.indices.get_mapping(One.get_index()) == expect_one expect_two = {'test_model_two': {'mappings': {'model_two': {}}}} assert es.indices.get_mapping(Two.get_index()) == expect_two