python: use '#!/usr/bin/env python' to cope with varying install locations
[sahlberg/ctdb.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     def test_nonexistant_read(self):
16         self.assertRaises(IOError, tdb.Tdb, "/some/nonexistant/file", 0, tdb.DEFAULT, os.O_RDWR)
17
18 class CloseTdbTests(TestCase):
19     def test_double_close(self):
20         self.tdb = tdb.Tdb(tempfile.mkstemp()[1], 0, tdb.DEFAULT, os.O_CREAT|os.O_RDWR)
21         self.assertNotEqual(None, self.tdb)
22
23         # ensure that double close does not crash python
24         self.tdb.close()
25         self.tdb.close()
26
27
28 class SimpleTdbTests(TestCase):
29     def setUp(self):
30         super(SimpleTdbTests, self).setUp()
31         self.tdb = tdb.Tdb(tempfile.mkstemp()[1], 0, tdb.DEFAULT, os.O_CREAT|os.O_RDWR)
32         self.assertNotEqual(None, self.tdb)
33
34     def tearDown(self):
35         del self.tdb
36
37     def test_repr(self):
38         self.assertTrue(repr(self.tdb).startswith("Tdb('"))
39
40     def test_lockall(self):
41         self.tdb.lock_all()
42
43     def test_max_dead(self):
44         self.tdb.max_dead = 20
45
46     def test_unlockall(self):
47         self.tdb.lock_all()
48         self.tdb.unlock_all()
49
50     def test_lockall_read(self):
51         self.tdb.read_lock_all()
52         self.tdb.read_unlock_all()
53
54     def test_reopen(self):
55         self.tdb.reopen()
56
57     def test_store(self):
58         self.tdb.store("bar", "bla")
59         self.assertEquals("bla", self.tdb.get("bar"))
60
61     def test_getitem(self):
62         self.tdb["bar"] = "foo"
63         self.tdb.reopen()
64         self.assertEquals("foo", self.tdb["bar"])
65
66     def test_delete(self):
67         self.tdb["bar"] = "foo"
68         del self.tdb["bar"]
69         self.assertRaises(KeyError, lambda: self.tdb["bar"])
70     
71     def test_contains(self):
72         self.tdb["bla"] = "bloe"
73         self.assertTrue("bla" in self.tdb)
74
75     def test_keyerror(self):
76         self.assertRaises(KeyError, lambda: self.tdb["bla"])
77
78     def test_hash_size(self):
79         self.tdb.hash_size
80
81     def test_map_size(self):
82         self.tdb.map_size
83
84     def test_name(self):
85         self.tdb.filename
86
87     def test_iterator(self):
88         self.tdb["bla"] = "1"
89         self.tdb["brainslug"] = "2"
90         self.assertEquals(["bla", "brainslug"], list(self.tdb))
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_iterkeys(self):
113         self.tdb["bloe"] = "2"
114         self.tdb["bla"] = "25"
115         i = self.tdb.iterkeys()
116         self.assertEquals(set(["bloe", "bla"]), set([i.next(), i.next()]))
117
118     def test_clear(self):
119         self.tdb["bloe"] = "2"
120         self.tdb["bla"] = "25"
121         self.assertEquals(2, len(list(self.tdb)))
122         self.tdb.clear()
123         self.assertEquals(0, len(list(self.tdb)))
124
125     def test_len(self):
126         self.assertEquals(0, len(list(self.tdb)))
127         self.tdb["entry"] = "value"
128         self.assertEquals(1, len(list(self.tdb)))
129
130
131 if __name__ == '__main__':
132     import unittest
133     unittest.TestProgram()