def test_cache_doesnt_autoclear_dirty_and_deleted_(app, client, monkeypatch): """The cache does not autoclear dirty models upon commit if they do not have cache keys.""" user = User.from_pk(1) monkeypatch.setattr('core.users.models.User.__cache_key__', None) user.set_password('testing') assert cache.has('users_1') db.session.commit() assert cache.has('users_1')
def test_cache_autoclear_dirty_and_deleted(app, client): """The cache autoclears dirty models upon commit.""" user = User.from_pk(1) user_2 = User.from_pk(3) user.set_password('testing') db.session.delete(user_2) assert cache.has('users_1') assert cache.has('users_3') db.session.commit() assert not cache.has('users_1') assert not cache.has('users_3') assert not User.from_pk(3)
def test_make_message(client): pm = PrivateConversation.from_pk(1) pm.set_state(1) pm_state = PrivateConversationState.from_attrs(conv_id=1, user_id=1) assert (datetime.utcnow().replace(tzinfo=pytz.utc) - pm.last_response_time).total_seconds() > 60 * 60 assert cache.has(pm_state.cache_key) PrivateMessage.new(conv_id=1, user_id=2, contents='hi') assert not cache.has(pm_state.cache_key) pm.set_state(1) assert (datetime.utcnow().replace(tzinfo=pytz.utc) - pm.last_response_time).total_seconds() < 15
def test_clear_cache_keys_without_type(client): ckey = Notification.__cache_key_notification_count__.format( user_id=1, type=1 ) cache.set(ckey, 100) Notification.clear_cache_keys(1) assert not cache.has(ckey)
def test_clear_cache_keys(client): ckey = Notification.__cache_key_notification_count__.format( user_id=1, type=1 ) cache.set(ckey, 100) Notification.clear_cache_keys(1, 'subscripple') assert not cache.has(ckey)
def test_clear_cache_keys(client): for uid in [1, 2]: for f in ['inbox', 'sentbox', 'deleted']: cache.set( PrivateConversation.__cache_key_of_user__.format(user_id=uid, filter=f), 1, ) PrivateConversation.clear_cache_keys(1) for f in ['inbox', 'sentbox', 'deleted']: assert not cache.has( PrivateConversation.__cache_key_of_user__.format(user_id=1, filter=f)) assert cache.has( PrivateConversation.__cache_key_of_user__.format(user_id=2, filter=f))
def test_user_permissions_property_cached(app, client, monkeypatch): """Permissions property should properly handle differences in userclasses and custom perms.""" add_permissions(app, 'one', 'three', 'four') user = User.from_pk(1) assert set(user.permissions) == {'four', 'one', 'three'} assert cache.has(user.__cache_key_permissions__.format(id=user.id)) del user with mock.patch('core.users.models.User.user_class_model', None): user = User.from_pk(1) assert set(user.permissions) == {'four', 'one', 'three'}
def test_route(): cache.set('key3', 1) cache.inc('key1') cache.get('key2') cache.ttl('key2') cache.ttl('key3') cache.get('key3') cache.delete('key3') cache.delete('key4') assert cache.has('key1') assert flask.g.cache_keys['inc'] == {'key1'} assert flask.g.cache_keys['get'] == {'key3'} assert flask.g.cache_keys['set'] == {'key3'} assert flask.g.cache_keys['delete'] == {'key3'} assert flask.g.cache_keys['ttl'] == {'key3'} assert flask.g.cache_keys['has'] == {'key1'} return flask.jsonify('complete')
def test_cache_has_key(app, client): """The `has_key` function should return True/False based on key existence.""" cache.set('test_1', 1) assert cache.has('test_1') assert not cache.has('test_2')