[Zope-CVS] CVS: Products/ZCTextIndex/tests - test_index.py:1.1.2.1

Fred L. Drake, Jr. fdrake@acm.org
Tue, 30 Apr 2002 16:19:44 -0400


Update of /cvs-repository/Products/ZCTextIndex/tests
In directory cvs.zope.org:/tmp/cvs-serv16959/tests

Added Files:
      Tag: TextIndexDS9-branch
	test_index.py 
Log Message:
First portion of new index.

=== Added File Products/ZCTextIndex/tests/test_index.py ===
##############################################################################
#
# Copyright (c) 2001, 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.
# 
##############################################################################
"""

Revision information:
$Id: test_index.py,v 1.1.2.1 2002/04/30 20:19:43 fdrake Exp $
"""

from unittest import TestCase, TestSuite, main, makeSuite

from Products.ZCTextIndex.Index import Index


class SimpleLexicon:
    def __init__(self):
        self._words = {}

    def textToWordIDs(self, text):
        words = text.split()
        wids = []
        for word in words:
            wid = self._words.get(word)
            if wid is None:
                wid = len(self._words)
                self._words[word] = wid
            wids.append(wid)
        return wids


class Indexable:
    def __init__(self, text):
        self.text = text

class MethodIndexable:
    def __init__(self, text):
        self._text = text

    def text(self):
        return self._text


class IndexTest(TestCase):
    def setUp(self):
        self.lexicon = SimpleLexicon()
        self.index = Index(self.lexicon, "text")

    def test_index_document(self):
        doc = Indexable("this is a simple document")
        DOCID = 1
        self.index.index_object(DOCID, doc)
        self.assert_(self.index._docweight[DOCID])
        self.assertEqual(len(self.index._wordinfo), 5)
        self.assertEqual(len(self.index._docwords), 1)
        self.assertEqual(len(self.index._docwords[DOCID]), 5)
        for docfreq, map in self.index._wordinfo.values():
            self.assertEqual(docfreq, 1)
            self.assertEqual(len(map), 1)
            self.assert_(map.has_key(DOCID))


def test_suite():
    return makeSuite(IndexTest)

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