[Zope3-checkins] CVS: Zope3/src/zope/keywordindex/tests - test_keywordindex.py:1.1.2.1

Andreas Jung andreas@andreas-jung.com
Sat, 12 Jul 2003 05:32:03 -0400


Update of /cvs-repository/Zope3/src/zope/keywordindex/tests
In directory cvs.zope.org:/tmp/cvs-serv25534

Added Files:
      Tag: ajung-keywordindex-branch
	test_keywordindex.py 
Log Message:
added



=== Added File Zope3/src/zope/keywordindex/tests/test_keywordindex.py ===
##############################################################################
#
# Copyright (c) 2002 Zope Corporation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################

from unittest import TestCase, TestSuite, main, makeSuite

from zodb.btrees.IIBTree import IISet
from zope.keywordindex.keywordindex import KeywordIndex
from zope.keywordindex.ikeywordindex import IKeywordIndex
from zope.interface.verify import verifyClass

class KeywordIndexTest(TestCase):

    def setUp(self):
        self.index = KeywordIndex()

    def _populate_index(self):

        self.index.index_doc(1, ('zope', 'CMF', 'zope3', 'Zope3'))
        self.index.index_doc(2, ('the', 'quick', 'brown', 'FOX'))
        self.index.index_doc(3, ('Zope', 'zope'))
        self.index.index_doc(4, ())
        self.index.index_doc(5, ('cmf',))

    def test_interface(self):
        verifyClass(IKeywordIndex, KeywordIndex)

    def test_empty_index(self):

        self.assertEqual(self.index.documentCount(), 0)
        self._populate_index()
        self.assertEqual(self.index.documentCount(), 4)
        self.index.clear()
        self.assertEqual(self.index.documentCount(), 0)

    def test_unindex(self):

        self._populate_index()
        self.assertEqual(self.index.documentCount(), 4)
        self.index.unindex_doc(1)
        self.index.unindex_doc(2)
        self.assertEqual(self.index.documentCount(), 2)
        self.index.unindex_doc(-99999)     # no exception should be raised
        self.assertEqual(self.index.documentCount(), 2)


    def test_reindex(self):

        self._populate_index()
        self.assertEqual(self.index.documentCount(), 4)
        self.index.unindex_doc(1)
        self.index.unindex_doc(2)
        self.index.index_doc(1,  ('foo', 'bar'))
        self.assertEqual(self.index.documentCount(), 3)


    def test_hasdoc(self):
    
        self._populate_index()
        self.assertEqual(self.index.has_doc(1), 1)
        self.assertEqual(self.index.has_doc(2), 1)
        self.assertEqual(self.index.has_doc(3), 1)
        self.assertEqual(self.index.has_doc(4), 0)
        self.assertEqual(self.index.has_doc(5), 1)
        self.assertEqual(self.index.has_doc(6), 0)


    def test_simplesearch(self):

        self._populate_index()
        self._search(None,    IISet())
        self._search('',      IISet())
        self._search('cmf',   IISet([1, 5]))
        self._search('zope',  IISet([1, 3]))
        self._search('zope3', IISet([1]))
        self._search('foo',   IISet())

    def test_search_and(self):

        self._populate_index()
        self._search_and(('cmf', 'zope3'), IISet([1]))
        self._search_and(('cmf', 'zope'),  IISet([1]))
        self._search_and(('cmf', 'zope4'), IISet())
        self._search_and(('zope', 'ZOPE'), IISet([1, 3]))

    def test_search_or(self):

        self._populate_index()
        self._search_or(('cmf', 'zope3'), IISet([1, 5]))
        self._search_or(('cmf', 'zope'),  IISet([1, 3, 5]))
        self._search_or(('cmf', 'zope4'), IISet([1, 5]))
        self._search_or(('zope', 'ZOPE'), IISet([1,3]))

def test_suite():
    return TestSuite((makeSuite(KeywordIndexTest), ))

if __name__=='__main__':
    main(defaultTest='test_suite')