Guido van Rossum | 2f1064c | 2001-01-18 16:46:52 +0000 | [diff] [blame] | 1 | #! /usr/bin/env python |
| 2 | """Test script for the dumbdbm module |
| 3 | Original by Roger E. Masse |
| 4 | """ |
| 5 | |
Guido van Rossum | 2f1064c | 2001-01-18 16:46:52 +0000 | [diff] [blame] | 6 | import os |
Barry Warsaw | 3ca656f | 2001-11-13 20:16:52 +0000 | [diff] [blame] | 7 | import test_support |
| 8 | import unittest |
| 9 | import dumbdbm |
| 10 | import tempfile |
Guido van Rossum | 2f1064c | 2001-01-18 16:46:52 +0000 | [diff] [blame] | 11 | |
Tim Peters | d9fbf35 | 2002-01-30 07:32:53 +0000 | [diff] [blame] | 12 | _fname = tempfile.mktemp() |
| 13 | |
| 14 | def _delete_files(): |
| 15 | for ext in [".dir", ".dat", ".bak"]: |
| 16 | try: |
| 17 | os.unlink(_fname + ext) |
| 18 | except OSError: |
| 19 | pass |
| 20 | |
Barry Warsaw | 3ca656f | 2001-11-13 20:16:52 +0000 | [diff] [blame] | 21 | class DumbDBMTestCase(unittest.TestCase): |
Barry Warsaw | 3ca656f | 2001-11-13 20:16:52 +0000 | [diff] [blame] | 22 | _dict = {'0': '', |
| 23 | 'a': 'Python:', |
| 24 | 'b': 'Programming', |
| 25 | 'c': 'the', |
| 26 | 'd': 'way', |
| 27 | 'f': 'Guido', |
| 28 | 'g': 'intended' |
| 29 | } |
| 30 | |
| 31 | def __init__(self, *args): |
| 32 | unittest.TestCase.__init__(self, *args) |
Tim Peters | 8876848 | 2001-11-13 21:51:26 +0000 | [diff] [blame] | 33 | |
Barry Warsaw | 3ca656f | 2001-11-13 20:16:52 +0000 | [diff] [blame] | 34 | def test_dumbdbm_creation(self): |
Tim Peters | d9fbf35 | 2002-01-30 07:32:53 +0000 | [diff] [blame] | 35 | f = dumbdbm.open(_fname, 'c') |
Barry Warsaw | 3ca656f | 2001-11-13 20:16:52 +0000 | [diff] [blame] | 36 | self.assertEqual(f.keys(), []) |
| 37 | for key in self._dict: |
| 38 | f[key] = self._dict[key] |
| 39 | self.read_helper(f) |
| 40 | f.close() |
| 41 | |
| 42 | def test_dumbdbm_modification(self): |
Skip Montanaro | c08fe82 | 2002-03-17 23:03:42 +0000 | [diff] [blame] | 43 | self.init_db() |
Tim Peters | d9fbf35 | 2002-01-30 07:32:53 +0000 | [diff] [blame] | 44 | f = dumbdbm.open(_fname, 'w') |
Barry Warsaw | 3ca656f | 2001-11-13 20:16:52 +0000 | [diff] [blame] | 45 | self._dict['g'] = f['g'] = "indented" |
| 46 | self.read_helper(f) |
| 47 | f.close() |
| 48 | |
| 49 | def test_dumbdbm_read(self): |
Skip Montanaro | c08fe82 | 2002-03-17 23:03:42 +0000 | [diff] [blame] | 50 | self.init_db() |
Tim Peters | d9fbf35 | 2002-01-30 07:32:53 +0000 | [diff] [blame] | 51 | f = dumbdbm.open(_fname, 'r') |
Barry Warsaw | 3ca656f | 2001-11-13 20:16:52 +0000 | [diff] [blame] | 52 | self.read_helper(f) |
| 53 | f.close() |
| 54 | |
| 55 | def test_dumbdbm_keys(self): |
Skip Montanaro | c08fe82 | 2002-03-17 23:03:42 +0000 | [diff] [blame] | 56 | self.init_db() |
Tim Peters | d9fbf35 | 2002-01-30 07:32:53 +0000 | [diff] [blame] | 57 | f = dumbdbm.open(_fname) |
Barry Warsaw | 3ca656f | 2001-11-13 20:16:52 +0000 | [diff] [blame] | 58 | keys = self.keys_helper(f) |
| 59 | f.close() |
| 60 | |
Skip Montanaro | c08fe82 | 2002-03-17 23:03:42 +0000 | [diff] [blame] | 61 | def test_write_write_read(self): |
| 62 | # test for bug #482460 |
| 63 | f = dumbdbm.open(_fname) |
| 64 | f['1'] = 'hello' |
| 65 | f['1'] = 'hello2' |
| 66 | f.close() |
| 67 | f = dumbdbm.open(_fname) |
| 68 | self.assertEqual(f['1'], 'hello2') |
| 69 | f.close() |
| 70 | |
Barry Warsaw | 3ca656f | 2001-11-13 20:16:52 +0000 | [diff] [blame] | 71 | def read_helper(self, f): |
| 72 | keys = self.keys_helper(f) |
| 73 | for key in self._dict: |
| 74 | self.assertEqual(self._dict[key], f[key]) |
Tim Peters | 8876848 | 2001-11-13 21:51:26 +0000 | [diff] [blame] | 75 | |
Skip Montanaro | c08fe82 | 2002-03-17 23:03:42 +0000 | [diff] [blame] | 76 | def init_db(self): |
| 77 | f = dumbdbm.open(_fname, 'w') |
| 78 | for k in self._dict: |
| 79 | f[k] = self._dict[k] |
| 80 | f.close() |
| 81 | |
Barry Warsaw | 3ca656f | 2001-11-13 20:16:52 +0000 | [diff] [blame] | 82 | def keys_helper(self, f): |
| 83 | keys = f.keys() |
| 84 | keys.sort() |
Skip Montanaro | c08fe82 | 2002-03-17 23:03:42 +0000 | [diff] [blame] | 85 | dkeys = self._dict.keys() |
| 86 | dkeys.sort() |
| 87 | self.assertEqual(keys, dkeys) |
Barry Warsaw | 3ca656f | 2001-11-13 20:16:52 +0000 | [diff] [blame] | 88 | return keys |
| 89 | |
Skip Montanaro | c08fe82 | 2002-03-17 23:03:42 +0000 | [diff] [blame] | 90 | def tearDown(self): |
| 91 | _delete_files() |
| 92 | |
| 93 | def setUp(self): |
| 94 | _delete_files() |
| 95 | |
Barry Warsaw | 3ca656f | 2001-11-13 20:16:52 +0000 | [diff] [blame] | 96 | def test_main(): |
Tim Peters | d9fbf35 | 2002-01-30 07:32:53 +0000 | [diff] [blame] | 97 | try: |
| 98 | test_support.run_unittest(DumbDBMTestCase) |
| 99 | finally: |
| 100 | _delete_files() |
Barry Warsaw | 3ca656f | 2001-11-13 20:16:52 +0000 | [diff] [blame] | 101 | |
| 102 | if __name__ == "__main__": |
| 103 | test_main() |