r26586: Rename fetch to get for consistency with the Python dictionary interface.
[ira/wip.git] / source / 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
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(os.tmpnam(), 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_transaction_cancel(self):
86         self.tdb["bloe"] = "2"
87         self.tdb.transaction_start()
88         self.tdb["bloe"] = "1"
89         self.tdb.transaction_cancel()
90         self.assertEquals("2", self.tdb["bloe"])
91
92     def test_transaction_commit(self):
93         self.tdb["bloe"] = "2"
94         self.tdb.transaction_start()
95         self.tdb["bloe"] = "1"
96         self.tdb.transaction_commit()
97         self.assertEquals("1", self.tdb["bloe"])
98
99     def test_iterator(self):
100         self.tdb["bloe"] = "2"
101         self.tdb["bla"] = "hoi"
102         i = iter(self.tdb)
103         self.assertEquals(set(["bloe", "bla"]), set([i.next(), i.next()]))
104
105     def test_keys(self):
106         self.tdb["bloe"] = "2"
107         self.tdb["bla"] = "25"
108         self.assertEquals(["bla", "bloe"], self.tdb.keys())
109
110     def test_values(self):
111         self.tdb["bloe"] = "2"
112         self.tdb["bla"] = "25"
113         self.assertEquals(["25", "2"], self.tdb.values())
114
115     def test_clear(self):
116         self.tdb["bloe"] = "2"
117         self.tdb["bla"] = "25"
118         self.assertEquals(2, len(self.tdb))
119         self.tdb.clear()
120         self.assertEquals(0, len(self.tdb))
121
122     def test_len(self):
123         self.assertEquals(0, len(self.tdb))
124         self.tdb["entry"] = "value"
125         self.assertEquals(1, len(self.tdb))