[Zope-CVS] CVS: Products/ZCTextIndex/tests - testIndex.py:1.1.2.10

Guido van Rossum guido@python.org
Mon, 13 May 2002 20:33:44 -0400


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

Modified Files:
      Tag: TextIndexDS9-branch
	testIndex.py 
Log Message:
Add trivial tests for search_phrase and search_glob.

Get rid of SimpleLexicon, using the real Lexicon and Splitter instead
(in part so that the SimpleLexicon doesn't have to grow a
globToWordIds() method, but also because it just seems to make more
sense).

Formulate some tests that require a particular result set differently;
assertEqual(list(result.keys()), [...]) shows better diagnostics.


=== Products/ZCTextIndex/tests/testIndex.py 1.1.2.9 => 1.1.2.10 ===
 
 from Products.ZCTextIndex.Index import Index
-
-class SimpleLexicon:
-    def __init__(self):
-        self._words = {}
-
-    def sourceToWordIds(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
-
-    def termToWordIds(self, text):
-        words = text.split()
-        wids = []
-        for word in words:
-            wid = self._words.get(word)
-            if wid is None:
-                continue
-            wids.append(wid)
-        return wids
+from Products.ZCTextIndex.Lexicon import Lexicon, Splitter
 
 class IndexTest(TestCase):
     def setUp(self):
-        self.lexicon = SimpleLexicon()
+        self.lexicon = Lexicon(Splitter())
         self.index = Index(self.lexicon)
 
     def test_index_document(self, DOCID=1):
@@ -116,20 +92,33 @@
     def test_simple_query_oneresult(self):
         self.index.index_doc(1, 'not the same document')
         results = self.index.search("document")
-        self.assertEqual(len(results), 1)
-        self.assert_(results.has_key(1))
+        self.assertEqual(list(results.keys()), [1])
 
     def test_simple_query_noresults(self):
         self.index.index_doc(1, 'not the same document')
         results = self.index.search("frobnicate")
-        self.assertEqual(len(results), 0)
+        self.assertEqual(list(results.keys()), [])
 
     def test_query_oneresult(self):
         self.index.index_doc(1, 'not the same document')
         self.index.index_doc(2, 'something about something else')
         results = self.index.search("document")
-        self.assertEqual(len(results), 1)
-        self.assert_(results.has_key(1))
+        self.assertEqual(list(results.keys()), [1])
+
+    def test_search_phrase(self):
+        self.index.index_doc(1, "the quick brown fox jumps over the lazy dog")
+        self.index.index_doc(2, "the quick fox jumps lazy over the brown dog")
+        results = self.index.search_phrase("quick brown fox")
+        self.assertEqual(list(results.keys()), [1])
+
+    def test_search_glob(self):
+        self.index.index_doc(1, "how now brown cow")
+        self.index.index_doc(2, "hough nough browne cough")
+        self.index.index_doc(3, "bar brawl")
+        results = self.index.search_glob("bro*")
+        self.assertEqual(list(results.keys()), [1, 2])
+        results = self.index.search_glob("b*")
+        self.assertEqual(list(results.keys()), [1, 2, 3])
 
 def test_suite():
     return makeSuite(IndexTest)