2 TestCases for checking dbShelve objects.
6 import tempfile
, random
7 from pprint
import pprint
11 from bsddb
import dbshelve
, db
13 from test_all
import verbose
16 #----------------------------------------------------------------------
18 # We want the objects to be comparable so we can test dbshelve.values
22 self
.value
= random
.random()
24 def __cmp__(self
, other
):
25 return cmp(self
.value
, other
)
27 class DBShelveTestCase(unittest
.TestCase
):
29 self
.filename
= tempfile
.mktemp()
35 os
.remove(self
.filename
)
39 def populateDB(self
, d
):
40 for x
in string
.letters
:
41 d
['S' + x
] = 10 * x
# add a string
42 d
['I' + x
] = ord(x
) # add an integer
43 d
['L' + x
] = [x
] * 10 # add a list
45 inst
= DataClass() # add an instance
52 # overridable in derived classes to affect how the shelf is created/opened
54 self
.d
= dbshelve
.open(self
.filename
)
62 def test01_basics(self
):
65 print "Running %s.test01_basics..." % self
.__class
__.__name
__
67 self
.populateDB(self
.d
)
83 assert 0 == d
.has_key('bad key')
84 assert 1 == d
.has_key('IA')
85 assert 1 == d
.has_key('OA')
89 assert 0 == d
.has_key('IA')
90 assert 0 == d
.has_key('OA')
98 print "%s: %s" % (key
, value
)
99 self
.checkrec(key
, value
)
101 dbvalues
= d
.values()
102 assert len(dbvalues
) == len(d
.keys())
105 assert values
== dbvalues
108 assert len(items
) == len(values
)
110 for key
, value
in items
:
111 self
.checkrec(key
, value
)
113 assert d
.get('bad key') == None
114 assert d
.get('bad key', None) == None
115 assert d
.get('bad key', 'a string') == 'a string'
116 assert d
.get('bad key', [1, 2, 3]) == [1, 2, 3]
118 d
.set_get_returns_none(0)
119 self
.assertRaises(db
.DBNotFoundError
, d
.get
, 'bad key')
120 d
.set_get_returns_none(1)
122 d
.put('new key', 'new data')
123 assert d
.get('new key') == 'new data'
124 assert d
['new key'] == 'new data'
128 def test02_cursors(self
):
130 print '\n', '-=' * 30
131 print "Running %s.test02_cursors..." % self
.__class
__.__name
__
133 self
.populateDB(self
.d
)
139 while rec
is not None:
144 self
.checkrec(key
, value
)
148 assert count
== len(d
)
153 while rec
is not None:
158 self
.checkrec(key
, value
)
161 assert count
== len(d
)
164 key
, value
= c
.current()
165 self
.checkrec(key
, value
)
170 def checkrec(self
, key
, value
):
173 assert type(value
) == StringType
174 assert value
== 10 * x
177 assert type(value
) == IntType
178 assert value
== ord(x
)
181 assert type(value
) == ListType
182 assert value
== [x
] * 10
185 assert type(value
) == InstanceType
186 assert value
.S
== 10 * x
187 assert value
.I
== ord(x
)
188 assert value
.L
== [x
] * 10
191 raise AssertionError, 'Unknown key type, fix the test'
193 #----------------------------------------------------------------------
195 class BasicShelveTestCase(DBShelveTestCase
):
197 self
.d
= dbshelve
.DBShelf()
198 self
.d
.open(self
.filename
, self
.dbtype
, self
.dbflags
)
204 class BTreeShelveTestCase(BasicShelveTestCase
):
206 dbflags
= db
.DB_CREATE
209 class HashShelveTestCase(BasicShelveTestCase
):
211 dbflags
= db
.DB_CREATE
214 class ThreadBTreeShelveTestCase(BasicShelveTestCase
):
216 dbflags
= db
.DB_CREATE | db
.DB_THREAD
219 class ThreadHashShelveTestCase(BasicShelveTestCase
):
221 dbflags
= db
.DB_CREATE | db
.DB_THREAD
224 #----------------------------------------------------------------------
226 class BasicEnvShelveTestCase(DBShelveTestCase
):
228 self
.homeDir
= homeDir
= os
.path
.join(
229 os
.path
.dirname(sys
.argv
[0]), 'db_home')
230 try: os
.mkdir(homeDir
)
231 except os
.error
: pass
232 self
.env
= db
.DBEnv()
233 self
.env
.open(homeDir
, self
.envflags | db
.DB_INIT_MPOOL | db
.DB_CREATE
)
235 self
.filename
= os
.path
.split(self
.filename
)[1]
236 self
.d
= dbshelve
.DBShelf(self
.env
)
237 self
.d
.open(self
.filename
, self
.dbtype
, self
.dbflags
)
248 files
= glob
.glob(os
.path
.join(self
.homeDir
, '*'))
254 class EnvBTreeShelveTestCase(BasicEnvShelveTestCase
):
257 dbflags
= db
.DB_CREATE
260 class EnvHashShelveTestCase(BasicEnvShelveTestCase
):
263 dbflags
= db
.DB_CREATE
266 class EnvThreadBTreeShelveTestCase(BasicEnvShelveTestCase
):
267 envflags
= db
.DB_THREAD
269 dbflags
= db
.DB_CREATE | db
.DB_THREAD
272 class EnvThreadHashShelveTestCase(BasicEnvShelveTestCase
):
273 envflags
= db
.DB_THREAD
275 dbflags
= db
.DB_CREATE | db
.DB_THREAD
278 #----------------------------------------------------------------------
279 # TODO: Add test cases for a DBShelf in a RECNO DB.
282 #----------------------------------------------------------------------
285 suite
= unittest
.TestSuite()
287 suite
.addTest(unittest
.makeSuite(DBShelveTestCase
))
288 suite
.addTest(unittest
.makeSuite(BTreeShelveTestCase
))
289 suite
.addTest(unittest
.makeSuite(HashShelveTestCase
))
290 suite
.addTest(unittest
.makeSuite(ThreadBTreeShelveTestCase
))
291 suite
.addTest(unittest
.makeSuite(ThreadHashShelveTestCase
))
292 suite
.addTest(unittest
.makeSuite(EnvBTreeShelveTestCase
))
293 suite
.addTest(unittest
.makeSuite(EnvHashShelveTestCase
))
294 suite
.addTest(unittest
.makeSuite(EnvThreadBTreeShelveTestCase
))
295 suite
.addTest(unittest
.makeSuite(EnvThreadHashShelveTestCase
))
300 if __name__
== '__main__':
301 unittest
.main(defaultTest
='test_suite')