From 6228de8e1022f265358c08218ce97e49eec01958 Mon Sep 17 00:00:00 2001 From: ilvalle Date: Sun, 30 Nov 2014 19:53:33 +0100 Subject: [PATCH 1/3] Fix NULL values with SQLCustomType --- gluon/dal/adapters/base.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gluon/dal/adapters/base.py b/gluon/dal/adapters/base.py index f672b0f7..29aacd52 100644 --- a/gluon/dal/adapters/base.py +++ b/gluon/dal/adapters/base.py @@ -1323,7 +1323,7 @@ class BaseAdapter(ConnectionPool): value = fieldtype.encoder(obj) if fieldtype.type in ('string','text', 'json'): return self.adapt(value) - return value + return value or 'NULL' if isinstance(obj, (Expression, Field)): return str(obj) if field_is_type('list:'): From f64098af14e60d42937e58d2d7e242816b18095e Mon Sep 17 00:00:00 2001 From: ilvalle Date: Wed, 3 Dec 2014 18:20:07 +0100 Subject: [PATCH 2/3] 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() From 2a8c04c69fd10b4083ab84dcf31df87e518d938a Mon Sep 17 00:00:00 2001 From: ilvalle Date: Wed, 3 Dec 2014 19:32:25 +0100 Subject: [PATCH 3/3] better SQLCustomType tests --- gluon/tests/test_dal.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/gluon/tests/test_dal.py b/gluon/tests/test_dal.py index 9fd33ec3..464cb3dc 100644 --- a/gluon/tests/test_dal.py +++ b/gluon/tests/test_dal.py @@ -1578,8 +1578,13 @@ 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") + native_double = "double" + native_string = "string" + if hasattr(db._adapter, 'types'): + native_double = db._adapter.types['double'] + native_string = db._adapter.types['string'] % {'length': 256} + basic_t = SQLCustomType(type = "double", native = native_double) + basic_t_str = SQLCustomType(type = "string", native = 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()