Package mvpa :: Package tests :: Module test_externals
[hide private]
[frames] | no frames]

Source Code for Module mvpa.tests.test_externals

 1  # emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*- 
 2  # vi: set ft=python sts=4 ts=4 sw=4 et: 
 3  ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ## 
 4  # 
 5  #   See COPYING file distributed along with the PyMVPA package for the 
 6  #   copyright and license terms. 
 7  # 
 8  ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ## 
 9  """Test externals checking""" 
10   
11  import unittest 
12   
13  from mvpa import cfg 
14  from mvpa.base import externals 
15  from mvpa.support import copy 
16   
17 -class TestExternals(unittest.TestCase):
18
19 - def setUp(self):
20 self.backup = [] 21 # paranoid check 22 self.cfgstr = str(cfg) 23 # clean up externals cfg for proper testing 24 if cfg.has_section('externals'): 25 self.backup = copy.deepcopy(cfg.items('externals')) 26 cfg.remove_section('externals')
27 28
29 - def tearDown(self):
30 if len(self.backup): 31 # wipe existing one completely 32 if cfg.has_section('externals'): 33 cfg.remove_section('externals') 34 cfg.add_section('externals') 35 for o,v in self.backup: 36 cfg.set('externals', o,v) 37 # paranoid check 38 # since order can't be guaranteed, lets check 39 # each item after sorting 40 self.failUnlessEqual(sorted(self.cfgstr.split('\n')), 41 sorted(str(cfg).split('\n')))
42 43
44 - def testExternals(self):
45 self.failUnlessRaises(ValueError, externals.exists, 'BoGuS')
46 47
49 # no external should be checking twice (unless specified 50 # explicitely) 51 52 class Checker(object): 53 """Helper class to increment count of actual checks""" 54 def __init__(self): self.checked = 0 55 def check(self): self.checked += 1
56 57 checker = Checker() 58 59 externals._KNOWN['checker'] = 'checker.check()' 60 externals.__dict__['checker'] = checker 61 externals.exists('checker') 62 self.failUnlessEqual(checker.checked, 1) 63 externals.exists('checker') 64 self.failUnlessEqual(checker.checked, 1) 65 externals.exists('checker', force=True) 66 self.failUnlessEqual(checker.checked, 2) 67 externals.exists('checker') 68 self.failUnlessEqual(checker.checked, 2) 69 70 # restore original externals 71 externals.__dict__.pop('checker') 72 externals._KNOWN.pop('checker')
73 74
75 - def testExternalsCorrect2ndInvocation(self):
76 # always fails 77 externals._KNOWN['checker2'] = 'raise ImportError' 78 79 self.failUnless(not externals.exists('checker2'), 80 msg="Should be False on 1st invocation") 81 82 self.failUnless(not externals.exists('checker2'), 83 msg="Should be False on 2nd invocation as well") 84 85 externals._KNOWN.pop('checker2')
86 87 88
89 -def suite():
90 return unittest.makeSuite(TestExternals)
91 92 93 if __name__ == '__main__': 94 import runner 95