From 4c8be8510b753a3caedacb689162b15a24605a29 Mon Sep 17 00:00:00 2001 From: Jeremy Hylton Date: Wed, 13 Nov 2002 22:10:47 +0000 Subject: [PATCH] Add some simple tests of the persistence hooks. --- Lib/test/pickletester.py | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/Lib/test/pickletester.py b/Lib/test/pickletester.py index 2294c3dd0f8..7c031ec1326 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)