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.value)
def update(blog_id): """Update a blog if the current user is the author.""" if request.method == "GET": blog = get_blog(blog_id) form = CreateBlogForm(obj=blog, meta={'csrf_context': session}) else: form = CreateBlogForm(request.form, meta={'csrf_context': session}) if request.method == "POST": title = form.title.data body = form.body.data public = form.public.data blog = get_blog(blog_id) if g.user['id'] != blog['author_id']: abort(403) db = get_db() db.execute( "UPDATE blog SET title = ?, body = ?, public = ?, updated = CURRENT_TIMESTAMP WHERE id = ?", (title, body, public, blog_id)) db.commit() return redirect(url_for("blog.index")) elif request.method == "POST" and not form.validate(): for field_name, field_errors in form.errors.items(): for error in field_errors: flash(error, field_name.capitalize()) return render_template("blog/update.html", form=form, blog=blog)
def create(): """Create a new blog for the current user.""" form = CreateBlogForm(request.form, meta={'csrf_context': session}) if request.method == "POST" and form.validate(): title = form.title.data body = form.body.data public = form.public.data error = None if error is not None: flash(error) else: db = get_db() db.execute( "INSERT INTO blog (title, body, public, author_id) VALUES (?, ?, ?, ?)", (title, body, public, g.user["id"]), ) db.commit() return redirect(url_for("blog.index")) elif request.method == "POST" and not form.validate(): for field_name, field_errors in form.errors.items(): for error in field_errors: flash(error, field_name.capitalize()) return render_template("blog/create.html", form=form)
def app(): """Create and configure a new app instance for each test.""" # create a temporary file to isolate the database for each test db_fd, db_path = tempfile.mkstemp() # create the app with common test config app = create_app({"TESTING": True, "DATABASE": db_path}) # create the database and load test data with app.app_context(): init_db() get_db().executescript(_data_sql) yield app # close and remove the temporary database os.close(db_fd) os.unlink(db_path)
def get_comments(blog_id): """ Get comments from a blog. """ comments = (get_db().execute( " SELECT DISTINCT c.id, c.text, c.created, c.author_id" " FROM blog b JOIN comment c ON c.blog_id = b.id" " WHERE b.id = ?", (blog_id, ), ).fetchall()) return comments
def comment(blog_id): form = CreateCommentForm(request.form, meta={'csrf_context': session}) if request.method == "POST": text = form.text.data db = get_db() db.execute( "INSERT INTO comment (text, author_id, blog_id) VALUES (?, ?, ?)", (text, g.user["id"], blog_id), ) db.commit() return redirect(url_for("blog.detail", blog_id=blog_id)) elif request.method == "POST" and not form.validate(): for field_name, field_errors in form.errors.items(): for error in field_errors: flash(error, field_name.capitalize())