Skip to content

Commit

Permalink
Enable c.indexing during tests, but patch it to not defer operations:
Browse files Browse the repository at this point in the history
We need to enable collective.indexing during tests in order to
create Solr tests (because ftw.solr integrates itself by providing
an IIndexQueueProcessor).

So we need c.indexing's pluggable IndexQueueProcessor mechanism, but
we don't want the unpredictable behavior of reindexes that are deferred
until the end of the transaction during tests (or the queue getting
flushed at arbitrary points).

We therefore patch the IndexQueue's operation methods so that they
always immediately process the queue after being called.
  • Loading branch information
lukasgraf committed Jul 12, 2019
1 parent b2f7159 commit a4a0070
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
6 changes: 6 additions & 0 deletions base-plone-4.3.x.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ package-namespace = opengever
test-egg = opengever.core[api, tests]

[test]
initialization +=
# Enable c.indexing during tests, but patch it to not defer operations
from opengever.testing.patch import patch_collective_indexing
patch_collective_indexing()
from collective.indexing import monkey

arguments = ['-s', '${buildout:package-namespace}', '-s', 'plonetheme', '--exit-with-status', '--auto-color', '--auto-progress', '--xml', '--package-path', '${buildout:directory}/${buildout:package-namespace}', '${buildout:package-namespace}', '--package-path', '${buildout:directory}/plonetheme', 'plonetheme']

eggs +=
Expand Down
1 change: 1 addition & 0 deletions docs/HISTORY.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Changelog
2019.4.0 (unreleased)
---------------------

- Enable c.indexing during tests, but patch it to not defer operations. [lgraf]
- Fix upgrade step that adds linguistic index for task principal. [lgraf]
- Add ftw.catalogdoctor to dependencies. [deiferni]
- Fix exception formatter patch when there is no plone site. [deiferni]
Expand Down
38 changes: 38 additions & 0 deletions opengever/testing/patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,41 @@ def __exit__(self, exc_type, exc_value, traceback):
"""Restore the original value to the target object's attribute.
"""
self.restore()


def patch_collective_indexing():
"""During tests, patch collective.indexing's IndexQueue so that it
doesn't defer (re)indexing to the end of the transaction, but instead
executes these operations immediately (by always processing the queue).
This allows us to still have c.indexing active during tests, to make use
of it's pluggable IndexQueueProcessor mechanism (used to integrate Solr),
but not have the negative side effects from deferred indexing.
"""
from collective.indexing.queue import IndexQueue
from collective.indexing.queue import processQueue

orig_index = IndexQueue.index

def index(self, obj, attributes=None):
result = orig_index(self, obj, attributes=attributes)
processQueue()
return result

orig_reindex = IndexQueue.reindex

def reindex(self, obj, attributes=None):
result = orig_reindex(self, obj, attributes=attributes)
processQueue()
return result

orig_unindex = IndexQueue.unindex

def unindex(self, obj):
result = orig_unindex(self, obj)
processQueue()
return result

IndexQueue.index = index
IndexQueue.reindex = reindex
IndexQueue.unindex = unindex

0 comments on commit a4a0070

Please sign in to comment.