• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1"""
2TestCases for checking set_get_returns_none.
3"""
4
5import os, string
6import unittest
7
8from test_all import db, verbose, get_new_database_path
9
10
11#----------------------------------------------------------------------
12
13class GetReturnsNoneTestCase(unittest.TestCase):
14    def setUp(self):
15        self.filename = get_new_database_path()
16
17    def tearDown(self):
18        try:
19            os.remove(self.filename)
20        except os.error:
21            pass
22
23
24    def test01_get_returns_none(self):
25        d = db.DB()
26        d.open(self.filename, db.DB_BTREE, db.DB_CREATE)
27        d.set_get_returns_none(1)
28
29        for x in string.ascii_letters:
30            d.put(x, x * 40)
31
32        data = d.get('bad key')
33        self.assertEqual(data, None)
34
35        data = d.get(string.ascii_letters[0])
36        self.assertEqual(data, string.ascii_letters[0]*40)
37
38        count = 0
39        c = d.cursor()
40        rec = c.first()
41        while rec:
42            count = count + 1
43            rec = c.next()
44
45        self.assertEqual(rec, None)
46        self.assertEqual(count, len(string.ascii_letters))
47
48        c.close()
49        d.close()
50
51
52    def test02_get_raises_exception(self):
53        d = db.DB()
54        d.open(self.filename, db.DB_BTREE, db.DB_CREATE)
55        d.set_get_returns_none(0)
56
57        for x in string.ascii_letters:
58            d.put(x, x * 40)
59
60        self.assertRaises(db.DBNotFoundError, d.get, 'bad key')
61        self.assertRaises(KeyError, d.get, 'bad key')
62
63        data = d.get(string.ascii_letters[0])
64        self.assertEqual(data, string.ascii_letters[0]*40)
65
66        count = 0
67        exceptionHappened = 0
68        c = d.cursor()
69        rec = c.first()
70        while rec:
71            count = count + 1
72            try:
73                rec = c.next()
74            except db.DBNotFoundError:  # end of the records
75                exceptionHappened = 1
76                break
77
78        self.assertNotEqual(rec, None)
79        self.assertTrue(exceptionHappened)
80        self.assertEqual(count, len(string.ascii_letters))
81
82        c.close()
83        d.close()
84
85#----------------------------------------------------------------------
86
87def test_suite():
88    return unittest.makeSuite(GetReturnsNoneTestCase)
89
90
91if __name__ == '__main__':
92    unittest.main(defaultTest='test_suite')
93