Martin v. Löwis | 1c6b1a2 | 2002-11-19 17:47:07 +0000 | [diff] [blame^] | 1 | """ |
| 2 | TestCases for checking set_get_returns_none. |
| 3 | """ |
| 4 | |
| 5 | import sys, os, string |
| 6 | import tempfile |
| 7 | from pprint import pprint |
| 8 | import unittest |
| 9 | |
| 10 | from bsddb import db |
| 11 | |
| 12 | from test.test_support import verbose |
| 13 | |
| 14 | |
| 15 | #---------------------------------------------------------------------- |
| 16 | |
| 17 | class GetReturnsNoneTestCase(unittest.TestCase): |
| 18 | def setUp(self): |
| 19 | self.filename = tempfile.mktemp() |
| 20 | |
| 21 | def tearDown(self): |
| 22 | try: |
| 23 | os.remove(self.filename) |
| 24 | except os.error: |
| 25 | pass |
| 26 | |
| 27 | |
| 28 | def test01_get_returns_none(self): |
| 29 | d = db.DB() |
| 30 | d.open(self.filename, db.DB_BTREE, db.DB_CREATE) |
| 31 | d.set_get_returns_none(1) |
| 32 | |
| 33 | for x in string.letters: |
| 34 | d.put(x, x * 40) |
| 35 | |
| 36 | data = d.get('bad key') |
| 37 | assert data == None |
| 38 | |
| 39 | data = d.get('a') |
| 40 | assert data == 'a'*40 |
| 41 | |
| 42 | count = 0 |
| 43 | c = d.cursor() |
| 44 | rec = c.first() |
| 45 | while rec: |
| 46 | count = count + 1 |
| 47 | rec = c.next() |
| 48 | |
| 49 | assert rec == None |
| 50 | assert count == 52 |
| 51 | |
| 52 | c.close() |
| 53 | d.close() |
| 54 | |
| 55 | |
| 56 | def test02_get_raises_exception(self): |
| 57 | d = db.DB() |
| 58 | d.open(self.filename, db.DB_BTREE, db.DB_CREATE) |
| 59 | d.set_get_returns_none(0) |
| 60 | |
| 61 | for x in string.letters: |
| 62 | d.put(x, x * 40) |
| 63 | |
| 64 | self.assertRaises(db.DBNotFoundError, d.get, 'bad key') |
| 65 | self.assertRaises(KeyError, d.get, 'bad key') |
| 66 | |
| 67 | data = d.get('a') |
| 68 | assert data == 'a'*40 |
| 69 | |
| 70 | count = 0 |
| 71 | exceptionHappened = 0 |
| 72 | c = d.cursor() |
| 73 | rec = c.first() |
| 74 | while rec: |
| 75 | count = count + 1 |
| 76 | try: |
| 77 | rec = c.next() |
| 78 | except db.DBNotFoundError: # end of the records |
| 79 | exceptionHappened = 1 |
| 80 | break |
| 81 | |
| 82 | assert rec != None |
| 83 | assert exceptionHappened |
| 84 | assert count == 52 |
| 85 | |
| 86 | c.close() |
| 87 | d.close() |
| 88 | |
| 89 | #---------------------------------------------------------------------- |
| 90 | |
| 91 | def suite(): |
| 92 | return unittest.makeSuite(GetReturnsNoneTestCase) |
| 93 | |
| 94 | |
| 95 | if __name__ == '__main__': |
| 96 | unittest.main( defaultTest='suite' ) |
| 97 | |
| 98 | |