tdb2: create tdb2 versions of various testing TDBs.
[bbaumbach/samba-autobuild/.git] / lib / tdb / python / tests / simple.py
1 #!/usr/bin/env python
2 # Some simple tests for the Python bindings for TDB
3 # Note that this tests the interface of the Python bindings
4 # It does not test tdb itself.
5 #
6 # Copyright (C) 2007-2008 Jelmer Vernooij <jelmer@samba.org>
7 # Published under the GNU LGPLv3 or later
8
9 import tdb
10 from unittest import TestCase
11 import os, tempfile
12
13
14 class OpenTdbTests(TestCase):
15
16     def test_nonexistant_read(self):
17         self.assertRaises(IOError, tdb.Tdb, "/some/nonexistant/file", 0,
18                 tdb.DEFAULT, os.O_RDWR)
19
20 class CloseTdbTests(TestCase):
21
22     def test_double_close(self):
23         # No hash size in tdb2.
24         if tdb.__version__.startswith("2"):
25             self.tdb = tdb.Tdb(tempfile.mkstemp()[1], tdb.DEFAULT,
26                                os.O_CREAT|os.O_RDWR)
27         else:
28             self.tdb = tdb.Tdb(tempfile.mkstemp()[1], 0, tdb.DEFAULT,
29                                os.O_CREAT|os.O_RDWR)
30         self.assertNotEqual(None, self.tdb)
31
32         # ensure that double close does not crash python
33         self.tdb.close()
34         self.tdb.close()
35
36
37 class InternalTdbTests(TestCase):
38
39     def test_repr(self):
40         self.tdb = tdb.Tdb()
41
42         # repr used to crash on internal db
43         self.assertEquals(repr(self.tdb), "Tdb(<internal>)")
44
45
46 class SimpleTdbTests(TestCase):
47
48     def setUp(self):
49         super(SimpleTdbTests, self).setUp()
50         if tdb.__version__.startswith("2"):
51             self.tdb = tdb.Tdb(tempfile.mkstemp()[1], tdb.DEFAULT,
52                                os.O_CREAT|os.O_RDWR)
53         else:
54             self.tdb = tdb.Tdb(tempfile.mkstemp()[1], 0, tdb.DEFAULT,
55                                os.O_CREAT|os.O_RDWR)
56         self.assertNotEqual(None, self.tdb)
57
58     def tearDown(self):
59         del self.tdb
60
61     def test_repr(self):
62         self.assertTrue(repr(self.tdb).startswith("Tdb('"))
63
64     def test_lockall(self):
65         self.tdb.lock_all()
66
67     def test_max_dead(self):
68         if not tdb.__version__.startswith("2"):
69             self.tdb.max_dead = 20
70
71     def test_unlockall(self):
72         self.tdb.lock_all()
73         self.tdb.unlock_all()
74
75     def test_lockall_read(self):
76         self.tdb.read_lock_all()
77         self.tdb.read_unlock_all()
78
79     def test_reopen(self):
80         if not tdb.__version__.startswith("2"):
81             self.tdb.reopen()
82
83     def test_store(self):
84         self.tdb.store("bar", "bla")
85         self.assertEquals("bla", self.tdb.get("bar"))
86
87     def test_getitem(self):
88         self.tdb["bar"] = "foo"
89         if not tdb.__version__.startswith("2"):
90             self.tdb.reopen()
91         self.assertEquals("foo", self.tdb["bar"])
92
93     def test_delete(self):
94         self.tdb["bar"] = "foo"
95         del self.tdb["bar"]
96         self.assertRaises(KeyError, lambda: self.tdb["bar"])
97     
98     def test_contains(self):
99         self.tdb["bla"] = "bloe"
100         self.assertTrue("bla" in self.tdb)
101
102     def test_keyerror(self):
103         self.assertRaises(KeyError, lambda: self.tdb["bla"])
104
105     def test_hash_size(self):
106         if not tdb.__version__.startswith("2"):
107             self.tdb.hash_size
108
109     def test_map_size(self):
110         if not tdb.__version__.startswith("2"):
111             self.tdb.map_size
112
113     def test_freelist_size(self):
114         if not tdb.__version__.startswith("2"):
115             self.tdb.freelist_size
116
117     def test_name(self):
118         self.tdb.filename
119
120     def test_iterator(self):
121         self.tdb["bla"] = "1"
122         self.tdb["brainslug"] = "2"
123         l = list(self.tdb)
124         l.sort()
125         self.assertEquals(["bla", "brainslug"], l)
126
127     def test_transaction_cancel(self):
128         self.tdb["bloe"] = "2"
129         self.tdb.transaction_start()
130         self.tdb["bloe"] = "1"
131         self.tdb.transaction_cancel()
132         self.assertEquals("2", self.tdb["bloe"])
133
134     def test_transaction_commit(self):
135         self.tdb["bloe"] = "2"
136         self.tdb.transaction_start()
137         self.tdb["bloe"] = "1"
138         self.tdb.transaction_commit()
139         self.assertEquals("1", self.tdb["bloe"])
140
141     def test_transaction_prepare_commit(self):
142         self.tdb["bloe"] = "2"
143         self.tdb.transaction_start()
144         self.tdb["bloe"] = "1"
145         self.tdb.transaction_prepare_commit()
146         self.tdb.transaction_commit()
147         self.assertEquals("1", self.tdb["bloe"])
148
149     def test_iterkeys(self):
150         self.tdb["bloe"] = "2"
151         self.tdb["bla"] = "25"
152         i = self.tdb.iterkeys()
153         self.assertEquals(set(["bloe", "bla"]), set([i.next(), i.next()]))
154
155     def test_clear(self):
156         self.tdb["bloe"] = "2"
157         self.tdb["bla"] = "25"
158         self.assertEquals(2, len(list(self.tdb)))
159         self.tdb.clear()
160         self.assertEquals(0, len(list(self.tdb)))
161
162     def test_repack(self):
163         if not tdb.__version__.startswith("2"):
164             self.tdb["foo"] = "abc"
165             self.tdb["bar"] = "def"
166             del self.tdb["foo"]
167             self.tdb.repack()
168
169     def test_seqnum(self):
170         if not tdb.__version__.startswith("2"):
171             self.tdb.enable_seqnum()
172             seq1 = self.tdb.seqnum
173             self.tdb.increment_seqnum_nonblock()
174             seq2 = self.tdb.seqnum
175             self.assertEquals(seq2-seq1, 1)
176
177     def test_len(self):
178         self.assertEquals(0, len(list(self.tdb)))
179         self.tdb["entry"] = "value"
180         self.assertEquals(1, len(list(self.tdb)))
181
182     def test_add_flags(self):
183         if tdb.__version__.startswith("2"):
184             self.tdb.add_flag(tdb.NOMMAP)
185             self.tdb.remove_flag(tdb.NOMMAP)
186         else:
187             self.tdb.add_flags(tdb.NOMMAP)
188             self.tdb.remove_flags(tdb.NOMMAP)
189
190
191 class VersionTests(TestCase):
192
193     def test_present(self):
194         self.assertTrue(isinstance(tdb.__version__, str))
195
196
197 if __name__ == '__main__':
198     import unittest
199     unittest.TestProgram()