PyLucene 3.4.0-1 import
[pylucene.git] / samples / LuceneInAction / lia / common / LiaTestCase.py
1 # ====================================================================
2 #   Licensed under the Apache License, Version 2.0 (the "License");
3 #   you may not use this file except in compliance with the License.
4 #   You may obtain a copy of the License at
5 #
6 #       http://www.apache.org/licenses/LICENSE-2.0
7 #
8 #   Unless required by applicable law or agreed to in writing, software
9 #   distributed under the License is distributed on an "AS IS" BASIS,
10 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 #   See the License for the specific language governing permissions and
12 #   limitations under the License.
13 # ====================================================================
14
15 import os
16
17 from unittest import TestCase
18 from lucene import \
19     SimpleFSDirectory, Document, \
20     System, SimpleDateFormat, File
21
22
23 class LiaTestCase(TestCase):
24
25     def __init__(self, *args):
26
27         super(LiaTestCase, self).__init__(*args)
28         self.indexDir = System.getProperty("index.dir")
29
30     def setUp(self):
31
32         self.directory = SimpleFSDirectory(File(self.indexDir))
33
34     def tearDown(self):
35
36         self.directory.close()
37
38     #
39     # For troubleshooting
40     #
41     def dumpHits(self, searcher, scoreDocs):
42
43         if not scoreDocs:
44             print "No hits"
45         else:
46             for scoreDoc in scoreDocs:
47                 print "%s: %s" %(scoreDoc.score,
48                                  searcher.doc(scoreDoc.doc).get('title'))
49
50     def assertHitsIncludeTitle(self, searcher, scoreDocs, title,
51                                fail=False):
52
53         for scoreDoc in scoreDocs:
54             doc = searcher.doc(scoreDoc.doc)
55             if title == doc.get("title"):
56                 if fail:
57                     self.fail("title '%s' found" %(title))
58                 return
59
60         if not fail:
61             self.fail("title '%s' not found" %(title))
62
63     def parseDate(self, s):
64
65         return SimpleDateFormat("yyyy-MM-dd").parse(s)