319536d609c3198d1ebb906f8f85df80504ff8d5
1 # Copyright (c) 2002 ekit.com Inc (http://www.ekit-inc.com/)
2 #
3 # Permission is hereby granted, free of charge, to any person obtaining a copy
4 # of this software and associated documentation files (the "Software"), to deal
5 # in the Software without restriction, including without limitation the rights
6 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 # copies of the Software, and to permit persons to whom the Software is
8 # furnished to do so, subject to the following conditions:
9 #
10 # The above copyright notice and this permission notice shall be included in
11 # all copies or substantial portions of the Software.
12 #
13 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19 # SOFTWARE.
21 # $Id: test_indexer.py,v 1.13 2008-09-11 19:10:30 schlatterbeck Exp $
23 import os, unittest, shutil
25 from roundup.backends import get_backend, have_backend
26 from roundup.backends.indexer_rdbms import Indexer
28 # borrow from other tests
29 from db_test_base import setupSchema, config
30 from test_postgresql import postgresqlOpener
31 from test_mysql import mysqlOpener
32 from test_sqlite import sqliteOpener
34 class db:
35 class config(dict):
36 DATABASE = 'test-index'
37 config = config()
38 config[('main', 'indexer_stopwords')] = []
40 class IndexerTest(unittest.TestCase):
41 def setUp(self):
42 if os.path.exists('test-index'):
43 shutil.rmtree('test-index')
44 os.mkdir('test-index')
45 os.mkdir('test-index/files')
46 from roundup.backends.indexer_dbm import Indexer
47 self.dex = Indexer(db)
48 self.dex.load_index()
50 def assertSeqEqual(self, s1, s2):
51 # First argument is the db result we're testing, second is the
52 # desired result. Some db results don't have iterable rows, so we
53 # have to work around that.
54 # Also work around some dbs not returning items in the expected
55 # order.
56 s1 = list([tuple([r[n] for n in range(len(r))]) for r in s1])
57 s1.sort()
58 if s1 != s2:
59 self.fail('contents of %r != %r'%(s1, s2))
61 def test_basics(self):
62 self.dex.add_text(('test', '1', 'foo'), 'a the hello world')
63 self.dex.add_text(('test', '2', 'foo'), 'blah blah the world')
64 self.assertSeqEqual(self.dex.find(['world']), [('test', '1', 'foo'),
65 ('test', '2', 'foo')])
66 self.assertSeqEqual(self.dex.find(['blah']), [('test', '2', 'foo')])
67 self.assertSeqEqual(self.dex.find(['blah', 'hello']), [])
69 def test_change(self):
70 self.dex.add_text(('test', '1', 'foo'), 'a the hello world')
71 self.dex.add_text(('test', '2', 'foo'), 'blah blah the world')
72 self.assertSeqEqual(self.dex.find(['world']), [('test', '1', 'foo'),
73 ('test', '2', 'foo')])
74 self.dex.add_text(('test', '1', 'foo'), 'a the hello')
75 self.assertSeqEqual(self.dex.find(['world']), [('test', '2', 'foo')])
77 def test_clear(self):
78 self.dex.add_text(('test', '1', 'foo'), 'a the hello world')
79 self.dex.add_text(('test', '2', 'foo'), 'blah blah the world')
80 self.assertSeqEqual(self.dex.find(['world']), [('test', '1', 'foo'),
81 ('test', '2', 'foo')])
82 self.dex.add_text(('test', '1', 'foo'), '')
83 self.assertSeqEqual(self.dex.find(['world']), [('test', '2', 'foo')])
85 def tearDown(self):
86 shutil.rmtree('test-index')
88 class XapianIndexerTest(IndexerTest):
89 def setUp(self):
90 if os.path.exists('test-index'):
91 shutil.rmtree('test-index')
92 os.mkdir('test-index')
93 from roundup.backends.indexer_xapian import Indexer
94 self.dex = Indexer(db)
95 def tearDown(self):
96 shutil.rmtree('test-index')
98 class RDBMSIndexerTest(IndexerTest):
99 def setUp(self):
100 # remove previous test, ignore errors
101 if os.path.exists(config.DATABASE):
102 shutil.rmtree(config.DATABASE)
103 self.db = self.module.Database(config, 'admin')
104 self.dex = Indexer(self.db)
105 def tearDown(self):
106 if hasattr(self, 'db'):
107 self.db.close()
108 if os.path.exists(config.DATABASE):
109 shutil.rmtree(config.DATABASE)
111 class postgresqlIndexerTest(postgresqlOpener, RDBMSIndexerTest):
112 def setUp(self):
113 postgresqlOpener.setUp(self)
114 RDBMSIndexerTest.setUp(self)
115 def tearDown(self):
116 RDBMSIndexerTest.tearDown(self)
117 postgresqlOpener.tearDown(self)
119 class mysqlIndexerTest(mysqlOpener, RDBMSIndexerTest):
120 def setUp(self):
121 mysqlOpener.setUp(self)
122 RDBMSIndexerTest.setUp(self)
123 def tearDown(self):
124 RDBMSIndexerTest.tearDown(self)
125 mysqlOpener.tearDown(self)
127 class sqliteIndexerTest(sqliteOpener, RDBMSIndexerTest):
128 pass
130 def test_suite():
131 suite = unittest.TestSuite()
133 suite.addTest(unittest.makeSuite(IndexerTest))
135 try:
136 import xapian
137 suite.addTest(unittest.makeSuite(XapianIndexerTest))
138 except ImportError:
139 print "Skipping Xapian indexer tests"
140 pass
142 if have_backend('postgresql'):
143 # make sure we start with a clean slate
144 if postgresqlOpener.module.db_exists(config):
145 postgresqlOpener.module.db_nuke(config, 1)
146 suite.addTest(unittest.makeSuite(postgresqlIndexerTest))
147 else:
148 print "Skipping postgresql indexer tests"
150 if have_backend('mysql'):
151 # make sure we start with a clean slate
152 if mysqlOpener.module.db_exists(config):
153 mysqlOpener.module.db_nuke(config)
154 suite.addTest(unittest.makeSuite(mysqlIndexerTest))
155 else:
156 print "Skipping mysql indexer tests"
158 if have_backend('sqlite'):
159 # make sure we start with a clean slate
160 if sqliteOpener.module.db_exists(config):
161 sqliteOpener.module.db_nuke(config)
162 suite.addTest(unittest.makeSuite(sqliteIndexerTest))
163 else:
164 print "Skipping sqlite indexer tests"
166 return suite
168 if __name__ == '__main__':
169 runner = unittest.TextTestRunner()
170 unittest.main(testRunner=runner)
172 # vim: set filetype=python ts=4 sw=4 et si