1 """TestCases for exercising a Recno DB.
8 from pprint
import pprint
12 from test_all
import verbose
14 letters
= 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
17 #----------------------------------------------------------------------
19 class SimpleRecnoTestCase(unittest
.TestCase
):
21 self
.filename
= tempfile
.mktemp()
25 os
.remove(self
.filename
)
27 if e
.errno
<> errno
.EEXIST
: raise
29 def test01_basic(self
):
31 d
.open(self
.filename
, db
.DB_RECNO
, db
.DB_CREATE
)
34 recno
= d
.append(x
* 60)
35 assert type(recno
) == type(0)
46 for recno
in range(1, len(d
)+1):
51 assert type(data
) == type("")
52 assert data
== d
.get(recno
)
55 data
= d
[0] # This should raise a KeyError!?!?!
56 except db
.DBInvalidArgError
, val
:
57 assert val
[0] == db
.EINVAL
60 self
.fail("expected exception")
67 self
.fail("expected exception")
75 assert type(keys
) == type([])
76 assert type(keys
[0]) == type(123)
77 assert len(keys
) == len(d
)
82 assert type(items
) == type([])
83 assert type(items
[0]) == type(())
84 assert len(items
[0]) == 2
85 assert type(items
[0][0]) == type(123)
86 assert type(items
[0][1]) == type("")
87 assert len(items
) == len(d
)
92 assert not d
.has_key(25)
95 assert not d
.has_key(13)
97 data
= d
.get_both(26, "z" * 60)
98 assert data
== "z" * 60
118 c
.put(-1, "a replacement record", db
.DB_CURRENT
)
122 assert rec
== (50, "a replacement record")
126 rec
= c
.set_range(30)
134 d
.open(self
.filename
)
137 # put a record beyond the consecutive end of the recno's
138 d
[100] = "way out there"
139 assert d
[100] == "way out there"
146 self
.fail("expected exception")
150 except db
.DBKeyEmptyError
, val
:
151 assert val
[0] == db
.DB_KEYEMPTY
152 if verbose
: print val
154 self
.fail("expected exception")
165 def test02_WithSource(self
):
167 A Recno file that is given a "backing source file" is essentially a
168 simple ASCII file. Normally each record is delimited by \n and so is
169 just a line in the file, but you can set a different record delimiter
172 source
= os
.path
.join(os
.path
.dirname(sys
.argv
[0]),
173 'db_home/test_recno.txt')
174 f
= open(source
, 'w') # create the file
178 # This is the default value, just checking if both int
180 d
.set_re_delim('\n') # and char can be used...
181 d
.set_re_source(source
)
182 d
.open(self
.filename
, db
.DB_RECNO
, db
.DB_CREATE
)
184 data
= "The quick brown fox jumped over the lazy dog".split()
190 # get the text from the backing source
191 text
= open(source
, 'r').read()
196 print text
.split('\n')
198 assert text
.split('\n') == data
202 d
.set_re_source(source
)
203 d
.open(self
.filename
, db
.DB_RECNO
)
205 d
[3] = 'reddish-brown'
211 text
= open(source
, 'r').read()
215 print text
.split('\n')
217 assert text
.split('\n') == \
218 "The quick reddish-brown fox jumped over the comatose dog".split()
220 def test03_FixedLength(self
):
222 d
.set_re_len(40) # fixed length records, 40 bytes long
223 d
.set_re_pad('-') # sets the pad character...
224 d
.set_re_pad(45) # ...test both int and char
225 d
.open(self
.filename
, db
.DB_RECNO
, db
.DB_CREATE
)
228 d
.append(x
* 35) # These will be padded
230 d
.append('.' * 40) # this one will be exact
232 try: # this one will fail
234 except db
.DBInvalidArgError
, val
:
235 assert val
[0] == db
.EINVAL
236 if verbose
: print val
238 self
.fail("expected exception")
251 #----------------------------------------------------------------------
255 return unittest
.makeSuite(SimpleRecnoTestCase
)
258 if __name__
== '__main__':
259 unittest
.main(defaultTest
='test_suite')