def test_task_lifecycle(self): # Test task creation create = requests.post(self.url, data={ "title": "test", "desc": "this is a test" }) assert create.status_code == 200 # Check that the task was created with self.app.app_context(): assert get_db().execute("SELECT * from task where title = 'test'" ).fetchone() is not None #Check that the task is visible assert "this is a test" in create.text # Test task updating update = requests.post(self.url + "update", data={ "status": "done", "id": 1 }) assert update.status_code == 200 # Check that the task was updated correctly with self.app.app_context(): assert get_db().execute( "SELECT * from task where id = 1 and category = 'done'" ).fetchone() is not None # Test task deletion delete = requests.post(self.url + "delete", data={"id": "1"}) assert delete.status_code == 200 # Check that the task was deleted properly with self.app.app_context(): assert get_db().execute( "SELECT * from task where id = 1").fetchone() is None
def index(): # Returns the main page # fetches the tasks by grouping and creates the main page when the page is viewed if request.method == 'GET': db = get_db() todo_tasks = db.execute('SELECT *' ' FROM task p' ' WHERE category == "todo"' ' ORDER BY created ASC').fetchall() doing_tasks = db.execute('SELECT *' ' FROM task p' ' WHERE category == "doing"' ' ORDER BY created ASC').fetchall() done_tasks = db.execute('SELECT *' ' FROM task p' ' WHERE category == "done"' ' ORDER BY created ASC').fetchall() return render_template('index.html', todo_tasks=todo_tasks, doing_tasks=doing_tasks, done_tasks=done_tasks) # When a task is created, adds it to the database else: title = request.form['title'] description = request.form['desc'] db = get_db() db.execute( 'INSERT INTO task (category, title, description)' ' VALUES (?, ?, ?)', ("todo", title, description)) db.commit() return redirect(url_for('kanban.index'))
def test_get_close_db(self): with self.app.app_context(): db = get_db() assert db is get_db() with self.assertRaises(sqlite3.ProgrammingError): db.execute('SELECT 1')
def test_get_close_db(app): with app.app_context(): db = get_db() assert db is get_db() with pytest.raises(sqlite3.ProgrammingError) as e: db.execute('SELECT 1') assert 'closed' in str(e)
def app(): db_fd, db_path = tempfile.mkstemp() app = create_app({ 'TESTING': True, 'DATABASE': db_path, }) with app.app_context(): init_db() get_db().executescript(_data_sql) yield app os.close(db_fd) os.unlink(db_path)
def delete(): # Deletes a task id = request.form['id'] db = get_db() db.execute('DELETE FROM task' ' WHERE id == ?', (id)) db.commit() return redirect(url_for('kanban.index'))
def update(): ''' When users click 'doing' or 'done' on an existing task, this function updates the task category in the database and redirects to main(). ''' if request.method == 'POST': taskid = request.form['taskid'] category = request.form['category'] error = None if category == "doing": category = 2 elif category == "done": category = 3 else: error = 'Category misspecified.' if error is not None: flash(error) else: db = get_db() db.execute('UPDATE task SET category = ?' ' WHERE id = ?', (category, taskid)) db.commit() return redirect(url_for('kanban.main'))
def add(): ''' When users click the submit button, this function adds the new task to the database and redirects to the main() where the data will be fetched and displayed. ''' if request.method == 'POST': category = request.form['category'] body = request.form['body'] error = None if not category: error = 'Category is required.' if not body: error = 'Task description is required.' if error is not None: flash(error) else: user_id = str(g.user['id']) db = get_db() db.execute( 'INSERT INTO task (body, category, deleted, user_id)' ' VALUES (?, ?, ?, ?)', (body, category, False, user_id)) db.commit() return redirect(url_for('kanban.main'))
def main(): ''' Fetches all tasks from the database, and sends the information to the kanban/home.html template file. ''' db = get_db() user_id = str(g.user['id']) todos = db.execute( 'SELECT t.id, u.id, t.body, t.category, t.deleted' ' FROM task t JOIN user u ON t.user_id = u.id' ' WHERE u.id= (?) AND t.category=1 AND t.deleted=(?)' ' ORDER BY t.created ASC', (user_id, False)).fetchall() doings = db.execute( 'SELECT t.id, u.id, t.body, t.category, t.deleted' ' FROM task t JOIN user u ON t.user_id = u.id' ' WHERE u.id= (?) AND t.category=2 AND t.deleted=(?)' ' ORDER BY t.created ASC', (user_id, False)).fetchall() dones = db.execute( 'SELECT t.id, u.id, t.body, t.category, t.deleted' ' FROM task t JOIN user u ON t.user_id = u.id' ' WHERE u.id= (?) AND t.category=3 AND t.deleted=(?)' ' ORDER BY t.created ASC', (user_id, False)).fetchall() return render_template('kanban/home.html', todos=todos, doings=doings, dones=dones)
def update(): # Updates the status of a task category = request.form['status'] id = request.form['id'] db = get_db() db.execute('UPDATE task' ' SET category == ?' ' WHERE id == ?', (category, id)) db.commit() return redirect(url_for('kanban.index'))
def delete(): ''' When a user clicks the trash can button to delete the task, this function sets the task.deleted column to True. It redirects to main(), which filters tasks based on deleted=False. ''' taskid = request.form['taskid'] db = get_db() db.execute('UPDATE task SET deleted = ?' 'WHERE id = ?', (True, taskid)) db.commit() return redirect(url_for('kanban.main'))
def test_register(client, app): assert client.get('/auth/register').status_code == 200 response = client.post('/auth/register', data={ 'username': '******', 'password': '******' }) assert 'http://localhost/auth/login' == response.headers['Location'] with app.app_context(): assert get_db().execute( "select * from user where username = '******'", ).fetchone() is not None