Commit 36b80af4 authored by Barry Warsaw's avatar Barry Warsaw

Updates to do some simple pack() testing first.

dumps(), makeloader(): Take these out of the PackableStorage class,
and keep them even though they aren't currently used.

checkOldRevisionPacked(): Removed -- it's too complicated as a first
test.

checkSimplePack(): New pack() checker; it creates three revisions of
the same object, then attempts to pack to `now'.  Then it makes sure
that revisions 1 and 2 of the object are gone, but that revision 3
(the current revision) still exists.  Full passes this test, but
FileStorage fails. :(
parent 1463d652
...@@ -27,6 +27,25 @@ class Object: ...@@ -27,6 +27,25 @@ class Object:
return self._oid return self._oid
def dumps(obj):
def getpersid(obj):
return obj.getoid()
s = StringIO()
p = pickle.Pickler(s)
p.persistent_id = getpersid
p.dump(obj)
return s.getvalue()
def makeloader(persfunc):
def loads(str, persfunc=persfunc):
fp = StringIO(str)
u = pickle.Unpickler(fp)
u.persistent_load = persfunc
return u.load()
return loads
class PackableStorage: class PackableStorage:
def _newobj(self): def _newobj(self):
...@@ -36,54 +55,40 @@ class PackableStorage: ...@@ -36,54 +55,40 @@ class PackableStorage:
self._cache[obj.getoid()] = obj self._cache[obj.getoid()] = obj
return obj return obj
def checkOldRevisionPacked(self): def checkSimplePack(self):
# The initial revision has an object graph like so: # Create the object
# o1 -> o2 -> o3 obj = self._newobj()
o1 = self._newobj() oid = obj.getoid()
o2 = self._newobj() obj.value = 1
o3 = self._newobj() # Commit three different revisions
o1.object = o2 revid1 = self._dostoreNP(oid, data=pickle.dumps(obj))
o2.object = o3 obj.value = 2
# Pickle these objects revid2 = self._dostoreNP(oid, revid=revid1, data=pickle.dumps(obj))
def dumps(obj): obj.value = 3
def getpersid(obj): revid3 = self._dostoreNP(oid, revid=revid2, data=pickle.dumps(obj))
return obj.getoid() # Now make sure all three revisions can be extracted
s = StringIO() #loads = makeloader(self._cache.get)
p = pickle.Pickler(s) data = self._storage.loadSerial(oid, revid1)
p.persistent_id = getpersid pobj = pickle.loads(data)
p.dump(obj) assert pobj.getoid() == oid and pobj.value == 1
return s.getvalue() data = self._storage.loadSerial(oid, revid2)
p1, p2, p3 = map(dumps, (o1, o2, o3)) pobj = pickle.loads(data)
# Now commit these objects assert pobj.getoid() == oid and pobj.value == 2
revid1 = self._dostoreNP(oid=o1.getoid(), data=p1) data = self._storage.loadSerial(oid, revid3)
revid2 = self._dostoreNP(oid=o2.getoid(), data=p2) pobj = pickle.loads(data)
revid3 = self._dostoreNP(oid=o3.getoid(), data=p3) assert pobj.getoid() == oid and pobj.value == 3
# Record this moment in history so we can pack everything before it # Now pack away all but the most current revision
t0 = time.time() self._storage.pack(time.time(), referencesf)
# Now change the object graph to look like so: # Make sure the first two revisions are gone but the third (current)
# o1 -> o3 # still exists.
# and note that o2 is no longer referenced self.assertRaises(KeyError,
o1.object = o3 self._storage.loadSerial, oid, revid1)
p11 = dumps(o1)
revid11 = self._dostoreNP(oid=o1.getoid(), revid=revid1, data=p11)
# Pack away transaction 2
self._storage.pack(t0, referencesf)
# Now, objects 1 and 3 should exist, but object 2 should have been
# reference counted away. First, we need a custom unpickler.
def loads(str, persfunc=self._cache.get):
fp = StringIO(str)
u = pickle.Unpickler(fp)
u.persistent_load = persfunc
return u.load()
# Get object 1
data, revid = self._storage.load(o1.getoid(), '')
assert revid == revid11
from ZODB import utils
assert loads(data).getoid() == o1.getoid()
# Get object 3
data, revid = self._storage.load(o3.getoid(), '')
assert revid == revid2
assert loads(data).getoid() == o3.getoid()
# Object 2 should fail
self.assertRaises(KeyError, self.assertRaises(KeyError,
self._storage.load, o2.getoid(), '') self._storage.loadSerial, oid, revid2)
data = self._storage.loadSerial(oid, revid3)
pobj = pickle.loads(data)
assert pobj.getoid() == oid and pobj.value == 3
data, revid = self._storage.load(oid, '')
assert revid == revid3
pobj = pickle.loads(data)
assert pobj.getoid() == oid and pobj.value == 3
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment