Mercurial > p > roundup > code
diff test/test_config.py @ 6357:c985ed52ca2d
Add testcases for invalid indexer_language
Set invalid indexer_language and test:
indexer = "" and xapian not able to be loaded -> no exception
indexer = native -> no exception
indexer = xapian -> ValueError exception
Also test reset to verify that indexer_language is reset to english
from NO_LANG.
Also create helper method to munge config.ini for each test case.
Changed all test to use it.
Also moved doc from class to the test that it described.
| author | John Rouillard <rouilj@ieee.org> |
|---|---|
| date | Tue, 30 Mar 2021 02:10:00 -0400 |
| parents | c26b9ce33ae3 |
| children | 42db48c30d31 |
line wrap: on
line diff
--- a/test/test_config.py Mon Mar 29 22:47:54 2021 -0400 +++ b/test/test_config.py Tue Mar 30 02:10:00 2021 -0400 @@ -17,6 +17,7 @@ import unittest import logging +import fileinput import os, shutil, errno @@ -244,9 +245,6 @@ class TrackerConfig(unittest.TestCase): - """ Arguably this should be tested in test_instance since it is triggered - by instance.open. But it raises an error in the configuration module - with a missing required param in config.ini.""" backend = 'anydbm' @@ -269,33 +267,60 @@ except OSError as error: if error.errno not in (errno.ENOENT, errno.ESRCH): raise + def munge_configini(self, mods = None): + """ modify config.ini to meet testing requirements + + mods is a list of tuples: + [ ( "a = ", "b" ), ("c = ", None) ] + Match line with first tuple element e.g. "a = ". Note specify + trailing "=" and space to delimit keyword and properly format + replacement line. If first tuple element matches, the line is + replaced with the concatenation of the first and second elements. + If second element is None ("" doesn't work), the line will be + deleted. + + Note the key/first element of tuple must be unique in config.ini. + It is possible to have duplicates in different sections. This + method doesn't handle that. TBD option third element of tuple + defining section if needed. + """ + + if mods is None: + return + + for line in fileinput.input(os.path.join(self.dirname, "config.ini"), + inplace=True): + for match, value in mods: + if line.startswith(match): + if value is not None: + print(match + value) + break + else: + print(line[:-1]) # remove trailing \n def testNoDBInConfig(self): - # comment out the backend key in config.ini - import fileinput - for line in fileinput.input(os.path.join(self.dirname, "config.ini"), - inplace=True): - if line.startswith("backend = "): - continue - print(line) + """Arguably this should be tested in test_instance since it is + triggered by instance.open. But it raises an error in the + configuration module with a missing required param in + config.ini. + """ + + # remove the backend key in config.ini + self.munge_configini(mods=[ ("backend = ", None) ]) # this should fail as backend isn't defined. self.assertRaises(configuration.OptionUnsetError, instance.open, self.dirname) - - def testInvalidIndexer_language(self): + def testInvalidIndexerLanguage_w_empty(self): """ make sure we have a reasonable error message if - invalid language is specified """ + invalid indexer language is specified. This uses + default search path for indexers. + """ - # change the indexer_language value to an invalid value. - import fileinput - for line in fileinput.input(os.path.join(self.dirname, "config.ini"), - inplace=True): - if line.startswith("indexer_language = "): - print("indexer_language = NO_LANG") - continue - print(line[:-1]) # remove trailing \n + # SETUP: set indexer_language value to an invalid value. + self.munge_configini(mods=[ ("indexer = ", ""), + ("indexer_language = ", "NO_LANG") ]) config = configuration.CoreConfig() @@ -312,6 +337,66 @@ # look for supported language self.assertIn("english", cm.exception.args[0]) + def testInvalidIndexerLanguage_xapian_missing(self): + """Using default path for indexers, make import of xapian + fail and prevent exception from happening even though + the indexer_language would be invalid for xapian. + """ + + print("Testing xapian not loadable") + + # SETUP: same as testInvalidIndexerLanguage_w_empty + self.munge_configini(mods=[ ("indexer = ", ""), + ("indexer_language = ", "NO_LANG") ]) + + import sys + # Set module to Non to prevent xapian from loading + sys.modules['xapian'] = None + config.load(self.dirname) + + # need to delete both to make python2 not error finding _xapian + del(sys.modules['xapian']) + del(sys.modules['xapian._xapian']) + + self.assertEqual(config['INDEXER_LANGUAGE'], 'NO_LANG') + + # do a reset here to test reset rather than wasting cycles + # to do setup in a different test + config.reset() + self.assertEqual(config['INDEXER_LANGUAGE'], 'english') + + def testInvalidIndexerLanguage_w_native(self): + """indexer_language is invalid but indexer is not "" or xapian + Config load should succeed without exception. + """ + + print("Testing indexer = native") + + self.munge_configini(mods = [ ("indexer = ", "native"), + ("indexer_language = ", "NO_LANG") ]) + + config.load(self.dirname) + + self.assertEqual(config['HTML_VERSION'], 'html4') + self.assertEqual(config['INDEXER_LANGUAGE'], 'NO_LANG') + + def testInvalidIndexerLanguage_w_xapian(self): + """ Use explicit xapian indexer. VAerify exception is + generated. + """ + + print("Testing explicit xapian") + + self.munge_configini(mods=[ ("indexer = ", "xapian"), + ("indexer_language = ", "NO_LANG") ]) + + with self.assertRaises(ValueError) as cm: + config.load(self.dirname) + # don't test exception content. Done in + # testInvalidIndexerLanguage_w_empty + # if exception not generated assertRaises + # will generate failure. + def testLoadConfig(self): """ run load to validate config """ @@ -319,18 +404,17 @@ config.load(self.dirname) + # test various ways of accessing config data with self.assertRaises(configuration.InvalidOptionError) as cm: + # using lower case name fails c = config['indexer_language'] print(cm.exception) self.assertIn("indexer_language", repr(cm.exception)) + # uppercase name passes as does tuple index for setting in main self.assertEqual(config['HTML_VERSION'], 'html4') self.assertEqual(config[('main', 'html_version')], 'html4') + # uppercase name passes as does tuple index for setting in web self.assertEqual(config['WEB_COOKIE_TAKES_PRECEDENCE'], 0) self.assertEqual(config[('web','cookie_takes_precedence')], 0) - - - - -
