PageRenderTime 344ms CodeModel.GetById 181ms app.highlight 7ms RepoModel.GetById 154ms app.codeStats 0ms

/Lib/test/test_anydbm.py

http://unladen-swallow.googlecode.com/
Python | 95 lines | 85 code | 4 blank | 6 comment | 2 complexity | 63455c10720a5a1652d7dfcf7b774b5a MD5 | raw file
 1#! /usr/bin/env python
 2"""Test script for the anydbm module
 3   based on testdumbdbm.py
 4"""
 5
 6import os
 7import unittest
 8import anydbm
 9import glob
10from test import test_support
11
12_fname = test_support.TESTFN
13
14def _delete_files():
15    # we don't know the precise name the underlying database uses
16    # so we use glob to locate all names
17    for f in glob.glob(_fname + "*"):
18        try:
19            os.unlink(f)
20        except OSError:
21            pass
22
23class AnyDBMTestCase(unittest.TestCase):
24    _dict = {'0': '',
25             'a': 'Python:',
26             'b': 'Programming',
27             'c': 'the',
28             'd': 'way',
29             'f': 'Guido',
30             'g': 'intended'
31             }
32
33    def __init__(self, *args):
34        unittest.TestCase.__init__(self, *args)
35
36    def test_anydbm_creation(self):
37        f = anydbm.open(_fname, 'c')
38        self.assertEqual(f.keys(), [])
39        for key in self._dict:
40            f[key] = self._dict[key]
41        self.read_helper(f)
42        f.close()
43
44    def test_anydbm_modification(self):
45        self.init_db()
46        f = anydbm.open(_fname, 'c')
47        self._dict['g'] = f['g'] = "indented"
48        self.read_helper(f)
49        f.close()
50
51    def test_anydbm_read(self):
52        self.init_db()
53        f = anydbm.open(_fname, 'r')
54        self.read_helper(f)
55        f.close()
56
57    def test_anydbm_keys(self):
58        self.init_db()
59        f = anydbm.open(_fname, 'r')
60        keys = self.keys_helper(f)
61        f.close()
62
63    def read_helper(self, f):
64        keys = self.keys_helper(f)
65        for key in self._dict:
66            self.assertEqual(self._dict[key], f[key])
67
68    def init_db(self):
69        f = anydbm.open(_fname, 'n')
70        for k in self._dict:
71            f[k] = self._dict[k]
72        f.close()
73
74    def keys_helper(self, f):
75        keys = f.keys()
76        keys.sort()
77        dkeys = self._dict.keys()
78        dkeys.sort()
79        self.assertEqual(keys, dkeys)
80        return keys
81
82    def tearDown(self):
83        _delete_files()
84
85    def setUp(self):
86        _delete_files()
87
88def test_main():
89    try:
90        test_support.run_unittest(AnyDBMTestCase)
91    finally:
92        _delete_files()
93
94if __name__ == "__main__":
95    test_main()