def setUp(self): self.counter = UserDefTally(db_name='counter') self.counter.base = encode([ KW('defn'), KW('transform'), [KW('list'), KW('instance')], [ KW('if'), [ KW('and'), [KW('not_null?'), KW('instance')], [ KW('='), [KW('instance'), [KW('quote'), KW('__class__')]], 'Foo' ], ], [KW('instance'), [KW('quote'), KW('value')]], 0, ], ]) self.counter.get_tally = encode(0) self.counter.get_value = encode(KW('instance')) self.counter.filter_value = encode( [KW('>='), [KW('transform'), KW('value')], 3]) self.counter.handle_change = encode([ KW('->'), KW('tally'), [KW('-'), [KW('transform'), KW('old_value')]], [KW('+'), [KW('transform'), KW('new_value')]], ]) self.counter.save()
def setUp(self): self.aggregate_template = UserDefTemplate( params=AGGREGATE_PARAMS, template=encode(AGGREGATE_TEMPLATE), ) self.aggregate_template.save() self.sum_template = UserDefTemplate( params=SUM_PARAMS, template=encode(SUM_TEMPLATE), parent=self.aggregate_template, ) self.sum_template.save()
def setUp(self): self.counter = UserDefGroupTally(db_name='counter') self.counter.base = encode([ KW('defn'), KW('transform'), [KW('list'), KW('value')], [ KW('if'), [ KW('and'), [KW('not_null?'), KW('value')], [ KW('='), [KW('value'), [KW('quote'), KW('__class__')]], 'Foo', ], ], 1, 0, ] ]) self.counter.get_tally = encode(0) self.counter.get_group = encode([ KW('if'), [ KW('and'), [KW('not_null?'), KW('value')], [ KW('='), [KW('value'), [KW('quote'), KW('__class__')]], 'Foo', ], ], [KW('str'), [KW('value'), [KW('quote'), KW('value')]]], None, ]) self.counter.handle_change = encode([ KW('->'), KW('tally'), [KW('-'), [KW('transform'), KW('old_value')]], [KW('+'), [KW('transform'), KW('new_value')]], ]) self.counter.save()
def test_listen(self): sub = listen(Foo) # Initial value self.assertStored('counter', 0) # Create model foo = Foo(value=5) foo.save() self.assertStored('counter', 5) # Make values count twice self.counter.base = encode([ KW('defn'), KW('transform'), [KW('list'), KW('instance')], [ KW('if'), [ KW('and'), [KW('not_null?'), KW('instance')], [ KW('='), [KW('instance'), [KW('quote'), KW('__class__')]], 'Foo' ], ], [KW('*'), [KW('instance'), [KW('quote'), KW('value')]], 2], 0, ], ]) self.counter.save() # Update foo foo.value = 6 foo.save() # Note: tally does not know how it used to handle the value so we only # get the difference in value *2 added to the tally. self.assertStored('counter', 7) # Delete foo foo.delete() self.assertStored('counter', -5) sub.close()
def test_encode(self): self.assertEqual(encode(sample), encoded_sample)