home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 July / maximum-cd-2011-07.iso / DiscContents / LibO_3.3.2_Win_x86_install_multi.exe / libreoffice1.cab / test_whichdb.py < prev    next >
Encoding:
Python Source  |  2011-03-15  |  1.6 KB  |  66 lines

  1. #! /usr/bin/env python
  2. """Test script for the whichdb module
  3.    based on test_anydbm.py
  4. """
  5.  
  6. import os
  7. import test.test_support
  8. import unittest
  9. import whichdb
  10. import anydbm
  11. import glob
  12.  
  13. _fname = test.test_support.TESTFN
  14.  
  15. def _delete_files():
  16.     # we don't know the precise name the underlying database uses
  17.     # so we use glob to locate all names
  18.     for f in glob.glob(_fname + "*"):
  19.         try:
  20.             os.unlink(f)
  21.         except OSError:
  22.             pass
  23.  
  24. class WhichDBTestCase(unittest.TestCase):
  25.     # Actual test methods are added to namespace
  26.     # after class definition.
  27.     def __init__(self, *args):
  28.         unittest.TestCase.__init__(self, *args)
  29.  
  30.     def tearDown(self):
  31.         _delete_files()
  32.  
  33.     def setUp(self):
  34.         _delete_files()
  35.  
  36. for name in anydbm._names:
  37.     # we define a new test method for each
  38.     # candidate database module.
  39.     try:
  40.         mod = __import__(name)
  41.     except ImportError:
  42.         continue
  43.  
  44.     def test_whichdb_name(self, name=name, mod=mod):
  45.         # Check whether whichdb correctly guesses module name
  46.         # for databases opened with module mod.
  47.         # Try with empty files first
  48.         f = mod.open(_fname, 'c')
  49.         f.close()
  50.         self.assertEqual(name, whichdb.whichdb(_fname))
  51.         # Now add a key
  52.         f = mod.open(_fname, 'w')
  53.         f["1"] = "1"
  54.         f.close()
  55.         self.assertEqual(name, whichdb.whichdb(_fname))
  56.     setattr(WhichDBTestCase,"test_whichdb_%s" % name, test_whichdb_name)
  57.  
  58. def test_main():
  59.     try:
  60.         test.test_support.run_unittest(WhichDBTestCase)
  61.     finally:
  62.         _delete_files()
  63.  
  64. if __name__ == "__main__":
  65.     test_main()
  66.