ccan/ntdb: demote to junkcode.
[ccan] / junkcode / rusty@rustcorp.com.au-ntdb / test / python-api.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-2013 Jelmer Vernooij <jelmer@samba.org>
7 # Published under the GNU LGPLv3 or later
8
9 import ntdb
10 from unittest import TestCase
11 import os, tempfile
12
13
14 class OpenTdbTests(TestCase):
15
16     def test_nonexistent_read(self):
17         self.assertRaises(IOError, ntdb.Ntdb, "/some/nonexistent/file", 0,
18                 ntdb.DEFAULT, os.O_RDWR)
19
20 class CloseTdbTests(TestCase):
21
22     def test_double_close(self):
23         self.ntdb = ntdb.Ntdb(tempfile.mkstemp()[1], ntdb.DEFAULT,
24                            os.O_CREAT|os.O_RDWR)
25         self.assertNotEqual(None, self.ntdb)
26
27         # ensure that double close does not crash python
28         self.ntdb.close()
29         self.ntdb.close()
30
31         # Check that further operations do not crash python
32         self.assertRaises(RuntimeError, lambda: self.ntdb.transaction_start())
33
34         self.assertRaises(RuntimeError, lambda: self.ntdb["bar"])
35
36
37 class InternalTdbTests(TestCase):
38
39     def test_repr(self):
40         self.ntdb = ntdb.Ntdb()
41
42         # repr used to crash on internal db
43         self.assertEquals(repr(self.ntdb), "Ntdb(<internal>)")
44
45
46 class SimpleTdbTests(TestCase):
47
48     def setUp(self):
49         super(SimpleTdbTests, self).setUp()
50         self.ntdb = ntdb.Ntdb(tempfile.mkstemp()[1], ntdb.DEFAULT,
51                            os.O_CREAT|os.O_RDWR)
52         self.assertNotEqual(None, self.ntdb)
53
54     def tearDown(self):
55         del self.ntdb
56
57     def test_repr(self):
58         self.assertTrue(repr(self.ntdb).startswith("Ntdb('"))
59
60     def test_lockall(self):
61         self.ntdb.lock_all()
62
63     def test_unlockall(self):
64         self.ntdb.lock_all()
65         self.ntdb.unlock_all()
66
67     def test_lockall_read(self):
68         self.ntdb.read_lock_all()
69         self.ntdb.read_unlock_all()
70
71     def test_store(self):
72         self.ntdb.store("bar", "bla")
73         self.assertEquals("bla", self.ntdb.get("bar"))
74
75     def test_getitem(self):
76         self.ntdb["bar"] = "foo"
77         self.assertEquals("foo", self.ntdb["bar"])
78
79     def test_delete(self):
80         self.ntdb["bar"] = "foo"
81         del self.ntdb["bar"]
82         self.assertRaises(KeyError, lambda: self.ntdb["bar"])
83
84     def test_contains(self):
85         self.ntdb["bla"] = "bloe"
86         self.assertTrue("bla" in self.ntdb)
87
88     def test_keyerror(self):
89         self.assertRaises(KeyError, lambda: self.ntdb["bla"])
90
91     def test_name(self):
92         self.ntdb.filename
93
94     def test_iterator(self):
95         self.ntdb["bla"] = "1"
96         self.ntdb["brainslug"] = "2"
97         l = list(self.ntdb)
98         l.sort()
99         self.assertEquals(["bla", "brainslug"], l)
100
101     def test_transaction_cancel(self):
102         self.ntdb["bloe"] = "2"
103         self.ntdb.transaction_start()
104         self.ntdb["bloe"] = "1"
105         self.ntdb.transaction_cancel()
106         self.assertEquals("2", self.ntdb["bloe"])
107
108     def test_transaction_commit(self):
109         self.ntdb["bloe"] = "2"
110         self.ntdb.transaction_start()
111         self.ntdb["bloe"] = "1"
112         self.ntdb.transaction_commit()
113         self.assertEquals("1", self.ntdb["bloe"])
114
115     def test_transaction_prepare_commit(self):
116         self.ntdb["bloe"] = "2"
117         self.ntdb.transaction_start()
118         self.ntdb["bloe"] = "1"
119         self.ntdb.transaction_prepare_commit()
120         self.ntdb.transaction_commit()
121         self.assertEquals("1", self.ntdb["bloe"])
122
123     def test_iterkeys(self):
124         self.ntdb["bloe"] = "2"
125         self.ntdb["bla"] = "25"
126         i = self.ntdb.iterkeys()
127         self.assertEquals(set(["bloe", "bla"]), set([i.next(), i.next()]))
128
129     def test_clear(self):
130         self.ntdb["bloe"] = "2"
131         self.ntdb["bla"] = "25"
132         self.assertEquals(2, len(list(self.ntdb)))
133         self.ntdb.clear()
134         self.assertEquals(0, len(list(self.ntdb)))
135
136     def test_len(self):
137         self.assertEquals(0, len(list(self.ntdb)))
138         self.ntdb["entry"] = "value"
139         self.assertEquals(1, len(list(self.ntdb)))
140
141     def test_add_flags(self):
142         self.ntdb.add_flag(ntdb.NOMMAP)
143         self.ntdb.remove_flag(ntdb.NOMMAP)
144
145
146 class VersionTests(TestCase):
147
148     def test_present(self):
149         self.assertTrue(isinstance(ntdb.__version__, str))
150
151
152 if __name__ == '__main__':
153     import unittest
154     unittest.TestProgram()