def dbval2val(converter, dbval, obj=None): if isinstance(dbval, (int, bool, float, type(None))): return dbval val = json.loads(dbval) if obj is None: return val return TrackedValue.make(obj, converter.attr, val)
def validate(converter, val, obj=None): if obj is None or converter.attr is None: return val if isinstance(val, TrackedValue ) and val.obj is obj and val.attr is converter.attr: return val return TrackedValue.make(obj, converter.attr, val)
def test_make(self): obj = Object() attr = Attr() value = {'items': ['one', 'two', 'three']} tracked_value = TrackedValue.make(obj, attr, value) self.assertEqual(type(tracked_value), TrackedDict) self.assertEqual(type(tracked_value['items']), TrackedList)
def test_list_append(self): obj = Object() attr = Attr() value = {'items': ['one', 'two', 'three']} tracked_value = TrackedValue.make(obj, attr, value) log = [] obj.on_attr_changed = lambda x: log.append(x) tracked_value['items'].append('four') self.assertEqual(log, [attr])
def test_list_setslice(self): obj = Object() attr = Attr() value = {'items': ['one', 'two', 'three']} tracked_value = TrackedValue.make(obj, attr, value) log = [] obj.on_attr_changed = lambda x: log.append(x) tracked_value['items'][1:2] = ['a', 'b', 'c'] self.assertEqual(log, [attr]) self.assertEqual(tracked_value['items'], ['one', 'a', 'b', 'c', 'three'])