1edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepfrom test import test_support
2edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepimport unittest
3edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdbm = test_support.import_module('dbm')
4edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
5edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass DbmTestCase(unittest.TestCase):
6edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
7edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def setUp(self):
8edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.filename = test_support.TESTFN
9edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.d = dbm.open(self.filename, 'c')
10edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.d.close()
11edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
12edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def tearDown(self):
13edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        for suffix in ['', '.pag', '.dir', '.db']:
14edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            test_support.unlink(self.filename + suffix)
15edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
16edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_keys(self):
17edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.d = dbm.open(self.filename, 'c')
18edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(self.d.keys(), [])
19edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        a = [('a', 'b'), ('12345678910', '019237410982340912840198242')]
20edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        for k, v in a:
21edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.d[k] = v
22edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(sorted(self.d.keys()), sorted(k for (k, v) in a))
23edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        for k, v in a:
24edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertIn(k, self.d)
25edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual(self.d[k], v)
26edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertNotIn('xxx', self.d)
27edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertRaises(KeyError, lambda: self.d['xxx'])
28edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.d.close()
29edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
30edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_modes(self):
31edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        for mode in ['r', 'rw', 'w', 'n']:
32edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            try:
33edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.d = dbm.open(self.filename, mode)
34edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.d.close()
35edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            except dbm.error:
36edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.fail()
37edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
38edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef test_main():
39edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    test_support.run_unittest(DbmTestCase)
40edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
41edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepif __name__ == '__main__':
42edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    test_main()
43