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 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 play_game(game_id): """ Show the current game info and/or process a guess """ db = get_db() msg = None if request.method == 'POST': db.execute( "UPDATE game" " SET guesses = guesses + 1" " WHERE game.id = ?", (game_id, )) db.commit() guess = request.form.get('guess', type=int) game = db.execute("SELECT guesses, number FROM game WHERE id = ?", (game_id, )).fetchone() if guess > game['number']: msg = "Too high" elif guess < game['number']: msg = "Too low" else: db.execute("DELETE FROM game WHERE id = ?", (game_id, )) db.commit() return render_template('play/win.html', game=game) game = db.execute( "SELECT game.id, guesses, user_id, username" " FROM game JOIN user ON game.user_id = user.id" " WHERE game.id = ?", (game_id, )).fetchone() if msg is not None: flash(msg) return render_template('play/game.html', game=game)
def index(): db = get_db() # Get list of users current games games = db.execute("SELECT id, guesses FROM game" " WHERE user_id = ?", (g.user['id'], )).fetchall() return render_template('play/index.html', games=games)
def load_logged_in_user(): user_id = session.get('user_id') if user_id is None: g.user = None else: g.user = get_db().execute('SELECT * FROM user WHERE id = ?', (user_id, )).fetchone()
def new_game(): db = get_db() # Generate random number number = randint(1, 100) # Create game in database db.execute("INSERT INTO game (number, guesses, user_id) VALUES (?, 0, ?)", (number, g.user['id'])) db.commit() return redirect(url_for('index'))
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
def login(): if request.method == 'POST': username = request.form['username'] password = request.form['password'] db = get_db() error = None user = db.execute('SELECT * FROM user WHERE username = ?', (username, )).fetchone() if user is None: error = 'Incorrect username.' elif not check_password_hash(user['password'], password): error = 'Incorrect password.' if error is None: session.clear() session['user_id'] = user['id'] return redirect(url_for('index')) flash(error) return render_template('auth/login.html')
def register(): if request.method == 'POST': username = request.form['username'] password = request.form['password'] db = get_db() error = None if not username: error = 'Username required.' elif not password: error = 'Password required.' elif db.execute('SELECT id FROM user WHERE username = ?', (username, )).fetchone() is not None: error = f'User {username} is already registered' if error is None: db.execute('INSERT INTO user (username, password) VALUES (?, ?)', (username, generate_password_hash(password))) db.commit() return redirect(url_for('auth.login')) flash(error) return render_template('auth/register.html')