Roger E. Masse | 16f9fd2 | 1996-12-10 00:06:24 +0000 | [diff] [blame] | 1 | #! /usr/bin/env python |
| 2 | """Test script for the dbm module |
| 3 | Roger E. Masse |
| 4 | """ |
Neal Norwitz | e7629c8 | 2002-11-02 18:25:08 +0000 | [diff] [blame] | 5 | import os |
| 6 | import random |
Roger E. Masse | 16f9fd2 | 1996-12-10 00:06:24 +0000 | [diff] [blame] | 7 | import dbm |
| 8 | from dbm import error |
Neal Norwitz | e7629c8 | 2002-11-02 18:25:08 +0000 | [diff] [blame] | 9 | from test.test_support import verbose, verify, TestSkipped |
Roger E. Masse | fab8ab8 | 1996-12-20 22:36:52 +0000 | [diff] [blame] | 10 | |
Neal Norwitz | e7629c8 | 2002-11-02 18:25:08 +0000 | [diff] [blame] | 11 | # make filename unique to allow multiple concurrent tests |
| 12 | # and to minimize the likelihood of a problem from an old file |
| 13 | filename = '/tmp/delete_me_' + str(random.random())[-6:] |
Roger E. Masse | 16f9fd2 | 1996-12-10 00:06:24 +0000 | [diff] [blame] | 14 | |
Neal Norwitz | e7629c8 | 2002-11-02 18:25:08 +0000 | [diff] [blame] | 15 | def cleanup(): |
| 16 | for suffix in ['', '.pag', '.dir', '.db']: |
| 17 | try: |
| 18 | os.unlink(filename + suffix) |
| 19 | except OSError, (errno, strerror): |
| 20 | # if we can't delete the file because of permissions, |
| 21 | # nothing will work, so skip the test |
| 22 | if errno == 1: |
| 23 | raise TestSkipped, 'unable to remove: ' + filename + suffix |
Fred Drake | 57a4e90 | 2000-09-18 17:56:58 +0000 | [diff] [blame] | 24 | |
Neal Norwitz | e7629c8 | 2002-11-02 18:25:08 +0000 | [diff] [blame] | 25 | def test_keys(): |
| 26 | d = dbm.open(filename, 'c') |
| 27 | verify(d.keys() == []) |
| 28 | d['a'] = 'b' |
| 29 | d['12345678910'] = '019237410982340912840198242' |
| 30 | d.keys() |
| 31 | if d.has_key('a'): |
| 32 | if verbose: |
| 33 | print 'Test dbm keys: ', d.keys() |
Roger E. Masse | 16f9fd2 | 1996-12-10 00:06:24 +0000 | [diff] [blame] | 34 | |
Neal Norwitz | e7629c8 | 2002-11-02 18:25:08 +0000 | [diff] [blame] | 35 | d.close() |
| 36 | |
| 37 | def test_modes(): |
| 38 | d = dbm.open(filename, 'r') |
| 39 | d.close() |
| 40 | d = dbm.open(filename, 'rw') |
| 41 | d.close() |
| 42 | d = dbm.open(filename, 'w') |
| 43 | d.close() |
| 44 | d = dbm.open(filename, 'n') |
| 45 | d.close() |
| 46 | |
| 47 | cleanup() |
Roger E. Masse | 16f9fd2 | 1996-12-10 00:06:24 +0000 | [diff] [blame] | 48 | try: |
Neal Norwitz | e7629c8 | 2002-11-02 18:25:08 +0000 | [diff] [blame] | 49 | test_keys() |
| 50 | test_modes() |
Roger E. Masse | 16f9fd2 | 1996-12-10 00:06:24 +0000 | [diff] [blame] | 51 | except: |
Neal Norwitz | e7629c8 | 2002-11-02 18:25:08 +0000 | [diff] [blame] | 52 | cleanup() |
| 53 | raise |
| 54 | |
| 55 | cleanup() |