Mercurial > p > roundup > code
comparison test/test_indexer.py @ 6909:86cde9cae7e1
Improve test coverage of backends/indexer_common::get_indexer() method
| author | John Rouillard <rouilj@ieee.org> |
|---|---|
| date | Sat, 03 Sep 2022 21:33:12 -0400 |
| parents | 3260926d7e7e |
| children | a23eaa3013e6 |
comparison
equal
deleted
inserted
replaced
| 6908:3260926d7e7e | 6909:86cde9cae7e1 |
|---|---|
| 21 import os, unittest, shutil | 21 import os, unittest, shutil |
| 22 | 22 |
| 23 import pytest | 23 import pytest |
| 24 from roundup.backends import get_backend, have_backend | 24 from roundup.backends import get_backend, have_backend |
| 25 from roundup.backends.indexer_rdbms import Indexer | 25 from roundup.backends.indexer_rdbms import Indexer |
| 26 from roundup.backends.indexer_common import get_indexer | |
| 26 | 27 |
| 27 from roundup.cgi.exceptions import IndexerQueryError | 28 from roundup.cgi.exceptions import IndexerQueryError |
| 28 | 29 |
| 29 # borrow from other tests | 30 # borrow from other tests |
| 30 from .db_test_base import setupSchema, config | 31 from .db_test_base import setupSchema, config |
| 31 from .test_postgresql import postgresqlOpener, skip_postgresql | 32 from .test_postgresql import postgresqlOpener, skip_postgresql |
| 32 from .test_mysql import mysqlOpener, skip_mysql | 33 from .test_mysql import mysqlOpener, skip_mysql |
| 33 from .test_sqlite import sqliteOpener | 34 from .test_sqlite import sqliteOpener |
| 35 from .test_anydbm import anydbmOpener | |
| 34 | 36 |
| 35 try: | 37 try: |
| 36 import xapian | 38 import xapian |
| 37 skip_xapian = lambda func, *args, **kwargs: func | 39 skip_xapian = lambda func, *args, **kwargs: func |
| 38 except ImportError: | 40 except ImportError: |
| 57 DATABASE = 'test-index' | 59 DATABASE = 'test-index' |
| 58 config = config() | 60 config = config() |
| 59 config[('main', 'indexer_stopwords')] = [] | 61 config[('main', 'indexer_stopwords')] = [] |
| 60 config[('main', 'indexer_language')] = "english" | 62 config[('main', 'indexer_language')] = "english" |
| 61 | 63 |
| 62 class IndexerTest(unittest.TestCase): | 64 class IndexerTest(anydbmOpener, unittest.TestCase): |
| 63 def setUp(self): | 65 |
| 66 indexer_name = "native" | |
| 67 | |
| 68 def setUp(self): | |
| 69 # remove previous test, ignore errors | |
| 70 if os.path.exists(config.DATABASE): | |
| 71 shutil.rmtree(config.DATABASE) | |
| 72 self.db = self.module.Database(config, 'admin') | |
| 73 | |
| 64 if os.path.exists('test-index'): | 74 if os.path.exists('test-index'): |
| 65 shutil.rmtree('test-index') | 75 shutil.rmtree('test-index') |
| 66 os.mkdir('test-index') | 76 os.mkdir('test-index') |
| 67 os.mkdir('test-index/files') | 77 os.mkdir('test-index/files') |
| 68 from roundup.backends.indexer_dbm import Indexer | 78 from roundup.backends.indexer_dbm import Indexer |
| 101 self.dex.add_text(('test', '2', 'foo'), 'blah blah the world') | 111 self.dex.add_text(('test', '2', 'foo'), 'blah blah the world') |
| 102 self.assertSeqEqual(self.dex.find(['world']), [('test', '1', 'foo'), | 112 self.assertSeqEqual(self.dex.find(['world']), [('test', '1', 'foo'), |
| 103 ('test', '2', 'foo')]) | 113 ('test', '2', 'foo')]) |
| 104 self.dex.add_text(('test', '1', 'foo'), '') | 114 self.dex.add_text(('test', '1', 'foo'), '') |
| 105 self.assertSeqEqual(self.dex.find(['world']), [('test', '2', 'foo')]) | 115 self.assertSeqEqual(self.dex.find(['world']), [('test', '2', 'foo')]) |
| 116 | |
| 117 def test_get_indexer(self): | |
| 118 def class_name_of(object): | |
| 119 """ take and object and return just the class name. | |
| 120 So in: | |
| 121 | |
| 122 return the class name before "at". | |
| 123 | |
| 124 """ | |
| 125 return(str(object).split()[0]) | |
| 126 | |
| 127 old_indexer = self.db.config['INDEXER'] | |
| 128 self.db.config['INDEXER'] = self.indexer_name | |
| 129 | |
| 130 self.assertEqual(class_name_of(self.dex), | |
| 131 class_name_of(get_indexer(self.db.config, self.db))) | |
| 132 | |
| 133 self.db.config['INDEXER'] = old_indexer | |
| 106 | 134 |
| 107 def test_stopwords(self): | 135 def test_stopwords(self): |
| 108 """Test that we can find a text with a stopword in it.""" | 136 """Test that we can find a text with a stopword in it.""" |
| 109 stopword = "with" | 137 stopword = "with" |
| 110 self.assertTrue(self.dex.is_stopword(stopword.upper())) | 138 self.assertTrue(self.dex.is_stopword(stopword.upper())) |
| 179 self.assertSeqEqual(self.dex.find([u'\u0440\u0443\u0441\u0441\u043a\u0438\u0439']), | 207 self.assertSeqEqual(self.dex.find([u'\u0440\u0443\u0441\u0441\u043a\u0438\u0439']), |
| 180 [('test', '2', 'a')]) | 208 [('test', '2', 'a')]) |
| 181 | 209 |
| 182 def tearDown(self): | 210 def tearDown(self): |
| 183 shutil.rmtree('test-index') | 211 shutil.rmtree('test-index') |
| 212 if hasattr(self, 'db'): | |
| 213 self.db.close() | |
| 214 if os.path.exists(config.DATABASE): | |
| 215 shutil.rmtree(config.DATABASE) | |
| 184 | 216 |
| 185 @skip_whoosh | 217 @skip_whoosh |
| 186 class WhooshIndexerTest(IndexerTest): | 218 class WhooshIndexerTest(IndexerTest): |
| 187 def setUp(self): | 219 |
| 220 indexer_name = "whoosh" | |
| 221 | |
| 222 def setUp(self): | |
| 223 IndexerTest.setUp(self) | |
| 224 | |
| 188 if os.path.exists('test-index'): | 225 if os.path.exists('test-index'): |
| 189 shutil.rmtree('test-index') | 226 shutil.rmtree('test-index') |
| 190 os.mkdir('test-index') | 227 os.mkdir('test-index') |
| 191 from roundup.backends.indexer_whoosh import Indexer | 228 from roundup.backends.indexer_whoosh import Indexer |
| 192 self.dex = Indexer(db) | 229 self.dex = Indexer(db) |
| 193 def tearDown(self): | 230 def tearDown(self): |
| 194 shutil.rmtree('test-index') | 231 IndexerTest.tearDown(self) |
| 195 | 232 |
| 196 @skip_xapian | 233 @skip_xapian |
| 197 class XapianIndexerTest(IndexerTest): | 234 class XapianIndexerTest(IndexerTest): |
| 198 def setUp(self): | 235 |
| 236 indexer_name = "xapian" | |
| 237 | |
| 238 def setUp(self): | |
| 239 IndexerTest.setUp(self) | |
| 240 | |
| 199 if os.path.exists('test-index'): | 241 if os.path.exists('test-index'): |
| 200 shutil.rmtree('test-index') | 242 shutil.rmtree('test-index') |
| 201 os.mkdir('test-index') | 243 os.mkdir('test-index') |
| 202 from roundup.backends.indexer_xapian import Indexer | 244 from roundup.backends.indexer_xapian import Indexer |
| 203 self.dex = Indexer(db) | 245 self.dex = Indexer(db) |
| 204 def tearDown(self): | 246 def tearDown(self): |
| 205 shutil.rmtree('test-index') | 247 IndexerTest.tearDown(self) |
| 206 | 248 |
| 207 class RDBMSIndexerTest(object): | 249 class RDBMSIndexerTest(object): |
| 208 def setUp(self): | 250 def setUp(self): |
| 209 # remove previous test, ignore errors | 251 # remove previous test, ignore errors |
| 210 if os.path.exists(config.DATABASE): | 252 if os.path.exists(config.DATABASE): |
| 228 postgresqlOpener.tearDown(self) | 270 postgresqlOpener.tearDown(self) |
| 229 | 271 |
| 230 | 272 |
| 231 @skip_postgresql | 273 @skip_postgresql |
| 232 class postgresqlFtsIndexerTest(postgresqlOpener, RDBMSIndexerTest, IndexerTest): | 274 class postgresqlFtsIndexerTest(postgresqlOpener, RDBMSIndexerTest, IndexerTest): |
| 275 | |
| 276 indexer_name = "native-fts" | |
| 277 | |
| 233 def setUp(self): | 278 def setUp(self): |
| 234 postgresqlOpener.setUp(self) | 279 postgresqlOpener.setUp(self) |
| 235 RDBMSIndexerTest.setUp(self) | 280 RDBMSIndexerTest.setUp(self) |
| 236 from roundup.backends.indexer_postgresql_fts import Indexer | 281 from roundup.backends.indexer_postgresql_fts import Indexer |
| 237 self.dex = Indexer(self.db) | 282 self.dex = Indexer(self.db) |
| 238 self.dex.db = self.db | 283 self.dex.db = self.db |
| 239 | 284 |
| 240 def tearDown(self): | 285 def tearDown(self): |
| 241 RDBMSIndexerTest.tearDown(self) | 286 RDBMSIndexerTest.tearDown(self) |
| 242 postgresqlOpener.tearDown(self) | 287 postgresqlOpener.tearDown(self) |
| 243 | |
| 244 def test_get_indexer(self): | |
| 245 def class_name_of(object): | |
| 246 """ take and object and return just the class name. | |
| 247 So in: | |
| 248 | |
| 249 return the class name before "at". | |
| 250 | |
| 251 """ | |
| 252 return(str(object).split()[0]) | |
| 253 | |
| 254 from roundup.backends.indexer_common import get_indexer | |
| 255 old_indexer = self.db.config['INDEXER'] | |
| 256 self.db.config['INDEXER'] = 'native-fts' | |
| 257 | |
| 258 get_indexer(self.db.config, self.db) | |
| 259 | |
| 260 self.assertEqual(class_name_of(self.dex), | |
| 261 class_name_of(get_indexer(self.db.config, self.db))) | |
| 262 | |
| 263 self.db.config['INDEXER'] = old_indexer | |
| 264 | 288 |
| 265 def test_websearch_syntax(self): | 289 def test_websearch_syntax(self): |
| 266 """Test searches using websearch_to_tsquery. These never throw | 290 """Test searches using websearch_to_tsquery. These never throw |
| 267 errors regardless of how wacky the input. | 291 errors regardless of how wacky the input. |
| 268 """ | 292 """ |
| 469 | 493 |
| 470 class sqliteIndexerTest(sqliteOpener, RDBMSIndexerTest, IndexerTest): | 494 class sqliteIndexerTest(sqliteOpener, RDBMSIndexerTest, IndexerTest): |
| 471 pass | 495 pass |
| 472 | 496 |
| 473 class sqliteFtsIndexerTest(sqliteOpener, RDBMSIndexerTest, IndexerTest): | 497 class sqliteFtsIndexerTest(sqliteOpener, RDBMSIndexerTest, IndexerTest): |
| 498 | |
| 499 indexer_name = "native-fts" | |
| 500 | |
| 474 def setUp(self): | 501 def setUp(self): |
| 475 RDBMSIndexerTest.setUp(self) | 502 RDBMSIndexerTest.setUp(self) |
| 476 from roundup.backends.indexer_sqlite_fts import Indexer | 503 from roundup.backends.indexer_sqlite_fts import Indexer |
| 477 self.dex = Indexer(self.db) | 504 self.dex = Indexer(self.db) |
| 478 self.dex.db = self.db | 505 self.dex.db = self.db |
| 479 | |
| 480 def test_get_indexer(self): | |
| 481 def class_name_of(object): | |
| 482 """ take and object and return just the class name. | |
| 483 So in: | |
| 484 | |
| 485 return the class name before "at". | |
| 486 | |
| 487 """ | |
| 488 return(str(object).split()[0]) | |
| 489 | |
| 490 from roundup.backends.indexer_common import get_indexer | |
| 491 old_indexer = 'native-fts' | |
| 492 self.db.config['INDEXER'] = 'native-fts' | |
| 493 | |
| 494 get_indexer(self.db.config, self.db) | |
| 495 | |
| 496 self.assertEqual(class_name_of(self.dex), | |
| 497 class_name_of(get_indexer(self.db.config, self.db))) | |
| 498 | |
| 499 self.db.config['INDEXER'] = old_indexer | |
| 500 | 506 |
| 501 def test_phrase_and_near(self): | 507 def test_phrase_and_near(self): |
| 502 self.dex.add_text(('test', '1', 'foo'), 'a the hello world') | 508 self.dex.add_text(('test', '1', 'foo'), 'a the hello world') |
| 503 self.dex.add_text(('test', '2', 'foo'), 'helh blah blah the world') | 509 self.dex.add_text(('test', '2', 'foo'), 'helh blah blah the world') |
| 504 self.dex.add_text(('test', '3', 'foo'), 'blah hello the world') | 510 self.dex.add_text(('test', '3', 'foo'), 'blah hello the world') |
