Skip to content

Commit 0c2929a

Browse files
committed
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.
1 parent 7485b0d commit 0c2929a

File tree

2 files changed

+100
-19
lines changed

2 files changed

+100
-19
lines changed

roundup/configuration.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1881,13 +1881,8 @@ def validator(self, options):
18811881
languages = textwrap.fill(_("Valid languages: ") +
18821882
lang_avail, 75,
18831883
subsequent_indent=" ")
1884-
raise ValueError(
1885-
_("Invalid indexer_language '%(lang)s' in config.ini for xapian indexer\n\n"
1886-
"%(valid)s") % {
1887-
"lang": lang,
1888-
"valid": languages
1889-
}
1890-
)
1884+
raise OptionValueError( options["INDEXER_LANGUAGE"],
1885+
lang, languages)
18911886

18921887
def load(self, home_dir):
18931888
"""Load configuration from path designated by home_dir argument"""

test/test_config.py

Lines changed: 98 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,10 @@ def testConfigSave(self):
188188
config.save() # creates .bak file
189189
self.assertTrue(os.access("config.ini", os.F_OK))
190190
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))
191195

192196
self.assertFalse(os.access("foo.bar", os.F_OK))
193197
self.assertFalse(os.access("foo.bak", os.F_OK))
@@ -243,6 +247,59 @@ def testFloatAndInt_with_update_option(self):
243247
self.assertAlmostEqual(config['WEB_LOGIN_ATTEMPTS_MIN'], 3.1415926,
244248
places=6)
245249

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')))
302+
246303

247304
class TrackerConfig(unittest.TestCase):
248305

@@ -309,8 +366,11 @@ def testNoDBInConfig(self):
309366
self.munge_configini(mods=[ ("backend = ", None) ])
310367

311368
# this should fail as backend isn't defined.
312-
self.assertRaises(configuration.OptionUnsetError, instance.open,
313-
self.dirname)
369+
with self.assertRaises(configuration.OptionUnsetError) as cm:
370+
instance.open(self.dirname)
371+
372+
self.assertEqual("RDBMS_BACKEND is not set"
373+
" and has no default", cm.exception.__str__())
314374

315375
def testInvalidIndexerLanguage_w_empty(self):
316376
""" make sure we have a reasonable error message if
@@ -323,19 +383,17 @@ def testInvalidIndexerLanguage_w_empty(self):
323383
("indexer_language = ", "NO_LANG") ])
324384

325385
config = configuration.CoreConfig()
326-
327-
# Note this should raise OptionValueError, but
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:
386+
387+
with self.assertRaises(configuration.OptionValueError) as cm:
331388
config.load(self.dirname)
332389

333390
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))
335393
# look for failing language
336-
self.assertIn("NO_LANG", cm.exception.args[0])
394+
self.assertIn("NO_LANG", cm.exception.args[1])
337395
# look for supported language
338-
self.assertIn("english", cm.exception.args[0])
396+
self.assertIn("english", cm.exception.args[2])
339397

340398
def testInvalidIndexerLanguage_xapian_missing(self):
341399
"""Using default path for indexers, make import of xapian
@@ -390,7 +448,7 @@ def testInvalidIndexerLanguage_w_xapian(self):
390448
self.munge_configini(mods=[ ("indexer = ", "xapian"),
391449
("indexer_language = ", "NO_LANG") ])
392450

393-
with self.assertRaises(ValueError) as cm:
451+
with self.assertRaises(configuration.OptionValueError) as cm:
394452
config.load(self.dirname)
395453
# don't test exception content. Done in
396454
# testInvalidIndexerLanguage_w_empty
@@ -437,6 +495,27 @@ def testLoadConfigNoConfig(self):
437495
print(cm.exception)
438496
self.assertEqual(cm.exception.args[0], self.dirname)
439497

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+
440519
def testInvalidIndexerValue(self):
441520
""" Mistype native indexer. Verify exception is
442521
generated.
@@ -453,5 +532,12 @@ def testInvalidIndexerValue(self):
453532
# look for failing value
454533
self.assertEqual("nati", cm.exception.args[1])
455534
# look for supported values
456-
self.assertIn("whoosh", cm.exception.args[2])
535+
self.assertIn("'whoosh'", cm.exception.args[2])
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+
457543

0 commit comments

Comments
 (0)