Пример #1
0
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)
Пример #2
0
def get_all():
    cursor = get_db().cursor()
    query = ('SELECT symbol, company, total_count, price ' 'FROM stock')
    rows = cursor.execute(query).fetchall()
    cursor.close()

    return json.dumps([dict(ix) for ix in rows])
Пример #3
0
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)
Пример #4
0
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()
Пример #5
0
def get_stock(symbol):
    cursor = get_db().cursor()
    query = ('SELECT symbol, company, total_count, price '
             'FROM stock '
             'WHERE symbol = ?')
    stock = cursor.execute(query, [symbol]).fetchall()
    cursor.close()

    return [dict(ix) for ix in stock] if stock else None
Пример #6
0
def delete(symbol):
    if get_stock(symbol) is None:
        return make_response('Not found', 404)

    db = get_db()
    db.execute('DELETE FROM stock WHERE symbol = ?', (symbol, ))
    db.commit()

    return make_response('OK', 200)
Пример #7
0
def test_register(client, app):
    response = client.post('/auth/register',
                           data=json.dumps({
                               'username': '******',
                               'password': '******'
                           }),
                           content_type='application/json')
    assert response.status_code == 201
    assert response.data == b'Created'

    with app.app_context():
        assert get_db().execute(
            "select * from user where username = '******'", ).fetchone() is not None
Пример #8
0
def update(symbol):
    rj = request.get_json()
    company = rj['company']
    total_count = rj['total_count']
    price = rj['price']

    if get_stock(symbol) is None:
        return make_response('Does not exist', 404)

    db = get_db()
    db.execute(
        'UPDATE stock '
        'SET company = ?, total_count = ?, price = ? '
        'WHERE symbol = ?', (company, total_count, price, symbol))
    db.commit()

    return make_response('Updated', 200)
Пример #9
0
def create():
    req_json = request.get_json()
    symbol = req_json['symbol']
    company = req_json['company']
    total_count = req_json['total_count']
    price = req_json['price']

    if get_stock(symbol) is not None:
        return make_response('already exist', 409)

    db = get_db()
    db.execute(
        'INSERT INTO stock (symbol, company, total_count, price) '
        'VALUES (?, ?, ?, ?)', (symbol, company, total_count, price))
    db.commit()

    return make_response('Created', 201)
Пример #10
0
def login():
    req_json = request.get_json()
    username = req_json['username']
    password = req_json['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:
        # JWT
        session.clear()
        session['user_id'] = user['id']
        return make_response('OK', 200)

    return make_response(error, 404)
Пример #11
0
def register():
    req_json = request.get_json()
    username = req_json['username']
    password = req_json['password']

    db = get_db()
    error = None

    if not username:
        error = 'Username is required.'
    elif not password:
        error = 'Password is required.'
    elif db.execute('SELECT id FROM user WHERE username = ?',
                    (username, )).fetchone() is not None:
        error = 'User {} is already registered.'.format(username)

    if error is None:
        db.execute('INSERT INTO user (username, password) VALUES (?, ?)',
                   (username, generate_password_hash(password)))
        db.commit()
        return make_response('Created', 201)

    return make_response(error, 404)