def test_es_document(self, include_index, include_source): """Test that es_document() creates a dict with the expected keys and values.""" obj = SimpleModel(id=5, name='test-name') doc = ESSimpleModel.es_document( obj, include_index=include_index, include_source=include_source, ) source = { '_document_type': 'simplemodel', 'id': obj.pk, 'name': 'test-name', 'date': None, } expected_doc = { '_id': obj.pk, '_type': DEFAULT_MAPPING_TYPE, **({ '_index': ESSimpleModel.get_write_alias() } if include_index else {}), **({ '_source': source } if include_source else {}), } assert doc == expected_doc
def test_collector(monkeypatch, setup_es): """ Test that the collector collects and deletes all the django objects deleted. """ obj = SimpleModel.objects.create() sync_object(SimpleModelSearchApp, str(obj.pk)) setup_es.indices.refresh() es_doc = ESSimpleModel.es_document(obj, include_index=False, include_source=False) assert SimpleModel.objects.count() == 1 collector = Collector() # check that the post/pre_delete callbacks of SimpleModel are in the collected # signal receivers to disable simplemodel_receivers = [ receiver for receiver in collector.signal_receivers_to_disable if receiver.sender is SimpleModel ] assert simplemodel_receivers assert {receiver.signal for receiver in simplemodel_receivers} == {post_delete, pre_delete} # mock the receiver methods so that we can check they are called for receiver in collector.signal_receivers_to_disable: monkeypatch.setattr(receiver, 'connect', mock.Mock()) monkeypatch.setattr(receiver, 'disconnect', mock.Mock()) collector.connect() # check that the existing signal receivers are disconnected for receiver in collector.signal_receivers_to_disable: assert receiver.disconnect.called assert not receiver.connect.called obj.delete() collector.disconnect() # check that the existing signal receivers are connected back for receiver in collector.signal_receivers_to_disable: assert receiver.connect.called assert collector.deletions == { SimpleModel: [es_doc], } read_alias = ESSimpleModel.get_read_alias() assert SimpleModel.objects.count() == 0 assert setup_es.count(read_alias, doc_type=SimpleModelSearchApp.name)['count'] == 1 collector.delete_from_es() setup_es.indices.refresh() assert setup_es.count(read_alias, doc_type=SimpleModelSearchApp.name)['count'] == 0
def test_update_es_after_deletions(setup_es): """ Test that the context manager update_es_after_deletions collects and deletes all the django objects deleted. """ obj = SimpleModel.objects.create() sync_object(SimpleModelSearchApp, str(obj.pk)) setup_es.indices.refresh() read_alias = ESSimpleModel.get_read_alias() assert SimpleModel.objects.count() == 1 assert setup_es.count(read_alias, doc_type=SimpleModelSearchApp.name)['count'] == 1 with update_es_after_deletions(): obj.delete() setup_es.indices.refresh() assert setup_es.count(read_alias, doc_type=SimpleModelSearchApp.name)['count'] == 0