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

Guido van Rossum guido@python.org
Tue, 30 Apr 2002 17:54:24 -0400


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

Added Files:
      Tag: TextIndexDS9-branch
	testQueryEngine.py 
Log Message:
Add a query engine.

=== Added File Products/ZCTextIndex/tests/testQueryEngine.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.
# 
##############################################################################

from unittest import TestCase, TestSuite, main, makeSuite

from BTrees.IIBTree import IIBucket

from Products.ZCTextIndex.QueryParser import QueryParser, ParseError
from Products.ZCTextIndex.QueryEngine import QueryEngine, QueryError

class FauxIndex:

    def search(self, term):
        if term == "foo":
            b = IIBucket()
            b[1] = 1
            b[3] = 1
            return b
        if term == "bar":
            b = IIBucket()
            b[1] = 1
            b[2] = 1
            return b
        return IIBucket() # an empty one

class TestQueryEngine(TestCase):

    def setUp(self):
        self.parser = QueryParser()
        self.engine = QueryEngine()
        self.index = FauxIndex()

    def compareSet(self, set, dict):
        self.assertEqual(len(set), len(dict))
        for k, v in dict.items():
            self.assertEqual(set[k], v)

    def compareQuery(self, query, dict):
        tree = self.parser.parseQuery(query)
        set = self.engine.executeQuery(self.index, tree)
        self.compareSet(set, dict)

    def testExecuteQuery(self):
        self.compareQuery("foo AND bar", {1: 2})
        self.compareQuery("foo OR bar", {1: 2, 2: 1, 3:1})
        self.compareQuery("foo AND NOT bar", {3: 1})
        self.compareQuery("foo AND foo AND foo", {1: 3, 3: 3})
        self.compareQuery("foo OR foo OR foo", {1: 3, 3: 3})

    def testInvalidQuery(self):
        tree = self.parser.parseQuery("NOT foo")
        self.assertRaises(QueryError,
                          self.engine.executeQuery, self.index, tree)

def test_suite():
    return makeSuite(TestQueryEngine)

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