Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 1 | from test import support |
Kristján Valur Jónsson | 42a40c5 | 2009-04-01 11:28:47 +0000 | [diff] [blame] | 2 | support.import_module("dbm.ndbm") #skip if not supported |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 3 | import unittest |
Neal Norwitz | e7629c8 | 2002-11-02 18:25:08 +0000 | [diff] [blame] | 4 | import os |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 5 | import random |
Georg Brandl | 0a7ac7d | 2008-05-26 10:29:35 +0000 | [diff] [blame] | 6 | import dbm.ndbm |
| 7 | from dbm.ndbm import error |
Roger E. Masse | fab8ab8 | 1996-12-20 22:36:52 +0000 | [diff] [blame] | 8 | |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 9 | class DbmTestCase(unittest.TestCase): |
Roger E. Masse | 16f9fd2 | 1996-12-10 00:06:24 +0000 | [diff] [blame] | 10 | |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 11 | def setUp(self): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 12 | self.filename = support.TESTFN |
Georg Brandl | 0a7ac7d | 2008-05-26 10:29:35 +0000 | [diff] [blame] | 13 | self.d = dbm.ndbm.open(self.filename, 'c') |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 14 | self.d.close() |
Fred Drake | 57a4e90 | 2000-09-18 17:56:58 +0000 | [diff] [blame] | 15 | |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 16 | def tearDown(self): |
| 17 | for suffix in ['', '.pag', '.dir', '.db']: |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 18 | support.unlink(self.filename + suffix) |
Roger E. Masse | 16f9fd2 | 1996-12-10 00:06:24 +0000 | [diff] [blame] | 19 | |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 20 | def test_keys(self): |
Georg Brandl | 0a7ac7d | 2008-05-26 10:29:35 +0000 | [diff] [blame] | 21 | self.d = dbm.ndbm.open(self.filename, 'c') |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 22 | self.assertTrue(self.d.keys() == []) |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 23 | self.d['a'] = 'b' |
Brett Cannon | 7317c1e | 2008-11-25 19:19:17 +0000 | [diff] [blame] | 24 | self.d[b'bytes'] = b'data' |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 25 | self.d['12345678910'] = '019237410982340912840198242' |
| 26 | self.d.keys() |
Serhiy Storchaka | 9da33ab | 2013-10-24 23:59:28 +0300 | [diff] [blame^] | 27 | self.assertIn('a', self.d) |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 28 | self.assertIn(b'a', self.d) |
Brett Cannon | 7317c1e | 2008-11-25 19:19:17 +0000 | [diff] [blame] | 29 | self.assertEqual(self.d[b'bytes'], b'data') |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 30 | self.d.close() |
Neal Norwitz | e7629c8 | 2002-11-02 18:25:08 +0000 | [diff] [blame] | 31 | |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 32 | def test_modes(self): |
| 33 | for mode in ['r', 'rw', 'w', 'n']: |
| 34 | try: |
Georg Brandl | 0a7ac7d | 2008-05-26 10:29:35 +0000 | [diff] [blame] | 35 | self.d = dbm.ndbm.open(self.filename, mode) |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 36 | self.d.close() |
Georg Brandl | 0a7ac7d | 2008-05-26 10:29:35 +0000 | [diff] [blame] | 37 | except error: |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 38 | self.fail() |
Neal Norwitz | e7629c8 | 2002-11-02 18:25:08 +0000 | [diff] [blame] | 39 | |
Christian Heimes | 180510d | 2008-03-03 19:15:45 +0000 | [diff] [blame] | 40 | if __name__ == '__main__': |
Ezio Melotti | f79493b | 2013-03-01 11:23:28 +0200 | [diff] [blame] | 41 | unittest.main() |