1 """TestCases for exercising a Recno DB.
6 from pprint
import pprint
9 from test_all
import db
, test_support
, verbose
, get_new_environment_path
, get_new_database_path
11 letters
= 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
14 #----------------------------------------------------------------------
16 class SimpleRecnoTestCase(unittest
.TestCase
):
17 if sys
.version_info
< (2, 4) :
18 def assertFalse(self
, expr
, msg
=None) :
19 return self
.failIf(expr
,msg
=msg
)
20 def assertTrue(self
, expr
, msg
=None) :
21 return self
.assert_(expr
, msg
=msg
)
23 if (sys
.version_info
< (2, 7)) or ((sys
.version_info
>= (3, 0)) and
24 (sys
.version_info
< (3, 2))) :
25 def assertIsInstance(self
, obj
, datatype
, msg
=None) :
26 return self
.assertEqual(type(obj
), datatype
, msg
=msg
)
27 def assertGreaterEqual(self
, a
, b
, msg
=None) :
28 return self
.assertTrue(a
>=b
, msg
=msg
)
32 self
.filename
= get_new_database_path()
36 test_support
.unlink(self
.filename
)
38 test_support
.rmtree(self
.homeDir
)
40 def test01_basic(self
):
43 get_returns_none
= d
.set_get_returns_none(2)
44 d
.set_get_returns_none(get_returns_none
)
46 d
.open(self
.filename
, db
.DB_RECNO
, db
.DB_CREATE
)
49 recno
= d
.append(x
* 60)
50 self
.assertIsInstance(recno
, int)
51 self
.assertGreaterEqual(recno
, 1)
61 for recno
in range(1, len(d
)+1):
66 self
.assertIsInstance(data
, str)
67 self
.assertEqual(data
, d
.get(recno
))
70 data
= d
[0] # This should raise a KeyError!?!?!
71 except db
.DBInvalidArgError
, val
:
72 if sys
.version_info
< (2, 6) :
73 self
.assertEqual(val
[0], db
.EINVAL
)
75 self
.assertEqual(val
.args
[0], db
.EINVAL
)
78 self
.fail("expected exception")
80 # test that has_key raises DB exceptions (fixed in pybsddb 4.3.2)
83 except db
.DBError
, val
:
86 self
.fail("has_key did not raise a proper exception")
93 self
.fail("expected exception")
97 except db
.DBNotFoundError
, val
:
99 self
.fail("unexpected exception")
101 self
.assertEqual(data
, None)
106 self
.assertIsInstance(keys
, list)
107 self
.assertIsInstance(keys
[0], int)
108 self
.assertEqual(len(keys
), len(d
))
113 self
.assertIsInstance(items
, list)
114 self
.assertIsInstance(items
[0], tuple)
115 self
.assertEqual(len(items
[0]), 2)
116 self
.assertIsInstance(items
[0][0], int)
117 self
.assertIsInstance(items
[0][1], str)
118 self
.assertEqual(len(items
), len(d
))
120 self
.assertTrue(d
.has_key(25))
123 self
.assertFalse(d
.has_key(25))
126 self
.assertFalse(d
.has_key(13))
128 data
= d
.get_both(26, "z" * 60)
129 self
.assertEqual(data
, "z" * 60, 'was %r' % data
)
149 c
.put(-1, "a replacement record", db
.DB_CURRENT
)
153 self
.assertEqual(rec
, (50, "a replacement record"))
157 rec
= c
.set_range(30)
161 # test that non-existent key lookups work (and that
162 # DBC_set_range doesn't have a memleak under valgrind)
163 rec
= c
.set_range(999999)
164 self
.assertEqual(rec
, None)
172 d
.open(self
.filename
)
175 # put a record beyond the consecutive end of the recno's
176 d
[100] = "way out there"
177 self
.assertEqual(d
[100], "way out there")
184 self
.fail("expected exception")
188 except db
.DBKeyEmptyError
, val
:
190 self
.fail("unexpected DBKeyEmptyError exception")
192 if sys
.version_info
< (2, 6) :
193 self
.assertEqual(val
[0], db
.DB_KEYEMPTY
)
195 self
.assertEqual(val
.args
[0], db
.DB_KEYEMPTY
)
196 if verbose
: print val
198 if not get_returns_none
:
199 self
.fail("expected exception")
210 def test02_WithSource(self
):
212 A Recno file that is given a "backing source file" is essentially a
213 simple ASCII file. Normally each record is delimited by \n and so is
214 just a line in the file, but you can set a different record delimiter
217 homeDir
= get_new_environment_path()
218 self
.homeDir
= homeDir
219 source
= os
.path
.join(homeDir
, 'test_recno.txt')
220 if not os
.path
.isdir(homeDir
):
222 f
= open(source
, 'w') # create the file
226 # This is the default value, just checking if both int
228 d
.set_re_delim('\n') # and char can be used...
229 d
.set_re_source(source
)
230 d
.open(self
.filename
, db
.DB_RECNO
, db
.DB_CREATE
)
232 data
= "The quick brown fox jumped over the lazy dog".split()
238 # get the text from the backing source
239 text
= open(source
, 'r').read()
244 print text
.split('\n')
246 self
.assertEqual(text
.split('\n'), data
)
250 d
.set_re_source(source
)
251 d
.open(self
.filename
, db
.DB_RECNO
)
253 d
[3] = 'reddish-brown'
259 text
= open(source
, 'r').read()
263 print text
.split('\n')
265 self
.assertEqual(text
.split('\n'),
266 "The quick reddish-brown fox jumped over the comatose dog".split())
268 def test03_FixedLength(self
):
270 d
.set_re_len(40) # fixed length records, 40 bytes long
271 d
.set_re_pad('-') # sets the pad character...
272 d
.set_re_pad(45) # ...test both int and char
273 d
.open(self
.filename
, db
.DB_RECNO
, db
.DB_CREATE
)
276 d
.append(x
* 35) # These will be padded
278 d
.append('.' * 40) # this one will be exact
280 try: # this one will fail
282 except db
.DBInvalidArgError
, val
:
283 if sys
.version_info
< (2, 6) :
284 self
.assertEqual(val
[0], db
.EINVAL
)
286 self
.assertEqual(val
.args
[0], db
.EINVAL
)
287 if verbose
: print val
289 self
.fail("expected exception")
302 #----------------------------------------------------------------------
306 return unittest
.makeSuite(SimpleRecnoTestCase
)
309 if __name__
== '__main__':
310 unittest
.main(defaultTest
='test_suite')