Brett Cannon | 065f7b8 | 2003-05-13 06:42:59 +0000 | [diff] [blame] | 1 | """Tests for the bsddb185 module. |
Skip Montanaro | 46f4e79 | 2003-05-06 20:36:57 +0000 | [diff] [blame] | 2 | |
Brett Cannon | 065f7b8 | 2003-05-13 06:42:59 +0000 | [diff] [blame] | 3 | The file 185test.db found in Lib/test/ is for testing purposes with this |
| 4 | testing suite. |
| 5 | |
| 6 | """ |
Brett Cannon | 768d44f | 2008-05-10 02:47:54 +0000 | [diff] [blame] | 7 | from test.test_support import run_unittest, findfile, import_module |
Skip Montanaro | 46f4e79 | 2003-05-06 20:36:57 +0000 | [diff] [blame] | 8 | import unittest |
Brett Cannon | 768d44f | 2008-05-10 02:47:54 +0000 | [diff] [blame] | 9 | bsddb185 = import_module('bsddb185', deprecated=True) |
Skip Montanaro | 46f4e79 | 2003-05-06 20:36:57 +0000 | [diff] [blame] | 10 | import anydbm |
| 11 | import whichdb |
| 12 | import os |
| 13 | import tempfile |
Skip Montanaro | ab0053a | 2003-05-17 02:54:11 +0000 | [diff] [blame] | 14 | import shutil |
Skip Montanaro | 46f4e79 | 2003-05-06 20:36:57 +0000 | [diff] [blame] | 15 | |
| 16 | class Bsddb185Tests(unittest.TestCase): |
Brett Cannon | 065f7b8 | 2003-05-13 06:42:59 +0000 | [diff] [blame] | 17 | |
Skip Montanaro | 46f4e79 | 2003-05-06 20:36:57 +0000 | [diff] [blame] | 18 | def test_open_existing_hash(self): |
Brett Cannon | 065f7b8 | 2003-05-13 06:42:59 +0000 | [diff] [blame] | 19 | # Verify we can open a file known to be a hash v2 file |
Skip Montanaro | 46f4e79 | 2003-05-06 20:36:57 +0000 | [diff] [blame] | 20 | db = bsddb185.hashopen(findfile("185test.db")) |
| 21 | self.assertEqual(db["1"], "1") |
| 22 | db.close() |
| 23 | |
| 24 | def test_whichdb(self): |
Brett Cannon | 065f7b8 | 2003-05-13 06:42:59 +0000 | [diff] [blame] | 25 | # Verify that whichdb correctly sniffs the known hash v2 file |
Skip Montanaro | 46f4e79 | 2003-05-06 20:36:57 +0000 | [diff] [blame] | 26 | self.assertEqual(whichdb.whichdb(findfile("185test.db")), "bsddb185") |
| 27 | |
| 28 | def test_anydbm_create(self): |
Brett Cannon | 065f7b8 | 2003-05-13 06:42:59 +0000 | [diff] [blame] | 29 | # Verify that anydbm.open does *not* create a bsddb185 file |
Skip Montanaro | 46f4e79 | 2003-05-06 20:36:57 +0000 | [diff] [blame] | 30 | tmpdir = tempfile.mkdtemp() |
| 31 | try: |
Skip Montanaro | ab0053a | 2003-05-17 02:54:11 +0000 | [diff] [blame] | 32 | dbfile = os.path.join(tmpdir, "foo.db") |
| 33 | anydbm.open(dbfile, "c").close() |
| 34 | ftype = whichdb.whichdb(dbfile) |
| 35 | self.assertNotEqual(ftype, "bsddb185") |
Skip Montanaro | 46f4e79 | 2003-05-06 20:36:57 +0000 | [diff] [blame] | 36 | finally: |
Skip Montanaro | ab0053a | 2003-05-17 02:54:11 +0000 | [diff] [blame] | 37 | shutil.rmtree(tmpdir) |
Skip Montanaro | 46f4e79 | 2003-05-06 20:36:57 +0000 | [diff] [blame] | 38 | |
| 39 | def test_main(): |
| 40 | run_unittest(Bsddb185Tests) |
| 41 | |
| 42 | if __name__ == "__main__": |
| 43 | test_main() |