Initial tests for SQLCustomType
This commit is contained in:
@@ -1321,7 +1321,7 @@ class BaseAdapter(ConnectionPool):
|
|||||||
obj = obj()
|
obj = obj()
|
||||||
if isinstance(fieldtype, SQLCustomType):
|
if isinstance(fieldtype, SQLCustomType):
|
||||||
value = fieldtype.encoder(obj)
|
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 self.adapt(value)
|
||||||
return value or 'NULL'
|
return value or 'NULL'
|
||||||
if isinstance(obj, (Expression, Field)):
|
if isinstance(obj, (Expression, Field)):
|
||||||
|
|||||||
@@ -1573,6 +1573,36 @@ class TestGis(unittest.TestCase):
|
|||||||
t2.drop()
|
t2.drop()
|
||||||
return
|
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__':
|
if __name__ == '__main__':
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|||||||
Reference in New Issue
Block a user