From f64098af14e60d42937e58d2d7e242816b18095e Mon Sep 17 00:00:00 2001 From: ilvalle Date: Wed, 3 Dec 2014 18:20:07 +0100 Subject: [PATCH] Initial tests for SQLCustomType --- gluon/dal/adapters/base.py | 2 +- gluon/tests/test_dal.py | 30 ++++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/gluon/dal/adapters/base.py b/gluon/dal/adapters/base.py index 29aacd52..91aeba17 100644 --- a/gluon/dal/adapters/base.py +++ b/gluon/dal/adapters/base.py @@ -1321,7 +1321,7 @@ class BaseAdapter(ConnectionPool): obj = obj() if isinstance(fieldtype, SQLCustomType): value = fieldtype.encoder(obj) - if fieldtype.type in ('string','text', 'json'): + if value and fieldtype.type in ('string','text', 'json'): return self.adapt(value) return value or 'NULL' if isinstance(obj, (Expression, Field)): diff --git a/gluon/tests/test_dal.py b/gluon/tests/test_dal.py index 451d23bc..9fd33ec3 100644 --- a/gluon/tests/test_dal.py +++ b/gluon/tests/test_dal.py @@ -1573,6 +1573,36 @@ class TestGis(unittest.TestCase): t2.drop() return +class TestSQLCustomType(unittest.TestCase): + + def testRun(self): + db = DAL(DEFAULT_URI, check_reserved=['all']) + from dal.helpers.classes import SQLCustomType + basic_t = SQLCustomType(type = "double", native = "double") + basic_t_str = SQLCustomType(type = "string", native = "string") + t0=db.define_table('t0', Field("price", basic_t), Field("product", basic_t_str)) + r_id = t0.insert(price=None, product=None) + row = db(t0.id == r_id).select(t0.ALL).first() + self.assertEqual(row['price'], None) + self.assertEqual(row['product'], None) + r_id = t0.insert(price=1.2, product="car") + row=db(t0.id == r_id).select(t0.ALL).first() + self.assertEqual(row['price'], 1.2) + self.assertEqual(row['product'], 'car') + t0.drop() + import zlib + compressed = SQLCustomType( + type ='text', + native='text', + encoder =(lambda x: zlib.compress(x or '', 1)), + decoder = (lambda x: zlib.decompress(x)) + ) + t1=db.define_table('t0',Field('cdata', compressed)) + #r_id=t1.insert(cdata="car") + #row=db(t1.id == r_id).select(t1.ALL).first() + #self.assertEqual(row['cdata'], "'car'") + t1.drop() + return if __name__ == '__main__': unittest.main()