Skip to content

Commit 7813515

Browse files
committed
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.
1 parent 6563d39 commit 7813515

File tree

1 file changed

+109
-25
lines changed

1 file changed

+109
-25
lines changed

test/test_config.py

Lines changed: 109 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import unittest
1919
import logging
20+
import fileinput
2021

2122
import os, shutil, errno
2223

@@ -244,9 +245,6 @@ def testFloatAndInt_with_update_option(self):
244245

245246

246247
class TrackerConfig(unittest.TestCase):
247-
""" Arguably this should be tested in test_instance since it is triggered
248-
by instance.open. But it raises an error in the configuration module
249-
with a missing required param in config.ini."""
250248

251249
backend = 'anydbm'
252250

@@ -269,33 +267,60 @@ def tearDown(self):
269267
except OSError as error:
270268
if error.errno not in (errno.ENOENT, errno.ESRCH): raise
271269

270+
def munge_configini(self, mods = None):
271+
""" modify config.ini to meet testing requirements
272+
273+
mods is a list of tuples:
274+
[ ( "a = ", "b" ), ("c = ", None) ]
275+
Match line with first tuple element e.g. "a = ". Note specify
276+
trailing "=" and space to delimit keyword and properly format
277+
replacement line. If first tuple element matches, the line is
278+
replaced with the concatenation of the first and second elements.
279+
If second element is None ("" doesn't work), the line will be
280+
deleted.
281+
282+
Note the key/first element of tuple must be unique in config.ini.
283+
It is possible to have duplicates in different sections. This
284+
method doesn't handle that. TBD option third element of tuple
285+
defining section if needed.
286+
"""
287+
288+
if mods is None:
289+
return
272290

273-
def testNoDBInConfig(self):
274-
# comment out the backend key in config.ini
275-
import fileinput
276291
for line in fileinput.input(os.path.join(self.dirname, "config.ini"),
277292
inplace=True):
278-
if line.startswith("backend = "):
279-
continue
280-
print(line)
293+
for match, value in mods:
294+
if line.startswith(match):
295+
if value is not None:
296+
print(match + value)
297+
break
298+
else:
299+
print(line[:-1]) # remove trailing \n
300+
301+
def testNoDBInConfig(self):
302+
"""Arguably this should be tested in test_instance since it is
303+
triggered by instance.open. But it raises an error in the
304+
configuration module with a missing required param in
305+
config.ini.
306+
"""
307+
308+
# remove the backend key in config.ini
309+
self.munge_configini(mods=[ ("backend = ", None) ])
281310

282311
# this should fail as backend isn't defined.
283312
self.assertRaises(configuration.OptionUnsetError, instance.open,
284313
self.dirname)
285314

286-
287-
def testInvalidIndexer_language(self):
315+
def testInvalidIndexerLanguage_w_empty(self):
288316
""" make sure we have a reasonable error message if
289-
invalid language is specified """
317+
invalid indexer language is specified. This uses
318+
default search path for indexers.
319+
"""
290320

291-
# change the indexer_language value to an invalid value.
292-
import fileinput
293-
for line in fileinput.input(os.path.join(self.dirname, "config.ini"),
294-
inplace=True):
295-
if line.startswith("indexer_language = "):
296-
print("indexer_language = NO_LANG")
297-
continue
298-
print(line[:-1]) # remove trailing \n
321+
# SETUP: set indexer_language value to an invalid value.
322+
self.munge_configini(mods=[ ("indexer = ", ""),
323+
("indexer_language = ", "NO_LANG") ])
299324

300325
config = configuration.CoreConfig()
301326

@@ -312,25 +337,84 @@ def testInvalidIndexer_language(self):
312337
# look for supported language
313338
self.assertIn("english", cm.exception.args[0])
314339

340+
def testInvalidIndexerLanguage_xapian_missing(self):
341+
"""Using default path for indexers, make import of xapian
342+
fail and prevent exception from happening even though
343+
the indexer_language would be invalid for xapian.
344+
"""
345+
346+
print("Testing xapian not loadable")
347+
348+
# SETUP: same as testInvalidIndexerLanguage_w_empty
349+
self.munge_configini(mods=[ ("indexer = ", ""),
350+
("indexer_language = ", "NO_LANG") ])
351+
352+
import sys
353+
# Set module to Non to prevent xapian from loading
354+
sys.modules['xapian'] = None
355+
config.load(self.dirname)
356+
357+
# need to delete both to make python2 not error finding _xapian
358+
del(sys.modules['xapian'])
359+
del(sys.modules['xapian._xapian'])
360+
361+
self.assertEqual(config['INDEXER_LANGUAGE'], 'NO_LANG')
362+
363+
# do a reset here to test reset rather than wasting cycles
364+
# to do setup in a different test
365+
config.reset()
366+
self.assertEqual(config['INDEXER_LANGUAGE'], 'english')
367+
368+
def testInvalidIndexerLanguage_w_native(self):
369+
"""indexer_language is invalid but indexer is not "" or xapian
370+
Config load should succeed without exception.
371+
"""
372+
373+
print("Testing indexer = native")
374+
375+
self.munge_configini(mods = [ ("indexer = ", "native"),
376+
("indexer_language = ", "NO_LANG") ])
377+
378+
config.load(self.dirname)
379+
380+
self.assertEqual(config['HTML_VERSION'], 'html4')
381+
self.assertEqual(config['INDEXER_LANGUAGE'], 'NO_LANG')
382+
383+
def testInvalidIndexerLanguage_w_xapian(self):
384+
""" Use explicit xapian indexer. VAerify exception is
385+
generated.
386+
"""
387+
388+
print("Testing explicit xapian")
389+
390+
self.munge_configini(mods=[ ("indexer = ", "xapian"),
391+
("indexer_language = ", "NO_LANG") ])
392+
393+
with self.assertRaises(ValueError) as cm:
394+
config.load(self.dirname)
395+
# don't test exception content. Done in
396+
# testInvalidIndexerLanguage_w_empty
397+
# if exception not generated assertRaises
398+
# will generate failure.
399+
315400
def testLoadConfig(self):
316401
""" run load to validate config """
317402

318403
config = configuration.CoreConfig()
319404

320405
config.load(self.dirname)
321406

407+
# test various ways of accessing config data
322408
with self.assertRaises(configuration.InvalidOptionError) as cm:
409+
# using lower case name fails
323410
c = config['indexer_language']
324411
print(cm.exception)
325412
self.assertIn("indexer_language", repr(cm.exception))
326413

414+
# uppercase name passes as does tuple index for setting in main
327415
self.assertEqual(config['HTML_VERSION'], 'html4')
328416
self.assertEqual(config[('main', 'html_version')], 'html4')
329417

418+
# uppercase name passes as does tuple index for setting in web
330419
self.assertEqual(config['WEB_COOKIE_TAKES_PRECEDENCE'], 0)
331420
self.assertEqual(config[('web','cookie_takes_precedence')], 0)
332-
333-
334-
335-
336-

0 commit comments

Comments
 (0)