1818# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
1919# SOFTWARE.
2020
21- # $Id: test_indexer.py,v 1.10 2006-02-07 04:59:05 richard Exp $
21+ # $Id: test_indexer.py,v 1.11 2008-09-01 00:43:02 richard Exp $
2222
2323import os , unittest , shutil
2424
25+ from roundup .backends import get_backend , have_backend
26+ from roundup .backends .indexer_rdbms import Indexer
27+
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
33+
2534class db :
2635 class config (dict ):
2736 DATABASE = 'test-index'
@@ -38,29 +47,35 @@ def setUp(self):
3847 self .dex = Indexer (db )
3948 self .dex .load_index ()
4049
50+ def assertSeqEqual (self , s1 , s2 ):
51+ # first argument is the db result we're testing, second is the desired result
52+ # some db results don't have iterable rows, so we have to work around that
53+ if [i for x ,y in zip (s1 , s2 ) for i ,j in enumerate (y ) if x [i ] != j ]:
54+ self .fail ('contents of %r != %r' % (s1 , s2 ))
55+
4156 def test_basics (self ):
4257 self .dex .add_text (('test' , '1' , 'foo' ), 'a the hello world' )
4358 self .dex .add_text (('test' , '2' , 'foo' ), 'blah blah the world' )
44- self .assertEqual (self .dex .find (['world' ]), [('test' , '1' , 'foo' ),
59+ self .assertSeqEqual (self .dex .find (['world' ]), [('test' , '1' , 'foo' ),
4560 ('test' , '2' , 'foo' )])
46- self .assertEqual (self .dex .find (['blah' ]), [('test' , '2' , 'foo' )])
47- self .assertEqual (self .dex .find (['blah' , 'hello' ]), [])
61+ self .assertSeqEqual (self .dex .find (['blah' ]), [('test' , '2' , 'foo' )])
62+ self .assertSeqEqual (self .dex .find (['blah' , 'hello' ]), [])
4863
4964 def test_change (self ):
5065 self .dex .add_text (('test' , '1' , 'foo' ), 'a the hello world' )
5166 self .dex .add_text (('test' , '2' , 'foo' ), 'blah blah the world' )
52- self .assertEqual (self .dex .find (['world' ]), [('test' , '1' , 'foo' ),
67+ self .assertSeqEqual (self .dex .find (['world' ]), [('test' , '1' , 'foo' ),
5368 ('test' , '2' , 'foo' )])
5469 self .dex .add_text (('test' , '1' , 'foo' ), 'a the hello' )
55- self .assertEqual (self .dex .find (['world' ]), [('test' , '2' , 'foo' )])
70+ self .assertSeqEqual (self .dex .find (['world' ]), [('test' , '2' , 'foo' )])
5671
5772 def test_clear (self ):
5873 self .dex .add_text (('test' , '1' , 'foo' ), 'a the hello world' )
5974 self .dex .add_text (('test' , '2' , 'foo' ), 'blah blah the world' )
60- self .assertEqual (self .dex .find (['world' ]), [('test' , '1' , 'foo' ),
75+ self .assertSeqEqual (self .dex .find (['world' ]), [('test' , '1' , 'foo' ),
6176 ('test' , '2' , 'foo' )])
6277 self .dex .add_text (('test' , '1' , 'foo' ), '' )
63- self .assertEqual (self .dex .find (['world' ]), [('test' , '2' , 'foo' )])
78+ self .assertSeqEqual (self .dex .find (['world' ]), [('test' , '2' , 'foo' )])
6479
6580 def tearDown (self ):
6681 shutil .rmtree ('test-index' )
@@ -75,15 +90,74 @@ def setUp(self):
7590 def tearDown (self ):
7691 shutil .rmtree ('test-index' )
7792
93+ class RDBMSIndexerTest (IndexerTest ):
94+ def setUp (self ):
95+ # remove previous test, ignore errors
96+ if os .path .exists (config .DATABASE ):
97+ shutil .rmtree (config .DATABASE )
98+ self .db = self .module .Database (config , 'admin' )
99+ self .dex = Indexer (self .db )
100+ def tearDown (self ):
101+ if hasattr (self , 'db' ):
102+ self .db .close ()
103+ if os .path .exists (config .DATABASE ):
104+ shutil .rmtree (config .DATABASE )
105+
106+ class postgresqlIndexerTest (postgresqlOpener , RDBMSIndexerTest ):
107+ def setUp (self ):
108+ postgresqlOpener .setUp (self )
109+ RDBMSIndexerTest .setUp (self )
110+ def tearDown (self ):
111+ RDBMSIndexerTest .tearDown (self )
112+ postgresqlOpener .tearDown (self )
113+
114+ class mysqlIndexerTest (mysqlOpener , RDBMSIndexerTest ):
115+ def setUp (self ):
116+ mysqlOpener .setUp (self )
117+ RDBMSIndexerTest .setUp (self )
118+ def tearDown (self ):
119+ RDBMSIndexerTest .tearDown (self )
120+ mysqlOpener .tearDown (self )
121+
122+ class sqliteIndexerTest (sqliteOpener , RDBMSIndexerTest ):
123+ pass
124+
78125def test_suite ():
79126 suite = unittest .TestSuite ()
127+
80128 suite .addTest (unittest .makeSuite (IndexerTest ))
129+
81130 try :
82131 import xapian
83132 suite .addTest (unittest .makeSuite (XapianIndexerTest ))
84133 except ImportError :
85134 print "Skipping Xapian indexer tests"
86135 pass
136+
137+ if have_backend ('postgresql' ):
138+ # make sure we start with a clean slate
139+ if postgresqlOpener .module .db_exists (config ):
140+ postgresqlOpener .module .db_nuke (config , 1 )
141+ suite .addTest (unittest .makeSuite (postgresqlIndexerTest ))
142+ else :
143+ print "Skipping postgresql indexer tests"
144+
145+ if have_backend ('mysql' ):
146+ # make sure we start with a clean slate
147+ if mysqlOpener .module .db_exists (config ):
148+ mysqlOpener .module .db_nuke (config )
149+ suite .addTest (unittest .makeSuite (mysqlIndexerTest ))
150+ else :
151+ print "Skipping mysql indexer tests"
152+
153+ if have_backend ('sqlite' ):
154+ # make sure we start with a clean slate
155+ if sqliteOpener .module .db_exists (config ):
156+ sqliteOpener .module .db_nuke (config )
157+ suite .addTest (unittest .makeSuite (sqliteIndexerTest ))
158+ else :
159+ print "Skipping sqlite indexer tests"
160+
87161 return suite
88162
89163if __name__ == '__main__' :
0 commit comments