ReadOnlyStorage.py 2.48 KB
Newer Older
Jeremy Hylton's avatar
Jeremy Hylton committed
1 2 3 4
##############################################################################
#
# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
# All Rights Reserved.
5
#
Jeremy Hylton's avatar
Jeremy Hylton committed
6
# This software is subject to the provisions of the Zope Public License,
Jim Fulton's avatar
Jim Fulton committed
7
# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
Jeremy Hylton's avatar
Jeremy Hylton committed
8 9 10 11
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
12
#
Jeremy Hylton's avatar
Jeremy Hylton committed
13
##############################################################################
14
from ZODB.POSException import ReadOnlyError, Unsupported
15
import transaction
16 17 18 19 20 21 22 23 24 25 26 27 28

class ReadOnlyStorage:

    def _create_data(self):
        # test a read-only storage that already has some data
        self.oids = {}
        for i in range(10):
            oid = self._storage.new_oid()
            revid = self._dostore(oid)
            self.oids[oid] = revid

    def _make_readonly(self):
        self._storage.close()
Jeremy Hylton's avatar
Jeremy Hylton committed
29
        self.open(read_only=True)
30 31 32 33 34
        self.assert_(self._storage.isReadOnly())

    def checkReadMethods(self):
        self._create_data()
        self._make_readonly()
35
        # Note that this doesn't check _all_ read methods.
36 37 38 39
        for oid in self.oids.keys():
            data, revid = self._storage.load(oid, '')
            self.assertEqual(revid, self.oids[oid])
            self.assert_(not self._storage.modifiedInVersion(oid))
40 41 42 43 44 45
            # Storages without revisions may not have loadSerial().
            try:
                _data = self._storage.loadSerial(oid, revid)
                self.assertEqual(data, _data)
            except Unsupported:
                pass
46 47 48 49

    def checkWriteMethods(self):
        self._make_readonly()
        self.assertRaises(ReadOnlyError, self._storage.new_oid)
50
        t = transaction.Transaction()
51 52
        self.assertRaises(ReadOnlyError, self._storage.tpc_begin, t)

53 54 55 56 57 58
        if self._storage.supportsVersions():
            self.assertRaises(ReadOnlyError, self._storage.abortVersion,
                              '', t)
            self.assertRaises(ReadOnlyError, self._storage.commitVersion,
                              '', '', t)

59 60 61 62
        self.assertRaises(ReadOnlyError, self._storage.store,
                          '\000' * 8, None, '', '', t)

        if self._storage.supportsTransactionalUndo():
63
            self.assertRaises(ReadOnlyError, self._storage.undo,
64
                              '\000' * 8, t)