1"""
2Copyright (c) 2008-2020, Jesus Cea Avion <jcea@jcea.es>
3All rights reserved.
4
5Redistribution and use in source and binary forms, with or without
6modification, are permitted provided that the following conditions
7are met:
8
9    1. Redistributions of source code must retain the above copyright
10    notice, this list of conditions and the following disclaimer.
11
12    2. Redistributions in binary form must reproduce the above
13    copyright notice, this list of conditions and the following
14    disclaimer in the documentation and/or other materials provided
15    with the distribution.
16
17    3. Neither the name of Jesus Cea Avion nor the names of its
18    contributors may be used to endorse or promote products derived
19    from this software without specific prior written permission.
20
21    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
22    CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
23    INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
24    MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25    DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
26    BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
27    EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
28            TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29            DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
30    ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
31    TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
32    THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33    SUCH DAMAGE.
34    """
35
36import os
37import pickle
38import sys
39
40if sys.version_info[0] < 3 :
41    try:
42        import cPickle
43    except ImportError:
44        cPickle = None
45else :
46    cPickle = None
47
48import unittest
49
50from test_all import db, test_support, get_new_environment_path, get_new_database_path
51
52#----------------------------------------------------------------------
53
54class pickleTestCase(unittest.TestCase):
55    """Verify that DBError can be pickled and unpickled"""
56    db_name = 'test-dbobj.db'
57
58    def setUp(self):
59        self.homeDir = get_new_environment_path()
60
61    def tearDown(self):
62        if hasattr(self, 'db'):
63            del self.db
64        if hasattr(self, 'env'):
65            del self.env
66        test_support.rmtree(self.homeDir)
67
68    def _base_test_pickle_DBError(self, pickle):
69        self.env = db.DBEnv()
70        self.env.open(self.homeDir, db.DB_CREATE | db.DB_INIT_MPOOL)
71        self.db = db.DB(self.env)
72        self.db.open(self.db_name, db.DB_HASH, db.DB_CREATE)
73        self.db.put('spam', 'eggs')
74        self.assertEqual(self.db['spam'], 'eggs')
75        try:
76            self.db.put('spam', 'ham', flags=db.DB_NOOVERWRITE)
77        except db.DBError, egg:
78            pickledEgg = pickle.dumps(egg)
79            #print repr(pickledEgg)
80            rottenEgg = pickle.loads(pickledEgg)
81            if rottenEgg.args != egg.args or type(rottenEgg) != type(egg):
82                raise Exception, (rottenEgg, '!=', egg)
83        else:
84            raise Exception, "where's my DBError exception?!?"
85
86        self.db.close()
87        self.env.close()
88
89    def test01_pickle_DBError(self):
90        self._base_test_pickle_DBError(pickle=pickle)
91
92    if cPickle:
93        def test02_cPickle_DBError(self):
94            self._base_test_pickle_DBError(pickle=cPickle)
95
96#----------------------------------------------------------------------
97
98def test_suite():
99    return unittest.makeSuite(pickleTestCase)
100
101if __name__ == '__main__':
102    unittest.main(defaultTest='test_suite')
103