Beispiel #1
0
def test_collector(monkeypatch, setup_es):
    """
    Test that the collector collects and deletes all the django objects deleted.
    """
    obj = SimpleModel.objects.create()
    sync_object_async(ESSimpleModel, SimpleModel, str(obj.pk))
    setup_es.indices.refresh()

    search_app = get_search_app_by_model(SimpleModel)

    es_doc = ESSimpleModel.es_document(obj)

    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 = search_app.es_model.get_read_alias()

    assert SimpleModel.objects.count() == 0
    assert setup_es.count(read_alias, doc_type=search_app.name)['count'] == 1

    collector.delete_from_es()

    setup_es.indices.refresh()
    assert setup_es.count(read_alias, doc_type=search_app.name)['count'] == 0
Beispiel #2
0
    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
Beispiel #3
0
def test_collector(monkeypatch, es_with_signals):
    """
    Test that the collector collects and deletes all the django objects deleted.
    """
    obj = SimpleModel.objects.create()
    sync_object(SimpleModelSearchApp, str(obj.pk))
    es_with_signals.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, 'enable', mock.Mock())
        monkeypatch.setattr(receiver, 'disable', mock.Mock())

    collector.connect()

    # check that the existing signal receivers are disabled
    for receiver in collector.signal_receivers_to_disable:
        assert receiver.disable.called
        assert not receiver.enable.called

    obj.delete()

    collector.disconnect()

    # check that the existing signal receivers are re-enabled
    for receiver in collector.signal_receivers_to_disable:
        assert receiver.enable.called

    assert collector.deletions == {
        SimpleModel: [es_doc],
    }

    read_alias = ESSimpleModel.get_read_alias()

    assert SimpleModel.objects.count() == 0
    assert es_with_signals.count(read_alias)['count'] == 1

    collector.delete_from_es()

    es_with_signals.indices.refresh()
    assert es_with_signals.count(read_alias)['count'] == 0