summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_anydbm.py
blob: 9399d711cffcb6a0b49e1ddc547eb8f6e9012fee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#! /usr/bin/env python
"""Test script for the anydbm module
   based on testdumbdbm.py
"""

import os
import unittest
import anydbm
import glob
from test import test_support

_fname = test_support.TESTFN

_all_modules = []

for _name in anydbm._names:
    try:
        _module = __import__(_name)
    except ImportError:
        continue
    _all_modules.append(_module)


#
# Iterates over every database module supported by anydbm
# currently available, setting anydbm to use each in turn,
# and yielding that module
#
def dbm_iterator():
    old_default = anydbm._defaultmod
    for module in _all_modules:
        anydbm._defaultmod = module
        yield module
    anydbm._defaultmod = old_default

#
# Clean up all scratch databases we might have created
# during testing
#
def delete_files():
    # we don't know the precise name the underlying database uses
    # so we use glob to locate all names
    for f in glob.glob(_fname + "*"):
        try:
            os.unlink(f)
        except OSError:
            pass

class AnyDBMTestCase(unittest.TestCase):
    _dict = {'0': b'',
             'a': b'Python:',
             'b': b'Programming',
             'c': b'the',
             'd': b'way',
             'f': b'Guido',
             'g': b'intended',
             }

    def __init__(self, *args):
        unittest.TestCase.__init__(self, *args)

    def test_anydbm_creation(self):
        f = anydbm.open(_fname, 'c')
        self.assertEqual(list(f.keys()), [])
        for key in self._dict:
            f[key.encode("ascii")] = self._dict[key]
        self.read_helper(f)
        f.close()

    def test_anydbm_modification(self):
        self.init_db()
        f = anydbm.open(_fname, 'c')
        self._dict['g'] = f[b'g'] = b"indented"
        self.read_helper(f)
        f.close()

    def test_anydbm_read(self):
        self.init_db()
        f = anydbm.open(_fname, 'r')
        self.read_helper(f)
        f.close()

    def test_anydbm_keys(self):
        self.init_db()
        f = anydbm.open(_fname, 'r')
        keys = self.keys_helper(f)
        f.close()

    def test_anydbm_access(self):
        self.init_db()
        f = anydbm.open(_fname, 'r')
        key = "a".encode("ascii")
        assert(key in f)
        assert(f[key] == b"Python:")
        f.close()

    def read_helper(self, f):
        keys = self.keys_helper(f)
        for key in self._dict:
            self.assertEqual(self._dict[key], f[key.encode("ascii")])

    def init_db(self):
        f = anydbm.open(_fname, 'n')
        for k in self._dict:
            f[k.encode("ascii")] = self._dict[k]
        f.close()

    def keys_helper(self, f):
        keys = sorted(k.decode("ascii") for k in f.keys())
        dkeys = sorted(self._dict.keys())
        self.assertEqual(keys, dkeys)
        return keys

    def tearDown(self):
        delete_files()

    def setUp(self):
        delete_files()


def test_main():
    try:
        for module in dbm_iterator():
            test_support.run_unittest(AnyDBMTestCase)
    finally:
        delete_files()

if __name__ == "__main__":
    test_main()