Kristján Valur Jónsson | 42a40c5 | 2009-04-01 11:28:47 +0000 | [diff] [blame] | 1 | from test import support |
| 2 | gdbm = support.import_module("dbm.gnu") #skip if not supported |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 3 | import unittest |
| 4 | import os |
Serhiy Storchaka | 6f600ff | 2018-02-26 16:02:22 +0200 | [diff] [blame] | 5 | from test.support import TESTFN, TESTFN_NONASCII, unlink |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 6 | |
Roger E. Masse | fab8ab8 | 1996-12-20 22:36:52 +0000 | [diff] [blame] | 7 | |
Guido van Rossum | f35ad33 | 2006-08-25 23:40:32 +0000 | [diff] [blame] | 8 | filename = TESTFN |
Roger E. Masse | 62a017c | 1996-12-17 19:54:27 +0000 | [diff] [blame] | 9 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10 | class TestGdbm(unittest.TestCase): |
| 11 | def setUp(self): |
| 12 | self.g = None |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 13 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 14 | def tearDown(self): |
| 15 | if self.g is not None: |
| 16 | self.g.close() |
| 17 | unlink(filename) |
Roger E. Masse | 62a017c | 1996-12-17 19:54:27 +0000 | [diff] [blame] | 18 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 19 | def test_key_methods(self): |
| 20 | self.g = gdbm.open(filename, 'c') |
| 21 | self.assertEqual(self.g.keys(), []) |
| 22 | self.g['a'] = 'b' |
| 23 | self.g['12345678910'] = '019237410982340912840198242' |
Brett Cannon | 7317c1e | 2008-11-25 19:19:17 +0000 | [diff] [blame] | 24 | self.g[b'bytes'] = b'data' |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 25 | key_set = set(self.g.keys()) |
Brett Cannon | 45dea65 | 2008-11-25 21:27:00 +0000 | [diff] [blame] | 26 | self.assertEqual(key_set, set([b'a', b'bytes', b'12345678910'])) |
Serhiy Storchaka | 7d6392c | 2013-10-25 00:06:52 +0300 | [diff] [blame] | 27 | self.assertIn('a', self.g) |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 28 | self.assertIn(b'a', self.g) |
Brett Cannon | 7317c1e | 2008-11-25 19:19:17 +0000 | [diff] [blame] | 29 | self.assertEqual(self.g[b'bytes'], b'data') |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 30 | key = self.g.firstkey() |
| 31 | while key: |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 32 | self.assertIn(key, key_set) |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 33 | key_set.remove(key) |
| 34 | key = self.g.nextkey(key) |
Georg Brandl | d9e833c | 2010-12-04 09:14:36 +0000 | [diff] [blame] | 35 | # get() and setdefault() work as in the dict interface |
Serhiy Storchaka | 2e38cc3 | 2018-04-29 12:38:06 +0300 | [diff] [blame] | 36 | self.assertEqual(self.g.get(b'a'), b'b') |
| 37 | self.assertIsNone(self.g.get(b'xxx')) |
Georg Brandl | d9e833c | 2010-12-04 09:14:36 +0000 | [diff] [blame] | 38 | self.assertEqual(self.g.get(b'xxx', b'foo'), b'foo') |
Serhiy Storchaka | 2e38cc3 | 2018-04-29 12:38:06 +0300 | [diff] [blame] | 39 | with self.assertRaises(KeyError): |
| 40 | self.g['xxx'] |
Georg Brandl | d9e833c | 2010-12-04 09:14:36 +0000 | [diff] [blame] | 41 | self.assertEqual(self.g.setdefault(b'xxx', b'foo'), b'foo') |
| 42 | self.assertEqual(self.g[b'xxx'], b'foo') |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 43 | |
| 44 | def test_error_conditions(self): |
| 45 | # Try to open a non-existent database. |
| 46 | unlink(filename) |
| 47 | self.assertRaises(gdbm.error, gdbm.open, filename, 'r') |
| 48 | # Try to access a closed database. |
| 49 | self.g = gdbm.open(filename, 'c') |
| 50 | self.g.close() |
| 51 | self.assertRaises(gdbm.error, lambda: self.g['a']) |
| 52 | # try pass an invalid open flag |
| 53 | self.assertRaises(gdbm.error, lambda: gdbm.open(filename, 'rx').close()) |
| 54 | |
| 55 | def test_flags(self): |
| 56 | # Test the flag parameter open() by trying all supported flag modes. |
| 57 | all = set(gdbm.open_flags) |
| 58 | # Test standard flags (presumably "crwn"). |
| 59 | modes = all - set('fsu') |
Georg Brandl | 16684eb | 2012-02-20 22:48:06 +0100 | [diff] [blame] | 60 | for mode in sorted(modes): # put "c" mode first |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 61 | self.g = gdbm.open(filename, mode) |
| 62 | self.g.close() |
| 63 | |
| 64 | # Test additional flags (presumably "fsu"). |
| 65 | flags = all - set('crwn') |
| 66 | for mode in modes: |
| 67 | for flag in flags: |
| 68 | self.g = gdbm.open(filename, mode + flag) |
| 69 | self.g.close() |
| 70 | |
| 71 | def test_reorganize(self): |
| 72 | self.g = gdbm.open(filename, 'c') |
| 73 | size0 = os.path.getsize(filename) |
| 74 | |
| 75 | self.g['x'] = 'x' * 10000 |
| 76 | size1 = os.path.getsize(filename) |
Serhiy Storchaka | 22525de | 2018-06-19 13:31:48 +0300 | [diff] [blame^] | 77 | self.assertGreater(size1, size0) |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 78 | |
| 79 | del self.g['x'] |
| 80 | # 'size' is supposed to be the same even after deleting an entry. |
| 81 | self.assertEqual(os.path.getsize(filename), size1) |
| 82 | |
| 83 | self.g.reorganize() |
| 84 | size2 = os.path.getsize(filename) |
Serhiy Storchaka | 22525de | 2018-06-19 13:31:48 +0300 | [diff] [blame^] | 85 | self.assertLess(size2, size1) |
| 86 | self.assertGreaterEqual(size2, size0) |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 87 | |
Nick Coghlan | c610aba | 2013-11-17 15:59:51 +1000 | [diff] [blame] | 88 | def test_context_manager(self): |
| 89 | with gdbm.open(filename, 'c') as db: |
| 90 | db["gdbm context manager"] = "context manager" |
| 91 | |
| 92 | with gdbm.open(filename, 'r') as db: |
| 93 | self.assertEqual(list(db.keys()), [b"gdbm context manager"]) |
| 94 | |
| 95 | with self.assertRaises(gdbm.error) as cm: |
| 96 | db.keys() |
| 97 | self.assertEqual(str(cm.exception), |
| 98 | "GDBM object has already been closed") |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 99 | |
Serhiy Storchaka | 6f600ff | 2018-02-26 16:02:22 +0200 | [diff] [blame] | 100 | def test_bytes(self): |
| 101 | with gdbm.open(filename, 'c') as db: |
| 102 | db[b'bytes key \xbd'] = b'bytes value \xbd' |
| 103 | with gdbm.open(filename, 'r') as db: |
| 104 | self.assertEqual(list(db.keys()), [b'bytes key \xbd']) |
| 105 | self.assertTrue(b'bytes key \xbd' in db) |
| 106 | self.assertEqual(db[b'bytes key \xbd'], b'bytes value \xbd') |
| 107 | |
| 108 | def test_unicode(self): |
| 109 | with gdbm.open(filename, 'c') as db: |
| 110 | db['Unicode key \U0001f40d'] = 'Unicode value \U0001f40d' |
| 111 | with gdbm.open(filename, 'r') as db: |
| 112 | self.assertEqual(list(db.keys()), ['Unicode key \U0001f40d'.encode()]) |
| 113 | self.assertTrue('Unicode key \U0001f40d'.encode() in db) |
| 114 | self.assertTrue('Unicode key \U0001f40d' in db) |
| 115 | self.assertEqual(db['Unicode key \U0001f40d'.encode()], |
| 116 | 'Unicode value \U0001f40d'.encode()) |
| 117 | self.assertEqual(db['Unicode key \U0001f40d'], |
| 118 | 'Unicode value \U0001f40d'.encode()) |
| 119 | |
| 120 | @unittest.skipUnless(TESTFN_NONASCII, |
| 121 | 'requires OS support of non-ASCII encodings') |
| 122 | def test_nonascii_filename(self): |
| 123 | filename = TESTFN_NONASCII |
| 124 | self.addCleanup(unlink, filename) |
| 125 | with gdbm.open(filename, 'c') as db: |
| 126 | db[b'key'] = b'value' |
| 127 | self.assertTrue(os.path.exists(filename)) |
| 128 | with gdbm.open(filename, 'r') as db: |
| 129 | self.assertEqual(list(db.keys()), [b'key']) |
| 130 | self.assertTrue(b'key' in db) |
| 131 | self.assertEqual(db[b'key'], b'value') |
| 132 | |
| 133 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 134 | if __name__ == '__main__': |
Ezio Melotti | f79493b | 2013-03-01 11:23:28 +0200 | [diff] [blame] | 135 | unittest.main() |