Merge branch 'singlecompression' into single
[kai/samba-autobuild/.git] / tdb / python / tests / simple.py
1 #!/usr/bin/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     def test_nonexistant_read(self):
16         self.assertRaises(IOError, tdb.Tdb, "/some/nonexistant/file", 0, tdb.DEFAULT, os.O_RDWR)
17
18
19 class SimpleTdbTests(TestCase):
20     def setUp(self):
21         super(SimpleTdbTests, self).setUp()
22         self.tdb = tdb.Tdb(tempfile.mkstemp()[1], 0, tdb.DEFAULT, os.O_CREAT|os.O_RDWR)
23         self.assertNotEqual(None, self.tdb)
24
25     def tearDown(self):
26         del self.tdb
27
28     def test_repr(self):
29         self.assertTrue(repr(self.tdb).startswith("Tdb('"))
30
31     def test_lockall(self):
32         self.tdb.lock_all()
33
34     def test_max_dead(self):
35         self.tdb.max_dead = 20
36
37     def test_unlockall(self):
38         self.tdb.lock_all()
39         self.tdb.unlock_all()
40
41     def test_lockall_read(self):
42         self.tdb.read_lock_all()
43         self.tdb.read_unlock_all()
44
45     def test_reopen(self):
46         self.tdb.reopen()
47
48     def test_store(self):
49         self.tdb.store("bar", "bla")
50         self.assertEquals("bla", self.tdb.get("bar"))
51
52     def test_getitem(self):
53         self.tdb["bar"] = "foo"
54         self.tdb.reopen()
55         self.assertEquals("foo", self.tdb["bar"])
56
57     def test_delete(self):
58         self.tdb["bar"] = "foo"
59         del self.tdb["bar"]
60         self.assertRaises(KeyError, lambda: self.tdb["bar"])
61     
62     def test_contains(self):
63         self.tdb["bla"] = "bloe"
64         self.assertTrue("bla" in self.tdb)
65
66     def test_keyerror(self):
67         self.assertRaises(KeyError, lambda: self.tdb["bla"])
68
69     def test_hash_size(self):
70         self.tdb.hash_size
71
72     def test_map_size(self):
73         self.tdb.map_size
74
75     def test_name(self):
76         self.tdb.name
77
78     def test_iterator(self):
79         self.tdb["bla"] = "1"
80         self.tdb["brainslug"] = "2"
81         self.assertEquals(["bla", "brainslug"], list(self.tdb))
82
83     def test_items(self):
84         self.tdb["bla"] = "1"
85         self.tdb["brainslug"] = "2"
86         self.assertEquals([("bla", "1"), ("brainslug", "2")], self.tdb.items())
87
88     def test_iteritems(self):
89         self.tdb["bloe"] = "2"
90         self.tdb["bla"] = "25"
91         i = self.tdb.iteritems()
92         self.assertEquals(set([("bla", "25"), ("bloe", "2")]),
93                               set([i.next(), i.next()]))
94
95     def test_transaction_cancel(self):
96         self.tdb["bloe"] = "2"
97         self.tdb.transaction_start()
98         self.tdb["bloe"] = "1"
99         self.tdb.transaction_cancel()
100         self.assertEquals("2", self.tdb["bloe"])
101
102     def test_transaction_commit(self):
103         self.tdb["bloe"] = "2"
104         self.tdb.transaction_start()
105         self.tdb["bloe"] = "1"
106         self.tdb.transaction_commit()
107         self.assertEquals("1", self.tdb["bloe"])
108
109     def test_iterator(self):
110         self.tdb["bloe"] = "2"
111         self.tdb["bla"] = "hoi"
112         i = iter(self.tdb)
113         self.assertEquals(set(["bloe", "bla"]), set([i.next(), i.next()]))
114
115     def test_keys(self):
116         self.tdb["bloe"] = "2"
117         self.tdb["bla"] = "25"
118         self.assertEquals(["bla", "bloe"], self.tdb.keys())
119
120     def test_iterkeys(self):
121         self.tdb["bloe"] = "2"
122         self.tdb["bla"] = "25"
123         i = self.tdb.iterkeys()
124         self.assertEquals(set(["bloe", "bla"]), set([i.next(), i.next()]))
125
126     def test_values(self):
127         self.tdb["bloe"] = "2"
128         self.tdb["bla"] = "25"
129         self.assertEquals(["25", "2"], self.tdb.values())
130
131     def test_itervalues(self):
132         self.tdb["bloe"] = "2"
133         self.tdb["bla"] = "25"
134         i = self.tdb.itervalues()
135         self.assertEquals(set(["25", "2"]), set([i.next(), i.next()]))
136
137     def test_clear(self):
138         self.tdb["bloe"] = "2"
139         self.tdb["bla"] = "25"
140         self.assertEquals(2, len(self.tdb))
141         self.tdb.clear()
142         self.assertEquals(0, len(self.tdb))
143
144     def test_len(self):
145         self.assertEquals(0, len(self.tdb))
146         self.tdb["entry"] = "value"
147         self.assertEquals(1, len(self.tdb))
148
149
150 if __name__ == '__main__':
151     import unittest
152     unittest.TestProgram()