1cc51aea07fb4e0ca350ee5ab7c0c74dab423052
[samba.git] / source4 / lib / 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 Jelmer Vernooij <jelmer@samba.org>
7 # Published under the GNU LGPL
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_lockall(self):
29         self.tdb.lock_all()
30
31     def test_max_dead(self):
32         self.tdb.max_dead = 20
33
34     def test_unlockall(self):
35         self.tdb.lock_all()
36         self.tdb.unlock_all()
37
38     def test_lockall_read(self):
39         self.tdb.read_lock_all()
40         self.tdb.read_unlock_all()
41
42     def test_reopen(self):
43         self.tdb.reopen()
44
45     def test_store(self):
46         self.tdb.store("bar", "bla")
47         self.assertEquals("bla", self.tdb.get("bar"))
48
49     def test_getitem(self):
50         self.tdb["bar"] = "foo"
51         self.tdb.reopen()
52         self.assertEquals("foo", self.tdb["bar"])
53
54     def test_delete(self):
55         self.tdb["bar"] = "foo"
56         del self.tdb["bar"]
57         self.assertRaises(KeyError, lambda: self.tdb["bar"])
58     
59     def test_contains(self):
60         self.tdb["bla"] = "bloe"
61         self.assertTrue("bla" in self.tdb)
62
63     def test_keyerror(self):
64         self.assertRaises(KeyError, lambda: self.tdb["bla"])
65
66     def test_hash_size(self):
67         self.tdb.hash_size
68
69     def test_map_size(self):
70         self.tdb.map_size
71
72     def test_name(self):
73         self.tdb.name
74
75     def test_iterator(self):
76         self.tdb["bla"] = "1"
77         self.tdb["brainslug"] = "2"
78         self.assertEquals(["bla", "brainslug"], list(self.tdb))
79
80     def test_items(self):
81         self.tdb["bla"] = "1"
82         self.tdb["brainslug"] = "2"
83         self.assertEquals([("bla", "1"), ("brainslug", "2")], self.tdb.items())
84
85     def test_iteritems(self):
86         self.tdb["bloe"] = "2"
87         self.tdb["bla"] = "25"
88         i = self.tdb.iteritems()
89         self.assertEquals(set([("bla", "25"), ("bloe", "2")]),
90                               set([i.next(), i.next()]))
91
92     def test_transaction_cancel(self):
93         self.tdb["bloe"] = "2"
94         self.tdb.transaction_start()
95         self.tdb["bloe"] = "1"
96         self.tdb.transaction_cancel()
97         self.assertEquals("2", self.tdb["bloe"])
98
99     def test_transaction_commit(self):
100         self.tdb["bloe"] = "2"
101         self.tdb.transaction_start()
102         self.tdb["bloe"] = "1"
103         self.tdb.transaction_commit()
104         self.assertEquals("1", self.tdb["bloe"])
105
106     def test_iterator(self):
107         self.tdb["bloe"] = "2"
108         self.tdb["bla"] = "hoi"
109         i = iter(self.tdb)
110         self.assertEquals(set(["bloe", "bla"]), set([i.next(), i.next()]))
111
112     def test_keys(self):
113         self.tdb["bloe"] = "2"
114         self.tdb["bla"] = "25"
115         self.assertEquals(["bla", "bloe"], self.tdb.keys())
116
117     def test_iterkeys(self):
118         self.tdb["bloe"] = "2"
119         self.tdb["bla"] = "25"
120         i = self.tdb.iterkeys()
121         self.assertEquals(set(["bloe", "bla"]), set([i.next(), i.next()]))
122
123     def test_values(self):
124         self.tdb["bloe"] = "2"
125         self.tdb["bla"] = "25"
126         self.assertEquals(["25", "2"], self.tdb.values())
127
128     def test_itervalues(self):
129         self.tdb["bloe"] = "2"
130         self.tdb["bla"] = "25"
131         i = self.tdb.itervalues()
132         self.assertEquals(set(["25", "2"]), set([i.next(), i.next()]))
133
134     def test_clear(self):
135         self.tdb["bloe"] = "2"
136         self.tdb["bla"] = "25"
137         self.assertEquals(2, len(self.tdb))
138         self.tdb.clear()
139         self.assertEquals(0, len(self.tdb))
140
141     def test_len(self):
142         self.assertEquals(0, len(self.tdb))
143         self.tdb["entry"] = "value"
144         self.assertEquals(1, len(self.tdb))