2
# Twisted, the Framework of Your Internet
3
# Copyright (C) 2001 Matthew W. Lefkowitz
5
# This library is free software; you can redistribute it and/or
6
# modify it under the terms of version 2.1 of the GNU Lesser General Public
7
# License as published by the Free Software Foundation.
9
# This library is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
# Lesser General Public License for more details.
14
# You should have received a copy of the GNU Lesser General Public
15
# License along with this library; if not, write to the Free Software
16
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20
Test cases for dirdbm module.
23
from pyunit import unittest
24
from twisted.persisted import dirdbm
25
import os, tempfile, shutil, glob
27
class DirDbmTestCase(unittest.TestCase):
30
self.path = tempfile.mktemp()
31
self.dbm = dirdbm.open(self.path)
32
self.items = (('abc', 'foo'), ('/lalal', '\000\001'), ('\000\012', 'baz'))
35
shutil.rmtree(self.path)
43
for k, v in self.items:
51
for k, v in self.items:
52
assert d.has_key(k), "has_key() failed"
53
assert d[k] == v, "database has wrong value"
55
# check non existent key
61
assert 0, "didn't raise KeyError on non-existent key"
63
# check keys(), values() and items()
64
dbkeys = list(d.keys())
65
dbvalues = list(d.values())
66
dbitems = list(d.items())
70
items = list(self.items)
72
assert keys == dbkeys, ".keys() output didn't match: %s != %s" % (repr(keys), repr(dbkeys))
73
assert values == dbvalues, ".values() output didn't match: %s != %s" % (repr(values), repr(dbvalues))
74
assert items == dbitems, "items() didn't match: %s != %s" % (repr(items), repr(dbitems))
77
for k, v in self.items:
79
assert not d.has_key(k), "has_key() even though we deleted it"
80
assert len(d.keys()) == 0, "database has keys"
81
assert len(d.values()) == 0, "database has values"
82
assert len(d.items()) == 0, "database has items"
84
def testRecovery(self):
85
"""DirDBM: test recovery from directory after a faked crash"""
86
k = self.dbm._encode("key1")
87
f = open(os.path.join(self.path, k + ".rpl"), "wb")
91
k2 = self.dbm._encode("key2")
92
f = open(os.path.join(self.path, k2), "wb")
95
f = open(os.path.join(self.path, k2 + ".rpl"), "wb")
99
f = open(os.path.join(self.path, "aa.new"), "wb")
103
dbm = dirdbm.DirDBM(self.path)
104
assert dbm["key1"] == "value"
105
assert dbm["key2"] == "correct"
106
assert not glob.glob(os.path.join(self.path, "*.new"))
107
assert not glob.glob(os.path.join(self.path, "*.rpl"))
110
class ShelfTestCase(DirDbmTestCase):
113
self.path = tempfile.mktemp()
114
self.dbm = dirdbm.Shelf(self.path)
115
self.items = (('abc', 'foo'), ('/lalal', '\000\001'), ('\000\012', 'baz'),
116
('int', 12), ('float', 12.0), ('tuple', (None, 12)))
119
testCases = [DirDbmTestCase, ShelfTestCase]