Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(267232)

Delta Between Two Patch Sets: Lib/test/test_dbm_gnu.py

Issue 16510: Using appropriate checks in tests
Left Patch Set: Created 6 years, 9 months ago
Right Patch Set: Created 5 years, 6 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « Lib/test/test_crypt.py ('k') | Lib/test/test_dbm.py » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 from test import support 1 from test import support
2 gdbm = support.import_module("dbm.gnu") #skip if not supported 2 gdbm = support.import_module("dbm.gnu") #skip if not supported
3 import unittest 3 import unittest
4 import os 4 import os
5 from test.support import verbose, TESTFN, run_unittest, unlink 5 from test.support import verbose, TESTFN, unlink
6 6
7 7
8 filename = TESTFN 8 filename = TESTFN
9 9
10 class TestGdbm(unittest.TestCase): 10 class TestGdbm(unittest.TestCase):
11 def setUp(self): 11 def setUp(self):
12 self.g = None 12 self.g = None
13 13
14 def tearDown(self): 14 def tearDown(self):
15 if self.g is not None: 15 if self.g is not None:
16 self.g.close() 16 self.g.close()
17 unlink(filename) 17 unlink(filename)
18 18
19 def test_key_methods(self): 19 def test_key_methods(self):
20 self.g = gdbm.open(filename, 'c') 20 self.g = gdbm.open(filename, 'c')
21 self.assertEqual(self.g.keys(), []) 21 self.assertEqual(self.g.keys(), [])
22 self.g['a'] = 'b' 22 self.g['a'] = 'b'
23 self.g['12345678910'] = '019237410982340912840198242' 23 self.g['12345678910'] = '019237410982340912840198242'
24 self.g[b'bytes'] = b'data' 24 self.g[b'bytes'] = b'data'
25 key_set = set(self.g.keys()) 25 key_set = set(self.g.keys())
26 self.assertEqual(key_set, set([b'a', b'bytes', b'12345678910'])) 26 self.assertEqual(key_set, set([b'a', b'bytes', b'12345678910']))
27 self.assertIn('a', self.g)
27 self.assertIn(b'a', self.g) 28 self.assertIn(b'a', self.g)
28 self.assertEqual(self.g[b'bytes'], b'data') 29 self.assertEqual(self.g[b'bytes'], b'data')
29 key = self.g.firstkey() 30 key = self.g.firstkey()
30 while key: 31 while key:
31 self.assertIn(key, key_set) 32 self.assertIn(key, key_set)
32 key_set.remove(key) 33 key_set.remove(key)
33 key = self.g.nextkey(key) 34 key = self.g.nextkey(key)
34 self.assertRaises(KeyError, lambda: self.g['xxx']) 35 self.assertRaises(KeyError, lambda: self.g['xxx'])
35 # get() and setdefault() work as in the dict interface 36 # get() and setdefault() work as in the dict interface
36 self.assertEqual(self.g.get(b'xxx', b'foo'), b'foo') 37 self.assertEqual(self.g.get(b'xxx', b'foo'), b'foo')
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
73 self.assertLess(size0, size1) 74 self.assertLess(size0, size1)
74 75
75 del self.g['x'] 76 del self.g['x']
76 # 'size' is supposed to be the same even after deleting an entry. 77 # 'size' is supposed to be the same even after deleting an entry.
77 self.assertEqual(os.path.getsize(filename), size1) 78 self.assertEqual(os.path.getsize(filename), size1)
78 79
79 self.g.reorganize() 80 self.g.reorganize()
80 size2 = os.path.getsize(filename) 81 size2 = os.path.getsize(filename)
81 self.assertTrue(size1 > size2 >= size0) 82 self.assertTrue(size1 > size2 >= size0)
82 83
84 def test_context_manager(self):
85 with gdbm.open(filename, 'c') as db:
86 db["gdbm context manager"] = "context manager"
83 87
84 def test_main(): 88 with gdbm.open(filename, 'r') as db:
85 run_unittest(TestGdbm) 89 self.assertEqual(list(db.keys()), [b"gdbm context manager"])
90
91 with self.assertRaises(gdbm.error) as cm:
92 db.keys()
93 self.assertEqual(str(cm.exception),
94 "GDBM object has already been closed")
86 95
87 if __name__ == '__main__': 96 if __name__ == '__main__':
88 test_main() 97 unittest.main()
LEFTRIGHT

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+