def redirect_short(short_hash): db = get_db() try: url = db.execute( "SELECT original_url FROM UrlMap WHERE short_hash = ?", (short_hash,) ).fetchone()["original_url"] except TypeError: return redirect(url_for(".index")) return redirect(url)
def app_conflict_next_generated(app): """ :param app: app fixture :return: an app fixture for which the next auto-generated short url will conflict with a custom url. """ with app.app_context(): db = get_db() next_id = ( 1 + db.execute("SELECT coalesce(max(id), 0) FROM HashIdGen").fetchone()[0] ) next_hash = hash_from_id(next_id) db.execute( "INSERT INTO UrlMap (original_url, short_hash) VALUES (?,?)", ("http://something.io", next_hash), ) db.commit() return app, next_id
def app(): db_fd, db_path = tempfile.mkstemp() app = create_app( test_config={ "TESTING": True, "DATABASE": db_path, "SECRET_KEY": "TEST", "SERVER_NAME": "mydomain.com", "PREFERRED_URL_SCHEME": "https", }) with app.app_context(): db = get_db() with app.open_resource("schema.sql") as schema: db.executescript(schema.read().decode("UTF-8")) with open(os.path.join(os.path.dirname(__file__), "test_data.sql"), "rb") as f: db.executescript(f.read().decode("UTF-8")) yield app os.close(db_fd) os.unlink(db_path)
def test_close_db(app): with app.app_context(): db = get_db() close_db() with pytest.raises(sqlite3.ProgrammingError): db.execute("SELECT 1")
def test_get_db(app): with app.app_context(): db = get_db() # get_db() returns the same object within an app context assert db is get_db()