fixed problem with computed fields

This commit is contained in:
mdipierro
2012-08-29 16:05:41 -05:00
parent ac100b7489
commit 36e8c95ca5
3 changed files with 23 additions and 5 deletions
+1 -1
View File
@@ -1 +1 @@
Version 2.00.1 (2012-08-29 15:42:48) rc4
Version 2.00.1 (2012-08-29 16:05:38) rc4
+8 -4
View File
@@ -7673,21 +7673,25 @@ class Table(object):
to_compute.append((name,ofield))
# if field is required, check its default value
elif not update and not ofield.default is None:
new_fields[name] = (ofield,ofield.default)
value = ofield.default
fields[name] = value
new_fields[name] = (ofield,value)
# if this is an update, user the update field instead
elif update and not ofield.update is None:
new_fields[name] = (ofield,ofield.update)
value = ofield.update
fields[name] = value
new_fields[name] = (ofield,value)
# if the field is still not there but it should, error
elif not update and ofield.required:
raise RuntimeError, \
'Table: missing required field: %s' % name
# now deal with fields that are supposed to be computed
if to_compute:
dummyrow = Row(new_fields)
row = Row(fields)
for name,ofield in to_compute:
# try compute it
try:
new_fields[name] = (ofield,ofield.compute(dummyrow))
new_fields[name] = (ofield,ofield.compute(row))
except (KeyError, AttributeError):
# error sinlently unless field is required!
if ofield.required:
+14
View File
@@ -515,6 +515,20 @@ class TestVirtualFields(unittest.TestCase):
db.t.drop()
db.commit()
class TestComputedFields(unittest.TestCase):
def testRun(self):
db = DAL('sqlite:memory:')
db.define_table('t',
Field('a'),
Field('b',default='x'),
Field('c',compute=lambda r: r.a+r.b))
db.commit()
id = db.t.insert(a="z")
self.assertEqual(db.t[id].c,'zx')
db.t.drop()
db.commit()
class TestImportExportFields(unittest.TestCase):
def testRun(self):