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