2 TestCases for checking dbShelve objects.
6 import tempfile
, random
7 from pprint
import pprint
13 from bsddb
import db
, dbshelve
15 # For earlier Pythons w/distutils pybsddb
16 from bsddb3
import db
, dbshelve
18 from test_all
import verbose
21 #----------------------------------------------------------------------
23 # We want the objects to be comparable so we can test dbshelve.values
27 self
.value
= random
.random()
29 def __cmp__(self
, other
):
30 return cmp(self
.value
, other
)
32 class DBShelveTestCase(unittest
.TestCase
):
34 self
.filename
= tempfile
.mktemp()
40 os
.remove(self
.filename
)
44 def populateDB(self
, d
):
45 for x
in string
.letters
:
46 d
['S' + x
] = 10 * x
# add a string
47 d
['I' + x
] = ord(x
) # add an integer
48 d
['L' + x
] = [x
] * 10 # add a list
50 inst
= DataClass() # add an instance
57 # overridable in derived classes to affect how the shelf is created/opened
59 self
.d
= dbshelve
.open(self
.filename
)
67 def test01_basics(self
):
70 print "Running %s.test01_basics..." % self
.__class
__.__name
__
72 self
.populateDB(self
.d
)
88 assert 0 == d
.has_key('bad key')
89 assert 1 == d
.has_key('IA')
90 assert 1 == d
.has_key('OA')
94 assert 0 == d
.has_key('IA')
95 assert 0 == d
.has_key('OA')
103 print "%s: %s" % (key
, value
)
104 self
.checkrec(key
, value
)
106 dbvalues
= d
.values()
107 assert len(dbvalues
) == len(d
.keys())
110 assert values
== dbvalues
113 assert len(items
) == len(values
)
115 for key
, value
in items
:
116 self
.checkrec(key
, value
)
118 assert d
.get('bad key') == None
119 assert d
.get('bad key', None) == None
120 assert d
.get('bad key', 'a string') == 'a string'
121 assert d
.get('bad key', [1, 2, 3]) == [1, 2, 3]
123 d
.set_get_returns_none(0)
124 self
.assertRaises(db
.DBNotFoundError
, d
.get
, 'bad key')
125 d
.set_get_returns_none(1)
127 d
.put('new key', 'new data')
128 assert d
.get('new key') == 'new data'
129 assert d
['new key'] == 'new data'
133 def test02_cursors(self
):
135 print '\n', '-=' * 30
136 print "Running %s.test02_cursors..." % self
.__class
__.__name
__
138 self
.populateDB(self
.d
)
144 while rec
is not None:
149 self
.checkrec(key
, value
)
153 assert count
== len(d
)
158 while rec
is not None:
163 self
.checkrec(key
, value
)
166 assert count
== len(d
)
169 key
, value
= c
.current()
170 self
.checkrec(key
, value
)
175 def checkrec(self
, key
, value
):
178 assert type(value
) == StringType
179 assert value
== 10 * x
182 assert type(value
) == IntType
183 assert value
== ord(x
)
186 assert type(value
) == ListType
187 assert value
== [x
] * 10
190 assert type(value
) == InstanceType
191 assert value
.S
== 10 * x
192 assert value
.I
== ord(x
)
193 assert value
.L
== [x
] * 10
196 raise AssertionError, 'Unknown key type, fix the test'
198 #----------------------------------------------------------------------
200 class BasicShelveTestCase(DBShelveTestCase
):
202 self
.d
= dbshelve
.DBShelf()
203 self
.d
.open(self
.filename
, self
.dbtype
, self
.dbflags
)
209 class BTreeShelveTestCase(BasicShelveTestCase
):
211 dbflags
= db
.DB_CREATE
214 class HashShelveTestCase(BasicShelveTestCase
):
216 dbflags
= db
.DB_CREATE
219 class ThreadBTreeShelveTestCase(BasicShelveTestCase
):
221 dbflags
= db
.DB_CREATE | db
.DB_THREAD
224 class ThreadHashShelveTestCase(BasicShelveTestCase
):
226 dbflags
= db
.DB_CREATE | db
.DB_THREAD
229 #----------------------------------------------------------------------
231 class BasicEnvShelveTestCase(DBShelveTestCase
):
233 self
.homeDir
= homeDir
= os
.path
.join(
234 os
.path
.dirname(sys
.argv
[0]), 'db_home')
235 try: os
.mkdir(homeDir
)
236 except os
.error
: pass
237 self
.env
= db
.DBEnv()
238 self
.env
.open(homeDir
, self
.envflags | db
.DB_INIT_MPOOL | db
.DB_CREATE
)
240 self
.filename
= os
.path
.split(self
.filename
)[1]
241 self
.d
= dbshelve
.DBShelf(self
.env
)
242 self
.d
.open(self
.filename
, self
.dbtype
, self
.dbflags
)
253 files
= glob
.glob(os
.path
.join(self
.homeDir
, '*'))
259 class EnvBTreeShelveTestCase(BasicEnvShelveTestCase
):
262 dbflags
= db
.DB_CREATE
265 class EnvHashShelveTestCase(BasicEnvShelveTestCase
):
268 dbflags
= db
.DB_CREATE
271 class EnvThreadBTreeShelveTestCase(BasicEnvShelveTestCase
):
272 envflags
= db
.DB_THREAD
274 dbflags
= db
.DB_CREATE | db
.DB_THREAD
277 class EnvThreadHashShelveTestCase(BasicEnvShelveTestCase
):
278 envflags
= db
.DB_THREAD
280 dbflags
= db
.DB_CREATE | db
.DB_THREAD
283 #----------------------------------------------------------------------
284 # TODO: Add test cases for a DBShelf in a RECNO DB.
287 #----------------------------------------------------------------------
290 suite
= unittest
.TestSuite()
292 suite
.addTest(unittest
.makeSuite(DBShelveTestCase
))
293 suite
.addTest(unittest
.makeSuite(BTreeShelveTestCase
))
294 suite
.addTest(unittest
.makeSuite(HashShelveTestCase
))
295 suite
.addTest(unittest
.makeSuite(ThreadBTreeShelveTestCase
))
296 suite
.addTest(unittest
.makeSuite(ThreadHashShelveTestCase
))
297 suite
.addTest(unittest
.makeSuite(EnvBTreeShelveTestCase
))
298 suite
.addTest(unittest
.makeSuite(EnvHashShelveTestCase
))
299 suite
.addTest(unittest
.makeSuite(EnvThreadBTreeShelveTestCase
))
300 suite
.addTest(unittest
.makeSuite(EnvThreadHashShelveTestCase
))
305 if __name__
== '__main__':
306 unittest
.main(defaultTest
='test_suite')