summaryrefslogtreecommitdiffstats
path: root/Lib/test/pickletester.py
diff options
context:
space:
mode:
authorJeremy Hylton <jeremy@alum.mit.edu>2002-11-13 22:10:47 (GMT)
committerJeremy Hylton <jeremy@alum.mit.edu>2002-11-13 22:10:47 (GMT)
commit4c8be8510b753a3caedacb689162b15a24605a29 (patch)
tree1e6fcdab67a727994b4d18a20b2688b488cf3b79 /Lib/test/pickletester.py
parent5e0f4e73a963a69b0ecd16e2477d6cd2cacef6c9 (diff)
downloadcpython-4c8be8510b753a3caedacb689162b15a24605a29.zip
cpython-4c8be8510b753a3caedacb689162b15a24605a29.tar.gz
cpython-4c8be8510b753a3caedacb689162b15a24605a29.tar.bz2
Add some simple tests of the persistence hooks.
Diffstat (limited to 'Lib/test/pickletester.py')
-rw-r--r--Lib/test/pickletester.py35
1 files changed, 35 insertions, 0 deletions
diff --git a/Lib/test/pickletester.py b/Lib/test/pickletester.py
index 2294c3d..7c031ec 100644
--- a/Lib/test/pickletester.py
+++ b/Lib/test/pickletester.py
@@ -286,3 +286,38 @@ class AbstractPickleModuleTests(unittest.TestCase):
self.assertRaises(ValueError, self.module.dump, 123, f)
finally:
os.remove(TESTFN)
+
+class AbstractPersistentPicklerTests(unittest.TestCase):
+
+ # This class defines persistent_id() and persistent_load()
+ # functions that should be used by the pickler. All even integers
+ # are pickled using persistent ids.
+
+ def persistent_id(self, object):
+ if isinstance(object, int) and object % 2 == 0:
+ self.id_count += 1
+ return str(object)
+ else:
+ return None
+
+ def persistent_load(self, oid):
+ self.load_count += 1
+ object = int(oid)
+ assert object % 2 == 0
+ return object
+
+ def test_persistence(self):
+ self.id_count = 0
+ self.load_count = 0
+ L = range(10)
+ self.assertEqual(self.loads(self.dumps(L)), L)
+ self.assertEqual(self.id_count, 5)
+ self.assertEqual(self.load_count, 5)
+
+ def test_bin_persistence(self):
+ self.id_count = 0
+ self.load_count = 0
+ L = range(10)
+ self.assertEqual(self.loads(self.dumps(L, 1)), L)
+ self.assertEqual(self.id_count, 5)
+ self.assertEqual(self.load_count, 5)