1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
|
#!/usr/bin/env python3
# Some simple tests for the Python bindings for TDB
# Note that this tests the interface of the Python bindings
# It does not test tdb itself.
#
# Copyright (C) 2007-2008 Jelmer Vernooij <jelmer@samba.org>
# Published under the GNU LGPLv3 or later
import sys
import os
import tempfile
from unittest import TestCase
import tdb
class OpenTdbTests(TestCase):
def test_nonexistent_read(self):
self.assertRaises(IOError, tdb.Tdb, "/some/nonexistent/file", 0,
tdb.DEFAULT, os.O_RDWR)
class CloseTdbTests(TestCase):
def test_double_close(self):
self.tdb = tdb.Tdb(tempfile.mkstemp()[1], 0, tdb.DEFAULT,
os.O_CREAT|os.O_RDWR)
self.assertNotEqual(None, self.tdb)
# ensure that double close does not crash python
self.tdb.close()
self.tdb.close()
# Check that further operations do not crash python
self.assertRaises(RuntimeError, lambda: self.tdb.transaction_start())
self.assertRaises(RuntimeError, lambda: self.tdb["bar"])
class InternalTdbTests(TestCase):
def test_repr(self):
self.tdb = tdb.Tdb()
# repr used to crash on internal db
self.assertEqual(repr(self.tdb), "Tdb(<internal>)")
class CommonTdbTests(TestCase):
"""Tests common to both the text & bytes interfaces"""
use_text = False
def setUp(self):
super(CommonTdbTests, self).setUp()
self.tdb = tdb.Tdb(tempfile.mkstemp()[1], 0, tdb.DEFAULT,
os.O_CREAT|os.O_RDWR)
self.assertNotEqual(None, self.tdb)
if self.use_text:
self.tdb = self.tdb.text
def test_lockall(self):
self.tdb.lock_all()
def test_max_dead(self):
self.tdb.max_dead = 20
def test_unlockall(self):
self.tdb.lock_all()
self.tdb.unlock_all()
def test_lockall_read(self):
self.tdb.read_lock_all()
self.tdb.read_unlock_all()
def test_reopen(self):
self.tdb.reopen()
def test_hash_size(self):
self.tdb.hash_size
def test_map_size(self):
self.tdb.map_size
def test_freelist_size(self):
self.tdb.freelist_size
def test_name(self):
self.tdb.filename
def test_add_flags(self):
self.tdb.add_flags(tdb.NOMMAP)
self.tdb.remove_flags(tdb.NOMMAP)
class TextCommonTdbTests(CommonTdbTests):
use_text = True
class SimpleTdbTests(TestCase):
def setUp(self):
super(SimpleTdbTests, self).setUp()
self.tdb = tdb.Tdb(tempfile.mkstemp()[1], 0, tdb.DEFAULT,
os.O_CREAT|os.O_RDWR)
self.assertNotEqual(None, self.tdb)
def test_repr(self):
self.assertTrue(repr(self.tdb).startswith("Tdb('"))
def test_store(self):
self.tdb.store(b"bar", b"bla")
self.assertEqual(b"bla", self.tdb.get(b"bar"))
def test_storev(self):
self.tdb.storev(b"bar", [b"first", b"second", b"third"])
self.assertEqual(b"firstsecondthird", self.tdb.get(b"bar"))
def test_getitem(self):
self.tdb[b"bar"] = b"foo"
self.tdb.reopen()
self.assertEqual(b"foo", self.tdb[b"bar"])
def test_delete(self):
self.tdb[b"bar"] = b"foo"
del self.tdb[b"bar"]
self.assertRaises(KeyError, lambda: self.tdb[b"bar"])
def test_contains(self):
self.tdb[b"bla"] = b"bloe"
self.assertTrue(b"bla" in self.tdb)
self.assertFalse(b"qwertyuiop" in self.tdb)
if sys.version_info < (3, 0):
self.assertTrue(self.tdb.has_key(b"bla"))
self.assertFalse(self.tdb.has_key(b"qwertyuiop"))
def test_keyerror(self):
self.assertRaises(KeyError, lambda: self.tdb[b"bla"])
def test_iterator(self):
self.tdb[b"bla"] = b"1"
self.tdb[b"brainslug"] = b"2"
l = list(self.tdb)
l.sort()
self.assertEqual([b"bla", b"brainslug"], l)
def test_transaction_cancel(self):
self.tdb[b"bloe"] = b"2"
self.tdb.transaction_start()
self.tdb[b"bloe"] = b"1"
self.tdb.transaction_cancel()
self.assertEqual(b"2", self.tdb[b"bloe"])
def test_transaction_commit(self):
self.tdb[b"bloe"] = b"2"
self.tdb.transaction_start()
self.tdb[b"bloe"] = b"1"
self.tdb.transaction_commit()
self.assertEqual(b"1", self.tdb[b"bloe"])
def test_transaction_prepare_commit(self):
self.tdb[b"bloe"] = b"2"
self.tdb.transaction_start()
self.tdb[b"bloe"] = b"1"
self.tdb.transaction_prepare_commit()
self.tdb.transaction_commit()
self.assertEqual(b"1", self.tdb[b"bloe"])
def test_iterkeys(self):
self.tdb[b"bloe"] = b"2"
self.tdb[b"bla"] = b"25"
if sys.version_info >= (3, 0):
i = self.tdb.keys()
else:
i = self.tdb.iterkeys()
self.assertEqual(set([b"bloe", b"bla"]), set([next(i), next(i)]))
def test_clear(self):
self.tdb[b"bloe"] = b"2"
self.tdb[b"bla"] = b"25"
self.assertEqual(2, len(list(self.tdb)))
self.tdb.clear()
self.assertEqual(0, len(list(self.tdb)))
def test_repack(self):
self.tdb[b"foo"] = b"abc"
self.tdb[b"bar"] = b"def"
del self.tdb[b"foo"]
self.tdb.repack()
def test_seqnum(self):
self.tdb.enable_seqnum()
seq1 = self.tdb.seqnum
self.tdb.increment_seqnum_nonblock()
seq2 = self.tdb.seqnum
self.assertEqual(seq2-seq1, 1)
def test_len(self):
self.assertEqual(0, len(list(self.tdb)))
self.tdb[b"entry"] = b"value"
self.assertEqual(1, len(list(self.tdb)))
class TdbTextTests(TestCase):
def setUp(self):
super(TdbTextTests, self).setUp()
self.tdb = tdb.Tdb(tempfile.mkstemp()[1], 0, tdb.DEFAULT,
os.O_CREAT|os.O_RDWR)
self.assertNotEqual(None, self.tdb)
def test_repr(self):
self.assertTrue(repr(self.tdb).startswith("Tdb('"))
def test_store(self):
self.tdb.text.store("bar", "bla")
self.assertEqual("bla", self.tdb.text.get("bar"))
def test_getitem(self):
self.tdb.text["bar"] = "foo"
self.tdb.reopen()
self.assertEqual("foo", self.tdb.text["bar"])
def test_delete(self):
self.tdb.text["bar"] = "foo"
del self.tdb.text["bar"]
self.assertRaises(KeyError, lambda: self.tdb.text["bar"])
def test_contains(self):
self.tdb.text["bla"] = "bloe"
self.assertTrue("bla" in self.tdb.text)
self.assertFalse("qwertyuiop" in self.tdb.text)
if sys.version_info < (3, 0):
self.assertTrue(self.tdb.text.has_key("bla"))
self.assertFalse(self.tdb.text.has_key("qwertyuiop"))
def test_keyerror(self):
self.assertRaises(KeyError, lambda: self.tdb.text["bla"])
def test_iterator(self):
self.tdb.text["bla"] = "1"
self.tdb.text["brainslug"] = "2"
l = list(self.tdb.text)
l.sort()
self.assertEqual(["bla", "brainslug"], l)
def test_transaction_cancel(self):
self.tdb.text["bloe"] = "2"
self.tdb.transaction_start()
self.tdb.text["bloe"] = "1"
self.tdb.transaction_cancel()
self.assertEqual("2", self.tdb.text["bloe"])
def test_transaction_commit(self):
self.tdb.text["bloe"] = "2"
self.tdb.transaction_start()
self.tdb.text["bloe"] = "1"
self.tdb.transaction_commit()
self.assertEqual("1", self.tdb.text["bloe"])
def test_transaction_prepare_commit(self):
self.tdb.text["bloe"] = "2"
self.tdb.transaction_start()
self.tdb.text["bloe"] = "1"
self.tdb.transaction_prepare_commit()
self.tdb.transaction_commit()
self.assertEqual("1", self.tdb.text["bloe"])
def test_iterkeys(self):
self.tdb.text["bloe"] = "2"
self.tdb.text["bla"] = "25"
if sys.version_info >= (3, 0):
i = self.tdb.text.keys()
else:
i = self.tdb.text.iterkeys()
self.assertEqual(set(["bloe", "bla"]), set([next(i), next(i)]))
def test_clear(self):
self.tdb.text["bloe"] = "2"
self.tdb.text["bla"] = "25"
self.assertEqual(2, len(list(self.tdb)))
self.tdb.clear()
self.assertEqual(0, len(list(self.tdb)))
def test_repack(self):
self.tdb.text["foo"] = "abc"
self.tdb.text["bar"] = "def"
del self.tdb.text["foo"]
self.tdb.repack()
def test_len(self):
self.assertEqual(0, len(list(self.tdb.text)))
self.tdb.text["entry"] = "value"
self.assertEqual(1, len(list(self.tdb.text)))
def test_text_and_binary(self):
text = u'\xfa\u0148\xef\xe7\xf8\xf0\xea'
bytestr = text.encode('utf-8')
self.tdb[b"entry"] = bytestr
self.tdb.text[u"entry2"] = text
self.assertEqual(self.tdb.text["entry"], text)
self.assertEqual(self.tdb[b"entry2"], bytestr)
assert self.tdb.text.raw == self.tdb
class VersionTests(TestCase):
def test_present(self):
self.assertTrue(isinstance(tdb.__version__, str))
if __name__ == '__main__':
import unittest
unittest.TestProgram()
|