[SQL-CVS] r543 - in trunk/SQLObject: sqlobject tests
SQLObject is a Python ORM.
Brought to you by:
ianbicking,
phd
From: <sub...@co...> - 2005-01-14 14:24:22
|
Author: phd Date: 2005-01-14 14:24:18 +0000 (Fri, 14 Jan 2005) New Revision: 543 Modified: trunk/SQLObject/sqlobject/col.py trunk/SQLObject/tests/test_sqlobject.py Log: Added PickleCol, PickleValidator and tests. Modified: trunk/SQLObject/sqlobject/col.py =================================================================== --- trunk/SQLObject/sqlobject/col.py 2005-01-14 13:53:41 UTC (rev 542) +++ trunk/SQLObject/sqlobject/col.py 2005-01-14 14:24:18 UTC (rev 543) @@ -19,6 +19,10 @@ """ import re, time +try: + import cPickle as pickle +except ImportError: + import pickle import sqlbuilder # Sadly the name "constraints" conflicts with many of the function # arguments in this module, so we rename it: @@ -885,6 +889,37 @@ baseClass = SOBLOBCol +class PickleValidator(BinaryValidator): + """ + Validator for pickle types. A pickle type is simply a binary type with + hidden pickling, so that we can simply store any kind of stuff in a + particular column. + + The support for this relies directly on the support for binary for your + database. + """ + + def toPython(self, value, state): + if value is None: + return None + if isinstance(value, str): + return pickle.loads(value) + return value + + def fromPython(self, value, state): + if value is None: + return None + pickled = pickle.dumps(value) + return BinaryValidator.fromPython(self, pickled, state) + + +class SOPickleCol(SOBLOBCol): + validatorClass = PickleValidator # can be overriden in descendants + +class PickleCol(BLOBCol): + baseClass = SOPickleCol + + def popKey(kw, name, default=None): if not kw.has_key(name): return default Modified: trunk/SQLObject/tests/test_sqlobject.py =================================================================== --- trunk/SQLObject/tests/test_sqlobject.py 2005-01-14 13:53:41 UTC (rev 542) +++ trunk/SQLObject/tests/test_sqlobject.py 2005-01-14 14:24:18 UTC (rev 543) @@ -1342,25 +1342,61 @@ ## BLOB columns ######################################## -class Profile(SQLObject): +class ImageData(SQLObject): image = BLOBCol(default='emptydata', length=65535) class BLOBColTest(SQLObjectTest): - classes = [Profile] + classes = [ImageData] def testBLOBCol(self): data = ''.join([chr(x) for x in range(256)]) - prof = Profile() + prof = ImageData() prof.image = data iid = prof.id connection().cache.clear() - prof2 = Profile.get(iid) - assert prof2.image == data + prof2 = ImageData.get(iid) + self.assertEqual(prof2.image, data) ######################################## +## Pickle columns +######################################## + +class PickleData: + pi = 3.14156 + def __init__(self): + self.question = 'The Ulimate Question of Life, the Universe and Everything' + self.answer = 42 + +class PickleContainer(SQLObject): + pickledata = PickleCol(default=None, length=65535) + +class PickleColTest(SQLObjectTest): + classes = [PickleContainer] + + def testPickleCol(self): + mypickledata = PickleData() + + ctnr = PickleContainer(pickledata=mypickledata) + iid = ctnr.id + + connection().cache.clear() + + ctnr2 = PickleContainer.get(iid) + s2 = ctnr2.pickledata + + self.failUnless(isinstance(s2, PickleData)) + self.failUnless(isinstance(s2.pi, float)) + self.failUnless(isinstance(s2.question, str)) + self.failUnless(isinstance(s2.answer, int)) + self.assertEqual(s2.pi, mypickledata.pi) + self.assertEqual(s2.question, mypickledata.question) + self.assertEqual(s2.answer, mypickledata.answer) + + +######################################## ## Run from command-line: ######################################## |