/usr/share/pyshared/ZODB/tests/testPersistentMapping.py is in python-zodb 1:3.10.5-0ubuntu3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 | ##############################################################################
#
# Copyright (c) 2001, 2002 Zope Foundation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
# 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.
#
##############################################################################
"""Verify that PersistentMapping works with old versions of Zope.
The comments in PersistentMapping.py address the issue in some detail.
The pickled form of a PersistentMapping must use _container to store
the actual mapping, because old versions of Zope used this attribute.
If the new code doesn't generate pickles that are consistent with the
old code, developers will have a hard time testing the new code.
"""
import unittest
import transaction
from transaction import Transaction
import ZODB
from ZODB.MappingStorage import MappingStorage
import cPickle
import cStringIO
import sys
# This pickle contains a persistent mapping pickle created from the
# old code.
pickle = ('((U\x0bPersistenceq\x01U\x11PersistentMappingtq\x02Nt.}q\x03U\n'
'_containerq\x04}q\x05U\x07versionq\x06U\x03oldq\x07ss.\n')
class PMTests(unittest.TestCase):
def checkOldStyleRoot(self):
# The Persistence module doesn't exist in Zope3's idea of what ZODB
# is, but the global `pickle` references it explicitly. So just
# bail if Persistence isn't available.
try:
import Persistence
except ImportError:
return
# insert the pickle in place of the root
s = MappingStorage()
t = Transaction()
s.tpc_begin(t)
s.store('\000' * 8, None, pickle, '', t)
s.tpc_vote(t)
s.tpc_finish(t)
db = ZODB.DB(s)
# If the root can be loaded successfully, we should be okay.
r = db.open().root()
# But make sure it looks like a new mapping
self.assert_(hasattr(r, 'data'))
self.assert_(not hasattr(r, '_container'))
# TODO: This test fails in ZODB 3.3a1. It's making some assumption(s)
# about pickles that aren't true. Hard to say when it stopped working,
# because this entire test suite hasn't been run for a long time, due to
# a mysterious "return None" at the start of the test_suite() function
# below. I noticed that when the new checkBackwardCompat() test wasn't
# getting run.
def TODO_checkNewPicklesAreSafe(self):
s = MappingStorage()
db = ZODB.DB(s)
r = db.open().root()
r[1] = 1
r[2] = 2
r[3] = r
transaction.commit()
# MappingStorage stores serialno + pickle in its _index.
root_pickle = s._index['\000' * 8][8:]
f = cStringIO.StringIO(root_pickle)
u = cPickle.Unpickler(f)
klass_info = u.load()
klass = find_global(*klass_info[0])
inst = klass.__new__(klass)
state = u.load()
inst.__setstate__(state)
self.assert_(hasattr(inst, '_container'))
self.assert_(not hasattr(inst, 'data'))
def checkBackwardCompat(self):
# Verify that the sanest of the ZODB 3.2 dotted paths still works.
from persistent.mapping import PersistentMapping as newPath
from ZODB.PersistentMapping import PersistentMapping as oldPath
self.assert_(oldPath is newPath)
def checkBasicOps(self):
from persistent.mapping import PersistentMapping
m = PersistentMapping({'x': 1}, a=2, b=3)
m['name'] = 'bob'
self.assertEqual(m['name'], "bob")
self.assertEqual(m.get('name', 42), "bob")
self.assert_('name' in m)
try:
m['fred']
except KeyError:
pass
else:
self.fail("expected KeyError")
self.assert_('fred' not in m)
self.assertEqual(m.get('fred'), None)
self.assertEqual(m.get('fred', 42), 42)
keys = m.keys()
keys.sort()
self.assertEqual(keys, ['a', 'b', 'name', 'x'])
values = m.values()
values.sort()
self.assertEqual(values, [1, 2, 3, 'bob'])
items = m.items()
items.sort()
self.assertEqual(items,
[('a', 2), ('b', 3), ('name', 'bob'), ('x', 1)])
keys = list(m.iterkeys())
keys.sort()
self.assertEqual(keys, ['a', 'b', 'name', 'x'])
values = list(m.itervalues())
values.sort()
self.assertEqual(values, [1, 2, 3, 'bob'])
items = list(m.iteritems())
items.sort()
self.assertEqual(items,
[('a', 2), ('b', 3), ('name', 'bob'), ('x', 1)])
# PersistentMapping didn't have an __iter__ method before ZODB 3.4.2.
# Check that it plays well now with the Python iteration protocol.
def checkIteration(self):
from persistent.mapping import PersistentMapping
m = PersistentMapping({'x': 1}, a=2, b=3)
m['name'] = 'bob'
def check(keylist):
keylist.sort()
self.assertEqual(keylist, ['a', 'b', 'name', 'x'])
check(list(m))
check([key for key in m])
i = iter(m)
keylist = []
while 1:
try:
key = i.next()
except StopIteration:
break
keylist.append(key)
check(keylist)
def find_global(modulename, classname):
"""Helper for this test suite to get special PersistentMapping"""
if classname == "PersistentMapping":
class PersistentMapping(object):
def __setstate__(self, state):
self.__dict__.update(state)
return PersistentMapping
else:
__import__(modulename)
mod = sys.modules[modulename]
return getattr(mod, classname)
def test_suite():
return unittest.makeSuite(PMTests, 'check')
if __name__ == "__main__":
unittest.main()
|