class TestRedisHash(CanvasTestCase): def after_setUp(self): redis.delete('rh_key') self.rh = RedisHash('rh_key') def test_hincrby_ifsufficient_none(self): result = self.rh.hincrby_ifsufficient('balance', -5) self.assertEqual(result['success'], False) self.assertEqual(result['remaining'], 0) def test_hincrby_ifsufficient_enough(self): self.rh.hset('balance', 8) result = self.rh.hincrby_ifsufficient('balance', -5) self.assertEqual(result['success'], True) self.assertEqual(result['remaining'], 3) def test_hincrby_ifsufficient_justenough(self): self.rh.hset('balance', 5) result = self.rh.hincrby_ifsufficient('balance', -5) self.assertEqual(result['success'], True) self.assertEqual(result['remaining'], 0) def test_hincrby_ifsufficient_notquiteenough(self): self.rh.hset('balance', 4) result = self.rh.hincrby_ifsufficient('balance', -5) self.assertEqual(result['success'], False) self.assertEqual(result['remaining'], 4)
class _CompressedFiles(object): hashes = { 'css': RedisHash('compressed:css'), 'js': RedisHash('compressed:js'), } def set(self, type, name, path): self.hashes[type].hset(name, path) def get(self, type, name): return self.hashes[type].hget(name)
def get_actions(user): """ Returns a list of stickers. """ actions = actions_base[:] if user.is_authenticated(): if user.is_staff: actions += actions_staff if user.userinfo.details()['moderatable']: actions += actions_group_mod # Grab stickers that are lab options if labs_actions: kv = user.redis.user_kv.hgetall() for action in labs_actions: if RedisHash.read_bool(kv, "labs:"+action.name): actions.append(action) return actions
def get_actions(user): """ Returns a list of stickers. """ actions = actions_base[:] if user.is_authenticated(): if user.is_staff: actions += actions_staff if user.userinfo.details()['moderatable']: actions += actions_group_mod # Grab stickers that are lab options if labs_actions: kv = user.redis.user_kv.hgetall() for action in labs_actions: if RedisHash.read_bool(kv, "labs:" + action.name): actions.append(action) return actions
class UserExperimentsBackend(object): """ A wrapper around a Redis hash to store experiments for an existing user. """ logged_in = True def __init__(self, user_id, staff): self._user_id = user_id self.staff = staff self.key = user_id def get_experiments(self): return self.user_experiments.hgetall() def add(self, experiment, branch): self.user_experiments.hset(experiment.name, branch.name) branch.users.sadd(self._user_id) user_experiments = property(lambda self: RedisHash('user:%s:experiments' % self._user_id))
def after_setUp(self): redis.delete('rh_key') self.rh = RedisHash('rh_key')