Exemple #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.value)
Exemple #2
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)
Exemple #3
0
def test_delete(client, app):
    response = client.delete('/dictionary/test_key')
    assert response.status_code == 200
    assert b"null" in response.data

    with app.app_context():
        row = get_db().execute(
            "select * from dictionary where key = 'test_key'",
        ).fetchone()
        assert row is None
Exemple #4
0
def test_update_not_found(client, app):
    with app.app_context():
        response = client.put(
            '/dictionary/some_random_key',
            data=json.dumps(dict(key='some_random_key', value='random_value')),
            content_type='application/json'
        )
        assert response.status_code == 404
        row = get_db().execute(
            "select * from dictionary where key = 'some_random_key'",
        ).fetchone()
        assert row is None
Exemple #5
0
def test_update(client, app):
    with app.app_context():
        response = client.put(
            '/dictionary/test_key',
            data=json.dumps(dict(key='test_key', value='replaced')),
            content_type='application/json'
        )
        assert response.status_code == 201
        row = get_db().execute(
            "select * from dictionary where key = 'test_key'",
        ).fetchone()
        assert row is not None
        assert row['value'] == 'replaced'
Exemple #6
0
def test_create_already_exist(client, app):
    with app.app_context():
        response = client.post(
            '/dictionary',
            data=json.dumps(dict(key='mail.ru', value='target')),
            content_type='application/json'
        )
        assert response.status_code == 409
        row = get_db().execute(
            "select count(1) from dictionary where key = 'mail.ru'",
        ).fetchone()
        assert row is not None
        assert row[0] == 1
Exemple #7
0
def update(key):
    dict_value = get_dictionary_value(key)
    if request.method == 'PUT':
        req_data = request.get_json()
        key = req_data['key']
        value = req_data['value']

        if dict_value is None:
            return make_response(
                jsonify(result="Not found", time=datetime.now()), 404)
        else:
            db = get_db()
            db.execute('UPDATE dictionary SET value = ?'
                       'WHERE key = ?', (value, key))
            db.commit()
        return make_response(jsonify(result=value, time=datetime.now()), 201)
Exemple #8
0
def create():
    if request.method == 'POST':
        req_data = request.get_json()
        key = req_data['key']
        value = req_data['value']
        dict_value = get_dictionary_value(key)

        if dict_value:
            return make_response(
                jsonify(result="Key already exist", time=datetime.now()), 409)

        db = get_db()
        db.execute('INSERT INTO dictionary (key, value)'
                   ' VALUES (?, ?)', (key, value))
        db.commit()
        return make_response(jsonify(result=value, time=datetime.now()), 201)
Exemple #9
0
def get_dictionary_value(key):
    dict_value = get_db().execute(
        'SELECT key, value'
        ' FROM dictionary'
        ' WHERE key = ?', (key, )).fetchone()
    return dict_value
Exemple #10
0
def delete(key):
    if request.method == 'DELETE':
        db = get_db()
        db.execute('DELETE FROM dictionary' ' WHERE key = ?', (key, ))
        db.commit()
        return make_response(jsonify(result="null", time=datetime.now()), 200)