def test_get_close_db(app): # test if db is the same in one app_context with app.app_context(): db = get_db() assert db is get_db() # test if db is closed, because db should close after # each request. request ends in with statement above with pytest.raises(sqlite3.ProgrammingError) as e: db.execute('SELECT 1') # error message contains 'closed' word assert 'closed' in str(e.value)
def restore(): if 'last_task_pop' not in session: flash('No') return to_index() db = get_db() # contains info about last popped before pop last = session['last_task_pop'] # just for short # contains info about last popped after pop task = db.execute(SQL.get_task_by_id, (last['id'], )).fetchone() # if last pop was deleting (done), means task is None if not task: # insert last popped task db.execute(SQL.add_task, (last['author_id'], last['content'], last['tstamp'])) # elif task has no tstamp, means tstamp was deleted # so task['tstamp'] is None elif not task['tstamp']: db.execute(SQL.update_timestamp_by_id, (last['tstamp'], last['id'])) # for some extraordinary cases huh else: flash('Nothing to undo') db.commit() return to_index()
def rmts(id_): save_last_pop(id_) db = get_db() # set tstamp to NULL db.execute(SQL.update_timestamp_by_id, (None, id_)) db.commit() flash('Removed! (undo)') return to_index()
def done(id_): save_last_pop(id_) db = get_db() db.execute(SQL.delete_task_by_id, (id_, )) db.commit() flash('Done! Good job! (undo)') # 'undo' is unique message, see wrapper.html return to_index()
def edit(id_): task = request.form.get('task', '') if not task: # if task user selected the task and deleted its # content, this probably means He want to delete it return done(id_) db = get_db() tstamp = recognize(task) db.execute(SQL.update_content_by_id, (task, id_)) db.execute(SQL.update_timestamp_by_id, (tstamp, id_)) db.commit() return to_index()
def test_signup(client, app): # basic assert client.get('/auth/signup').status_code == 200 # make correct signup request resp = client.post('/auth/signup', data={ 'username': '******', 'password': '******', 'password2': '6letter' }) # test redirect assert resp.headers.get('Location') == 'http://localhost/auth/signin' # test if such user now exists with app.app_context(): assert get_db().execute(sqls.get_user_by_username, ('somelogin', )).fetchone() is not None
def add(): # form has no validators, so no conditions here form = AddTaskForm(request.form) task = form.task.data if not task: flash('No task typed :( Are you lazy?') return to_index() tstamp = recognize(task) db = get_db() db.execute(SQL.add_task, (g.user['id'], task, tstamp)) db.commit() # when the task is added, I want user to stay at # that page so he can add more tasks quickly. # So I flash message and return needed template flash('Added!') return to_index()
def app(): db_fd, db_path = tempfile.mkstemp() app = create_app({ 'TESTING': True, 'DATABASE': db_path, 'SECRET_KEY': 'testing' }) # test_config is passed with app.app_context(): init_db() db = get_db() db.execute(sqls.add_users) db.execute(sqls.add_tasks) db.commit() yield app os.close(db_fd) os.unlink(db_path)
def save_last_pop(id_): session['last_task_pop'] = SQL.row_to_dict(get_db().execute( SQL.get_task_by_id, (id_, )).fetchone())
def get_tasks(): """ Returns list of current user's tasks """ return get_db().execute(SQL.get_user_tasks, (g.user['id'], )).fetchall()