diff --git a/src/zope/index/text/tests/test_index.py b/src/zope/index/text/tests/test_index.py index d294d1e..1d89400 100644 --- a/src/zope/index/text/tests/test_index.py +++ b/src/zope/index/text/tests/test_index.py @@ -15,8 +15,15 @@ """ import unittest -class IndexTestBase: - # Subclasses must define '_getTargetClass' and '_getBTreesFamily' +# pylint:disable=protected-access + +class IndexTestMixin(object): + def _getTargetClass(self): + raise NotImplementedError() + + def _getBTreesFamily(self): + raise NotImplementedError() + def _makeOne(self): from zope.index.text.lexicon import Lexicon from zope.index.text.lexicon import Splitter @@ -133,8 +140,8 @@ def test_index_duplicated_words(self): index.wordCount()) wids = index._lexicon.termToWordIds("repeat") self.assertEqual(len(wids), 1) - repititive_wid = wids[0] - for wid, map in index._wordinfo.items(): + + for _wid, map in index._wordinfo.items(): self.assertEqual(len(map), 1) self.assertTrue(1 in map) @@ -174,7 +181,7 @@ def test_search_glob(self): results = index.search_glob("b*") self.assertEqual(list(results.keys()), [1, 2, 3]) -class CosineIndexTest32(IndexTestBase, unittest.TestCase): +class CosineIndexTest32(IndexTestMixin, unittest.TestCase): def _getTargetClass(self): from zope.index.text.cosineindex import CosineIndex @@ -184,7 +191,7 @@ def _getBTreesFamily(self): import BTrees return BTrees.family32 -class OkapiIndexTest32(IndexTestBase, unittest.TestCase): +class OkapiIndexTest32(IndexTestMixin, unittest.TestCase): def _getTargetClass(self): from zope.index.text.okapiindex import OkapiIndex @@ -194,7 +201,7 @@ def _getBTreesFamily(self): import BTrees return BTrees.family32 -class CosineIndexTest64(IndexTestBase, unittest.TestCase): +class CosineIndexTest64(IndexTestMixin, unittest.TestCase): def _getTargetClass(self): from zope.index.text.cosineindex import CosineIndex @@ -204,7 +211,7 @@ def _getBTreesFamily(self): import BTrees return BTrees.family64 -class OkapiIndexTest64(IndexTestBase, unittest.TestCase): +class OkapiIndexTest64(IndexTestMixin, unittest.TestCase): def _getTargetClass(self): from zope.index.text.okapiindex import OkapiIndex @@ -213,11 +220,3 @@ def _getTargetClass(self): def _getBTreesFamily(self): import BTrees return BTrees.family64 - -def test_suite(): - return unittest.TestSuite(( - unittest.makeSuite(CosineIndexTest32), - unittest.makeSuite(OkapiIndexTest32), - unittest.makeSuite(CosineIndexTest64), - unittest.makeSuite(OkapiIndexTest64), - )) diff --git a/src/zope/index/topic/filter.py b/src/zope/index/topic/filter.py index 8876b17..e4f7d8e 100644 --- a/src/zope/index/topic/filter.py +++ b/src/zope/index/topic/filter.py @@ -61,7 +61,7 @@ def setExpression(self, expr): def getIds(self): return self._ids - def __repr__(self): #pragma NO COVERAGE + def __repr__(self): # pragma: no cover return '%s: (%s) %s' % (self.id, self.expr, list(self._ids)) __str__ = __repr__ diff --git a/src/zope/index/topic/tests/test_index.py b/src/zope/index/topic/tests/test_index.py index 260e94b..adbfaa3 100644 --- a/src/zope/index/topic/tests/test_index.py +++ b/src/zope/index/topic/tests/test_index.py @@ -15,6 +15,8 @@ """ import unittest +# pylint:disable=protected-access,blacklisted-name + _marker = object() class TopicIndexTest(unittest.TestCase): @@ -27,21 +29,24 @@ def _get_family(self): import BTrees return BTrees.family32 - def _makeOne(self, family=_marker): + def _makeOne(self, family=_marker, populate=False): if family is _marker: family = self._get_family() if family is None: - return self._getTargetClass()() - return self._getTargetClass()(family) + index = self._getTargetClass()() + index = self._getTargetClass()(family) + if populate: + self._addFilters(index) + self._populate(index) + return index def _search(self, index, query, expected, operator='and'): - result = index.search(query, operator) self.assertEqual(result.keys(), expected) def _search_or(self, index, query, expected): return self._search(index, query, expected, 'or') - + def _search_and(self, index, query, expected): return self._search(index, query, expected, 'and') @@ -52,7 +57,7 @@ def _apply(self, index, query, expected, operator='and'): def _apply_or(self, index, query, expected): result = index.apply({'query': query, 'operator': 'or'}) self.assertEqual(result.keys(), expected) - + def _apply_and(self, index, query, expected): result = index.apply({'query': query, 'operator': 'and'}) self.assertEqual(result.keys(), expected) @@ -273,9 +278,6 @@ def test_apply_query_matches_multiple_explicit_or(self): result = index.apply({'query': ['foo', 'bar'], 'operator': 'or'}) self.assertEqual(set(result), set([1, 2, 3, 4])) - -class _NotYet: - def _addFilters(self, index): from zope.index.topic.filter import PythonFilteredSet index.addFilter( @@ -296,64 +298,64 @@ class O(object): def __init__(self, meta_type): self.meta_type = meta_type - index.index_doc(0 , O('doc0')) - index.index_doc(1 , O('doc1')) - index.index_doc(2 , O('doc1')) - index.index_doc(3 , O('doc2')) - index.index_doc(4 , O('doc2')) - index.index_doc(5 , O('doc3')) - index.index_doc(6 , O('doc3')) + index.index_doc(0, O('doc0')) + index.index_doc(1, O('doc1')) + index.index_doc(2, O('doc1')) + index.index_doc(3, O('doc2')) + index.index_doc(4, O('doc2')) + index.index_doc(5, O('doc3')) + index.index_doc(6, O('doc3')) def test_unindex(self): - index = self._makeOne() - index.unindex_doc(-99) # should not raise - index.unindex_doc(3) - index.unindex_doc(4) - index.unindex_doc(5) - self._search_or(index, 'doc1', [1,2]) - self._search_or(index, 'doc2', []) - self._search_or(index, 'doc3', [6]) - self._search_or(index, 'doc4', []) + index = self._makeOne(populate=True) + index.unindex_doc(-99) # should not raise + index.unindex_doc(3) + index.unindex_doc(4) + index.unindex_doc(5) + self._search_or(index, 'doc1', [1, 2]) + self._search_or(index, 'doc2', []) + self._search_or(index, 'doc3', [6]) + self._search_or(index, 'doc4', []) def test_or(self): - index = self._makeOne() - self._search_or(index, 'doc1', [1,2]) - self._search_or(index, ['doc1'],[1,2]) - self._search_or(index, 'doc2', [3,4]), - self._search_or(index, ['doc2'],[3,4]) - self._search_or(index, ['doc1','doc2'], [1,2,3,4]) + index = self._makeOne(populate=True) + self._search_or(index, 'doc1', [1, 2]) + self._search_or(index, ['doc1'], [1, 2]) + self._search_or(index, 'doc2', [3, 4]) + self._search_or(index, ['doc2'], [3, 4]) + self._search_or(index, ['doc1', 'doc2'], [1, 2, 3, 4]) def test_and(self): - index = self._makeOne() - self._search_and(index, 'doc1', [1,2]) - self._search_and(index, ['doc1'], [1,2]) - self._search_and(index, 'doc2', [3,4]) - self._search_and(index, ['doc2'], [3,4]) - self._search_and(index, ['doc1','doc2'], []) + index = self._makeOne(populate=True) + self._search_and(index, 'doc1', [1, 2]) + self._search_and(index, ['doc1'], [1, 2]) + self._search_and(index, 'doc2', [3, 4]) + self._search_and(index, ['doc2'], [3, 4]) + self._search_and(index, ['doc1', 'doc2'], []) def test_apply_or(self): - index = self._makeOne() - self._apply_or(index, 'doc1', [1,2]) - self._apply_or(index, ['doc1'],[1,2]) - self._apply_or(index, 'doc2', [3,4]), - self._apply_or(index, ['doc2'],[3,4]) - self._apply_or(index, ['doc1','doc2'], [1,2,3,4]) + index = self._makeOne(populate=True) + self._apply_or(index, 'doc1', [1, 2]) + self._apply_or(index, ['doc1'], [1, 2]) + self._apply_or(index, 'doc2', [3, 4]) + self._apply_or(index, ['doc2'], [3, 4]) + self._apply_or(index, ['doc1', 'doc2'], [1, 2, 3, 4]) def test_apply_and(self): - index = self._makeOne() - self._apply_and(index, 'doc1', [1,2]) - self._apply_and(index, ['doc1'], [1,2]) - self._apply_and(index, 'doc2', [3,4]) - self._apply_and(index, ['doc2'], [3,4]) - self._apply_and(index, ['doc1','doc2'], []) + index = self._makeOne(populate=True) + self._apply_and(index, 'doc1', [1, 2]) + self._apply_and(index, ['doc1'], [1, 2]) + self._apply_and(index, 'doc2', [3, 4]) + self._apply_and(index, ['doc2'], [3, 4]) + self._apply_and(index, ['doc1', 'doc2'], []) def test_apply(self): - index = self._makeOne() - self._apply(index, 'doc1', [1,2]) - self._apply(index, ['doc1'], [1,2]) - self._apply(index, 'doc2', [3,4]) - self._apply(index, ['doc2'], [3,4]) - self._apply(index, ['doc1','doc2'], []) + index = self._makeOne(populate=True) + self._apply(index, 'doc1', [1, 2]) + self._apply(index, ['doc1'], [1, 2]) + self._apply(index, 'doc2', [3, 4]) + self._apply(index, ['doc2'], [3, 4]) + self._apply(index, ['doc1', 'doc2'], []) class TopicIndexTest64(TopicIndexTest): @@ -362,7 +364,7 @@ def _get_family(self): return BTrees.family64 -class DummyFilter: +class DummyFilter(object): _cleared = False @@ -386,12 +388,5 @@ def unindex_doc(self, docid): self._unindexed.append(docid) def getIds(self): - if self._family is not None: - return self._family.IF.TreeSet(self._ids) - return set(self._ids) - -def test_suite(): - return unittest.TestSuite(( - unittest.makeSuite(TopicIndexTest), - unittest.makeSuite(TopicIndexTest64), - )) + assert self._family is not None + return self._family.IF.TreeSet(self._ids)