Esempio n. 1
0
def test_mark_decorator(app, client):

    @app.route('/')
    @mark('test', 1)
    def index():
        return ''

    @app.route('/hourly')
    @mark('test_hourly', 2, track_hourly=True)
    def index_hourly():
        return ''

    client.get('/', follow_redirects=True)

    # month events
    assert 1 in MonthEvents('test', now.year, now.month)
    assert 2 not in MonthEvents('test', now.year, now.month)

    # week events
    assert 1 in WeekEvents('test', now.year, now.isocalendar()[1])
    assert 2 not in WeekEvents('test', now.year, now.isocalendar()[1])

    # day events
    assert 1 in DayEvents('test', now.year, now.month, now.day)
    assert 2 not in DayEvents('test', now.year, now.month, now.day)

    client.get('/hourly', follow_redirects=True)

    # hour events
    assert 1 not in HourEvents('test', now.year, now.month, now.day, now.hour)
    assert 2 in HourEvents('test_hourly', now.year, now.month, now.day, now.hour)
    assert 1 not in HourEvents('test_hourly', now.year, now.month, now.day, now.hour)
    assert 1 not in HourEvents('test_hourly', now.year, now.month, now.day, now.hour - 1)
Esempio n. 2
0
def test_authenticated_redis(app, auth_bitmap):
    assert auth_bitmap.redis_auth == 'foobared'

    # Test to make sure auth_bitmap setup with different set of params
    assert auth_bitmap.redis_url != app.config['BITMAPIST_REDIS_URL']

    mark_event('active', 42)
    assert 42 in MonthEvents('active', now.year, now.month)

    unmark_event('active', 42)
    assert 42 not in MonthEvents('active', now.year, now.month)
Esempio n. 3
0
def test_sqlalchemy_after_insert(sqlalchemy):
    db, User = sqlalchemy

    with db.app.test_request_context():
        # set up and save user
        user = User(name='Test User')
        db.session.add(user)
        db.session.commit()

        # test that user was saved
        assert user.id is not None

        # test that user id was marked with 'user:created' event
        assert user.id in MonthEvents('user:created', now.year, now.month)
Esempio n. 4
0
def test_sqlalchemy_before_update(sqlalchemy):
    db, User = sqlalchemy

    with db.app.test_request_context():
        # set up and save user
        user = User(name='Test User')
        db.session.add(user)
        db.session.commit()

        # update user, and test that user is updated
        user.name = 'New Name'
        assert db.session.is_modified(user)

        db.session.add(user)
        db.session.commit()
        assert not db.session.is_modified(user)

        # test that user id was marked with 'user:updated' event
        assert user.id in MonthEvents('user:updated', now.year, now.month)
Esempio n. 5
0
def test_sqlalchemy_before_delete(sqlalchemy):
    db, User = sqlalchemy

    with db.app.test_request_context():
        # set up and save user
        user = User(name='Test User')
        db.session.add(user)
        db.session.commit()

        # grab user id before we delete
        user_id = user.id

        # delete user, and test that user is deleted
        db.session.delete(user)
        db.session.commit()
        user_in_db = db.session.query(User).filter(User.id == user_id).first()
        assert not user_in_db

        # test that user id was marked with 'user:deleted' event
        assert user_id in MonthEvents('user:deleted', now.year, now.month)
Esempio n. 6
0
def test_flask_login_user_logout(app):
    login_manager = LoginManager()
    login_manager.init_app(app)

    user_id = datetime.now().microsecond

    with app.test_request_context():
        # set up, log in, and log out user
        user = User()
        user.id = user_id
        login_user(user)
        logout_user()

        # test that user was logged out
        assert not current_user.is_active
        assert not current_user.is_authenticated
        assert not current_user == user

        # test that user id was marked with 'user:logged_out' event
        assert user_id in MonthEvents('user:logged_out', now.year, now.month)
Esempio n. 7
0
def test_flask_login_user_login(app):
    # LoginManager could be set up in app fixture in conftest.py instead
    login_manager = LoginManager()
    login_manager.init_app(app)

    # TODO: once event is marked, user id exists in MonthEvents and test will
    #       continue to pass, regardless of continued success; set to current
    #       microsecond to temporarily circumvent, but there should be a better
    #       way to fix user_id assignment (or tear down redis or something)
    user_id = datetime.now().microsecond

    with app.test_request_context():
        # set up and log in user
        user = User()
        user.id = user_id
        login_user(user)

        # test that user was logged in
        assert current_user.is_active
        assert current_user.is_authenticated
        assert current_user == user

        # test that user id was marked with 'user:logged_in' event
        assert user_id in MonthEvents('user:logged_in', now.year, now.month)
Esempio n. 8
0
def test_unmark_function(app, client):
    mark_event('active', 126)
    assert 126 in MonthEvents('active', now.year, now.month)

    unmark_event('active', 126)
    assert 126 not in MonthEvents('active', now.year, now.month)
Esempio n. 9
0
def test_mark_function(app, client):
    mark_event('active', 125)
    assert 125 in MonthEvents('active', now.year, now.month)