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 | |
Andrew M. Kuchling | dc26758 | 2006-12-22 15:04:45 +0000 | [diff] [blame] | 41 | def test_dumbdbm_creation_mode(self): |
| 42 | # On platforms without chmod, don't do anything. |
Andrew M. Kuchling | 9ef0ef5 | 2006-12-22 15:16:58 +0000 | [diff] [blame] | 43 | if not (hasattr(os, 'chmod') and hasattr(os, 'umask')): |
Andrew M. Kuchling | dc26758 | 2006-12-22 15:04:45 +0000 | [diff] [blame] | 44 | return |
| 45 | |
Andrew M. Kuchling | 9ef0ef5 | 2006-12-22 15:16:58 +0000 | [diff] [blame] | 46 | try: |
| 47 | old_umask = os.umask(0002) |
| 48 | f = dumbdbm.open(_fname, 'c', 0637) |
| 49 | f.close() |
| 50 | finally: |
| 51 | os.umask(old_umask) |
Tim Peters | f733abb | 2007-01-30 03:03:46 +0000 | [diff] [blame] | 52 | |
Neal Norwitz | 06a9a0c | 2007-01-18 06:20:55 +0000 | [diff] [blame] | 53 | expected_mode = 0635 |
| 54 | if os.name != 'posix': |
| 55 | # Windows only supports setting the read-only attribute. |
| 56 | # This shouldn't fail, but doesn't work like Unix either. |
| 57 | expected_mode = 0666 |
| 58 | |
Andrew M. Kuchling | dc26758 | 2006-12-22 15:04:45 +0000 | [diff] [blame] | 59 | import stat |
| 60 | st = os.stat(_fname + '.dat') |
Neal Norwitz | 06a9a0c | 2007-01-18 06:20:55 +0000 | [diff] [blame] | 61 | self.assertEqual(stat.S_IMODE(st.st_mode), expected_mode) |
Andrew M. Kuchling | dc26758 | 2006-12-22 15:04:45 +0000 | [diff] [blame] | 62 | st = os.stat(_fname + '.dir') |
Neal Norwitz | 06a9a0c | 2007-01-18 06:20:55 +0000 | [diff] [blame] | 63 | self.assertEqual(stat.S_IMODE(st.st_mode), expected_mode) |
Tim Peters | f733abb | 2007-01-30 03:03:46 +0000 | [diff] [blame] | 64 | |
Tim Peters | 7a6c733 | 2003-07-13 17:21:10 +0000 | [diff] [blame] | 65 | def test_close_twice(self): |
| 66 | f = dumbdbm.open(_fname) |
| 67 | f['a'] = 'b' |
| 68 | self.assertEqual(f['a'], 'b') |
| 69 | f.close() |
| 70 | f.close() |
| 71 | |
Barry Warsaw | 3ca656f | 2001-11-13 20:16:52 +0000 | [diff] [blame] | 72 | def test_dumbdbm_modification(self): |
Skip Montanaro | c08fe82 | 2002-03-17 23:03:42 +0000 | [diff] [blame] | 73 | self.init_db() |
Tim Peters | d9fbf35 | 2002-01-30 07:32:53 +0000 | [diff] [blame] | 74 | f = dumbdbm.open(_fname, 'w') |
Barry Warsaw | 3ca656f | 2001-11-13 20:16:52 +0000 | [diff] [blame] | 75 | self._dict['g'] = f['g'] = "indented" |
| 76 | self.read_helper(f) |
| 77 | f.close() |
| 78 | |
| 79 | def test_dumbdbm_read(self): |
Skip Montanaro | c08fe82 | 2002-03-17 23:03:42 +0000 | [diff] [blame] | 80 | self.init_db() |
Tim Peters | d9fbf35 | 2002-01-30 07:32:53 +0000 | [diff] [blame] | 81 | f = dumbdbm.open(_fname, 'r') |
Barry Warsaw | 3ca656f | 2001-11-13 20:16:52 +0000 | [diff] [blame] | 82 | self.read_helper(f) |
| 83 | f.close() |
| 84 | |
| 85 | def test_dumbdbm_keys(self): |
Skip Montanaro | c08fe82 | 2002-03-17 23:03:42 +0000 | [diff] [blame] | 86 | self.init_db() |
Tim Peters | d9fbf35 | 2002-01-30 07:32:53 +0000 | [diff] [blame] | 87 | f = dumbdbm.open(_fname) |
Barry Warsaw | 3ca656f | 2001-11-13 20:16:52 +0000 | [diff] [blame] | 88 | keys = self.keys_helper(f) |
| 89 | f.close() |
| 90 | |
Skip Montanaro | c08fe82 | 2002-03-17 23:03:42 +0000 | [diff] [blame] | 91 | def test_write_write_read(self): |
| 92 | # test for bug #482460 |
| 93 | f = dumbdbm.open(_fname) |
| 94 | f['1'] = 'hello' |
| 95 | f['1'] = 'hello2' |
| 96 | f.close() |
| 97 | f = dumbdbm.open(_fname) |
| 98 | self.assertEqual(f['1'], 'hello2') |
| 99 | f.close() |
| 100 | |
Andrew M. Kuchling | ecdad85 | 2005-06-07 19:36:10 +0000 | [diff] [blame] | 101 | def test_line_endings(self): |
| 102 | # test for bug #1172763: dumbdbm would die if the line endings |
| 103 | # weren't what was expected. |
| 104 | f = dumbdbm.open(_fname) |
| 105 | f['1'] = 'hello' |
| 106 | f['2'] = 'hello2' |
| 107 | f.close() |
| 108 | |
| 109 | # Mangle the file by adding \r before each newline |
| 110 | data = open(_fname + '.dir').read() |
| 111 | data = data.replace('\n', '\r\n') |
| 112 | open(_fname + '.dir', 'wb').write(data) |
Anthony Baxter | 95a2a4e | 2005-06-08 04:35:50 +0000 | [diff] [blame] | 113 | |
Andrew M. Kuchling | ecdad85 | 2005-06-07 19:36:10 +0000 | [diff] [blame] | 114 | f = dumbdbm.open(_fname) |
| 115 | self.assertEqual(f['1'], 'hello') |
| 116 | self.assertEqual(f['2'], 'hello2') |
Anthony Baxter | 95a2a4e | 2005-06-08 04:35:50 +0000 | [diff] [blame] | 117 | |
| 118 | |
Barry Warsaw | 3ca656f | 2001-11-13 20:16:52 +0000 | [diff] [blame] | 119 | def read_helper(self, f): |
| 120 | keys = self.keys_helper(f) |
| 121 | for key in self._dict: |
| 122 | self.assertEqual(self._dict[key], f[key]) |
Tim Peters | 8876848 | 2001-11-13 21:51:26 +0000 | [diff] [blame] | 123 | |
Skip Montanaro | c08fe82 | 2002-03-17 23:03:42 +0000 | [diff] [blame] | 124 | def init_db(self): |
| 125 | f = dumbdbm.open(_fname, 'w') |
| 126 | for k in self._dict: |
| 127 | f[k] = self._dict[k] |
| 128 | f.close() |
| 129 | |
Barry Warsaw | 3ca656f | 2001-11-13 20:16:52 +0000 | [diff] [blame] | 130 | def keys_helper(self, f): |
| 131 | keys = f.keys() |
| 132 | keys.sort() |
Skip Montanaro | c08fe82 | 2002-03-17 23:03:42 +0000 | [diff] [blame] | 133 | dkeys = self._dict.keys() |
| 134 | dkeys.sort() |
| 135 | self.assertEqual(keys, dkeys) |
Barry Warsaw | 3ca656f | 2001-11-13 20:16:52 +0000 | [diff] [blame] | 136 | return keys |
| 137 | |
Tim Peters | 663d1b6 | 2003-07-11 04:09:09 +0000 | [diff] [blame] | 138 | # Perform randomized operations. This doesn't make assumptions about |
| 139 | # what *might* fail. |
| 140 | def test_random(self): |
| 141 | import random |
| 142 | d = {} # mirror the database |
| 143 | for dummy in range(5): |
| 144 | f = dumbdbm.open(_fname) |
| 145 | for dummy in range(100): |
| 146 | k = random.choice('abcdefghijklm') |
| 147 | if random.random() < 0.2: |
| 148 | if k in d: |
| 149 | del d[k] |
| 150 | del f[k] |
| 151 | else: |
| 152 | v = random.choice('abc') * random.randrange(10000) |
| 153 | d[k] = v |
| 154 | f[k] = v |
| 155 | self.assertEqual(f[k], v) |
| 156 | f.close() |
| 157 | |
| 158 | f = dumbdbm.open(_fname) |
| 159 | expected = d.items() |
| 160 | expected.sort() |
| 161 | got = f.items() |
| 162 | got.sort() |
| 163 | self.assertEqual(expected, got) |
| 164 | f.close() |
| 165 | |
Skip Montanaro | c08fe82 | 2002-03-17 23:03:42 +0000 | [diff] [blame] | 166 | def tearDown(self): |
| 167 | _delete_files() |
| 168 | |
| 169 | def setUp(self): |
| 170 | _delete_files() |
| 171 | |
Barry Warsaw | 3ca656f | 2001-11-13 20:16:52 +0000 | [diff] [blame] | 172 | def test_main(): |
Tim Peters | d9fbf35 | 2002-01-30 07:32:53 +0000 | [diff] [blame] | 173 | try: |
| 174 | test_support.run_unittest(DumbDBMTestCase) |
| 175 | finally: |
| 176 | _delete_files() |
Barry Warsaw | 3ca656f | 2001-11-13 20:16:52 +0000 | [diff] [blame] | 177 | |
| 178 | if __name__ == "__main__": |
| 179 | test_main() |