Mercurial > p > roundup > code
comparison test/test_config.py @ 6363:08e209a7f22b
Use OptionValueError for indexer_language error, add configuration tests
Added more tests for configuration.py. This is the last of the easy to
do tests for the module.
While doing that figured out how to get the proper arguments to
generate OptionValueError if indexer_language is invalid. Changed
implementation to generate same and updated test.
| author | John Rouillard <rouilj@ieee.org> |
|---|---|
| date | Tue, 30 Mar 2021 18:03:37 -0400 |
| parents | 2d42a308927b |
| children | 8a7de159d1a6 |
comparison
equal
deleted
inserted
replaced
| 6362:5f0463897afc | 6363:08e209a7f22b |
|---|---|
| 186 self.assertFalse(os.access("config.bak", os.F_OK)) | 186 self.assertFalse(os.access("config.bak", os.F_OK)) |
| 187 config.save() | 187 config.save() |
| 188 config.save() # creates .bak file | 188 config.save() # creates .bak file |
| 189 self.assertTrue(os.access("config.ini", os.F_OK)) | 189 self.assertTrue(os.access("config.ini", os.F_OK)) |
| 190 self.assertTrue(os.access("config.bak", os.F_OK)) | 190 self.assertTrue(os.access("config.bak", os.F_OK)) |
| 191 config.save() # trigger delete of old .bak file | |
| 192 # FIXME: this should test to see if a new .bak | |
| 193 # was created. For now verify .bak still exists | |
| 194 self.assertTrue(os.access("config.bak", os.F_OK)) | |
| 191 | 195 |
| 192 self.assertFalse(os.access("foo.bar", os.F_OK)) | 196 self.assertFalse(os.access("foo.bar", os.F_OK)) |
| 193 self.assertFalse(os.access("foo.bak", os.F_OK)) | 197 self.assertFalse(os.access("foo.bak", os.F_OK)) |
| 194 config.save("foo.bar") | 198 config.save("foo.bar") |
| 195 config.save("foo.bar") # creates .bak file | 199 config.save("foo.bar") # creates .bak file |
| 240 self.assertRaises(configuration.OptionValueError, | 244 self.assertRaises(configuration.OptionValueError, |
| 241 config._get_option('WEB_LOGIN_ATTEMPTS_MIN').set, "fred") | 245 config._get_option('WEB_LOGIN_ATTEMPTS_MIN').set, "fred") |
| 242 | 246 |
| 243 self.assertAlmostEqual(config['WEB_LOGIN_ATTEMPTS_MIN'], 3.1415926, | 247 self.assertAlmostEqual(config['WEB_LOGIN_ATTEMPTS_MIN'], 3.1415926, |
| 244 places=6) | 248 places=6) |
| 249 | |
| 250 # test removal of .0 on floats that are integers | |
| 251 self.assertEqual(None, | |
| 252 config._get_option('WEB_LOGIN_ATTEMPTS_MIN').set("3.0")) | |
| 253 | |
| 254 self.assertEqual("3", | |
| 255 config._get_option('WEB_LOGIN_ATTEMPTS_MIN')._value2str(3.00)) | |
| 256 | |
| 257 | |
| 258 def testOptionAsString(self): | |
| 259 | |
| 260 config = configuration.CoreConfig() | |
| 261 | |
| 262 config._get_option('WEB_LOGIN_ATTEMPTS_MIN').set("2552") | |
| 263 | |
| 264 v = config._get_option('WEB_LOGIN_ATTEMPTS_MIN').__str__() | |
| 265 print(v) | |
| 266 self.assertIn("55", v) | |
| 267 | |
| 268 v = config._get_option('WEB_LOGIN_ATTEMPTS_MIN').__repr__() | |
| 269 print(v) | |
| 270 self.assertIn("55", v) | |
| 271 | |
| 272 def testBooleanOption(self): | |
| 273 | |
| 274 config = configuration.CoreConfig() | |
| 275 | |
| 276 with self.assertRaises(configuration.OptionValueError) as cm: | |
| 277 config._get_option('INSTANT_REGISTRATION').set("3") | |
| 278 | |
| 279 # test multiple boolean representations | |
| 280 for b in [ "yes", "1", "true", "TRUE", "tRue", "on", | |
| 281 "oN", 1, True ]: | |
| 282 self.assertEqual(None, | |
| 283 config._get_option('INSTANT_REGISTRATION').set(b)) | |
| 284 self.assertEqual(1, | |
| 285 config._get_option('INSTANT_REGISTRATION').get()) | |
| 286 | |
| 287 for b in ["no", "0", "false", "FALSE", "fAlse", "off", | |
| 288 "oFf", 0, False]: | |
| 289 self.assertEqual(None, | |
| 290 config._get_option('INSTANT_REGISTRATION').set(b)) | |
| 291 self.assertEqual(0, | |
| 292 config._get_option('INSTANT_REGISTRATION').get()) | |
| 293 | |
| 294 def testOctalNumberOption(self): | |
| 295 | |
| 296 config = configuration.CoreConfig() | |
| 297 | |
| 298 with self.assertRaises(configuration.OptionValueError) as cm: | |
| 299 config._get_option('UMASK').set("xyzzy") | |
| 300 | |
| 301 print(type(config._get_option('UMASK'))) | |
| 245 | 302 |
| 246 | 303 |
| 247 class TrackerConfig(unittest.TestCase): | 304 class TrackerConfig(unittest.TestCase): |
| 248 | 305 |
| 249 backend = 'anydbm' | 306 backend = 'anydbm' |
| 307 | 364 |
| 308 # remove the backend key in config.ini | 365 # remove the backend key in config.ini |
| 309 self.munge_configini(mods=[ ("backend = ", None) ]) | 366 self.munge_configini(mods=[ ("backend = ", None) ]) |
| 310 | 367 |
| 311 # this should fail as backend isn't defined. | 368 # this should fail as backend isn't defined. |
| 312 self.assertRaises(configuration.OptionUnsetError, instance.open, | 369 with self.assertRaises(configuration.OptionUnsetError) as cm: |
| 313 self.dirname) | 370 instance.open(self.dirname) |
| 371 | |
| 372 self.assertEqual("RDBMS_BACKEND is not set" | |
| 373 " and has no default", cm.exception.__str__()) | |
| 314 | 374 |
| 315 def testInvalidIndexerLanguage_w_empty(self): | 375 def testInvalidIndexerLanguage_w_empty(self): |
| 316 """ make sure we have a reasonable error message if | 376 """ make sure we have a reasonable error message if |
| 317 invalid indexer language is specified. This uses | 377 invalid indexer language is specified. This uses |
| 318 default search path for indexers. | 378 default search path for indexers. |
| 321 # SETUP: set indexer_language value to an invalid value. | 381 # SETUP: set indexer_language value to an invalid value. |
| 322 self.munge_configini(mods=[ ("indexer = ", ""), | 382 self.munge_configini(mods=[ ("indexer = ", ""), |
| 323 ("indexer_language = ", "NO_LANG") ]) | 383 ("indexer_language = ", "NO_LANG") ]) |
| 324 | 384 |
| 325 config = configuration.CoreConfig() | 385 config = configuration.CoreConfig() |
| 326 | 386 |
| 327 # Note this should raise OptionValueError, but | 387 with self.assertRaises(configuration.OptionValueError) as cm: |
| 328 # the test fot this error occurs too late to have | |
| 329 # a valid option still available. So raise ValueError. | |
| 330 with self.assertRaises(ValueError) as cm: | |
| 331 config.load(self.dirname) | 388 config.load(self.dirname) |
| 332 | 389 |
| 333 print(cm.exception) | 390 print(cm.exception) |
| 334 self.assertIn("ValueError", repr(cm.exception)) | 391 # test repr. The type is right since it passed assertRaises. |
| 392 self.assertIn("OptionValueError", repr(cm.exception)) | |
| 335 # look for failing language | 393 # look for failing language |
| 336 self.assertIn("NO_LANG", cm.exception.args[0]) | 394 self.assertIn("NO_LANG", cm.exception.args[1]) |
| 337 # look for supported language | 395 # look for supported language |
| 338 self.assertIn("english", cm.exception.args[0]) | 396 self.assertIn("english", cm.exception.args[2]) |
| 339 | 397 |
| 340 def testInvalidIndexerLanguage_xapian_missing(self): | 398 def testInvalidIndexerLanguage_xapian_missing(self): |
| 341 """Using default path for indexers, make import of xapian | 399 """Using default path for indexers, make import of xapian |
| 342 fail and prevent exception from happening even though | 400 fail and prevent exception from happening even though |
| 343 the indexer_language would be invalid for xapian. | 401 the indexer_language would be invalid for xapian. |
| 388 print("Testing explicit xapian") | 446 print("Testing explicit xapian") |
| 389 | 447 |
| 390 self.munge_configini(mods=[ ("indexer = ", "xapian"), | 448 self.munge_configini(mods=[ ("indexer = ", "xapian"), |
| 391 ("indexer_language = ", "NO_LANG") ]) | 449 ("indexer_language = ", "NO_LANG") ]) |
| 392 | 450 |
| 393 with self.assertRaises(ValueError) as cm: | 451 with self.assertRaises(configuration.OptionValueError) as cm: |
| 394 config.load(self.dirname) | 452 config.load(self.dirname) |
| 395 # don't test exception content. Done in | 453 # don't test exception content. Done in |
| 396 # testInvalidIndexerLanguage_w_empty | 454 # testInvalidIndexerLanguage_w_empty |
| 397 # if exception not generated assertRaises | 455 # if exception not generated assertRaises |
| 398 # will generate failure. | 456 # will generate failure. |
| 435 config.load(self.dirname) | 493 config.load(self.dirname) |
| 436 | 494 |
| 437 print(cm.exception) | 495 print(cm.exception) |
| 438 self.assertEqual(cm.exception.args[0], self.dirname) | 496 self.assertEqual(cm.exception.args[0], self.dirname) |
| 439 | 497 |
| 498 def testCopyConfig(self): | |
| 499 | |
| 500 self.munge_configini(mods=[ ("html_version = ", "xhtml") ]) | |
| 501 | |
| 502 config = configuration.CoreConfig() | |
| 503 | |
| 504 # verify config is initalized to defaults | |
| 505 self.assertEqual(config['HTML_VERSION'], 'html4') | |
| 506 | |
| 507 # load config | |
| 508 config.load(self.dirname) | |
| 509 | |
| 510 # loaded new option | |
| 511 self.assertEqual(config['HTML_VERSION'], 'xhtml') | |
| 512 | |
| 513 # copy config | |
| 514 config_copy = config.copy() | |
| 515 | |
| 516 # this should work | |
| 517 self.assertEqual(config_copy['HTML_VERSION'], 'xhtml') | |
| 518 | |
| 440 def testInvalidIndexerValue(self): | 519 def testInvalidIndexerValue(self): |
| 441 """ Mistype native indexer. Verify exception is | 520 """ Mistype native indexer. Verify exception is |
| 442 generated. | 521 generated. |
| 443 """ | 522 """ |
| 444 | 523 |
| 451 | 530 |
| 452 self.assertIn("OptionValueError", repr(cm.exception)) | 531 self.assertIn("OptionValueError", repr(cm.exception)) |
| 453 # look for failing value | 532 # look for failing value |
| 454 self.assertEqual("nati", cm.exception.args[1]) | 533 self.assertEqual("nati", cm.exception.args[1]) |
| 455 # look for supported values | 534 # look for supported values |
| 456 self.assertIn("whoosh", cm.exception.args[2]) | 535 self.assertIn("'whoosh'", cm.exception.args[2]) |
| 457 | 536 |
| 537 # verify that args show up in string representaton | |
| 538 string_rep = cm.exception.__str__() | |
| 539 print(string_rep) | |
| 540 self.assertIn("nati", string_rep) | |
| 541 self.assertIn("'whoosh'", string_rep) | |
| 542 | |
| 543 |
