Example #1
0
def test_inject_commit_on_success():
    @json_response
    @inject(db=manage_session(commit_on_success=True))
    def view(db):
        db.execute("CREATE TABLE foo (id INTEGER);")
        db.execute("INSERT INTO foo (id) VALUES (9);")

    @json_response
    @inject_db
    def view2(db):
        return [row.id for row in db.execute("select id from foo;").fetchall()]

    app = create_app(
        routes=[Route.to("GET", "/foo", view),
                Route.to("GET", "/bar", view2)],
        callbacks=[callback_factory(sqlalchemy_database, config, key="db")],
    )
    with app.test_client() as client:
        response = client.get("/foo")
    assert response.status_code == 200

    with app.test_client() as client:
        response = client.get("/bar")
    assert response.status_code == 200
    assert response.json == [9]
Example #2
0
def test_route_registered():
    def view():
        return flask.jsonify(5)

    app = create_app(routes=[Route.to("GET", "/foo", view)])
    with app.test_client() as client:
        response = client.get("/foo")

    assert response.json == 5
    assert response.status_code == 200
Example #3
0
def test_error_handlers_handled_exception():
    def err():
        raise BadRequest("agh!")

    app = create_app(routes=[Route.to("GET", "/foo", err)], error_handlers=default_error_handlers)
    with app.test_client() as client:
        response = client.get("/foo")

    assert response.json == {"error": "agh!"}
    assert response.status_code == 400
Example #4
0
def test_changed_status_code():
    @json_response(status=201)
    def view():
        return 5

    app = create_app(routes=[Route.to("GET", "/foo", view)], )
    with app.test_client() as client:
        response = client.get("/foo")

    assert response.json == 5
    assert response.status_code == 201
Example #5
0
def test_error_handlers_normal_exception_no_sentry():
    def err():
        raise Exception("agh!")

    app = create_app(routes=[Route.to("GET", "/foo", err)], error_handlers=default_error_handlers)
    with app.test_client() as client:
        response = client.get("/foo")

    assert response.json["error"] == "(Exception) agh!"
    assert response.status_code == 500
    assert "traceback" in response.json
Example #6
0
def test_inject_invalid():
    @json_response
    @inject(db=manage_session(commit_on_success=True))
    def view(db):
        pass

    app = create_app(routes=[Route.to("GET", "/foo", view)])
    with app.test_client() as client:
        response = client.get("/foo")
    assert response.status_code == 500
    assert response.json[
        "error"] == "(ValueError) db is not registered in flask's extensions."
Example #7
0
def test_error_handlers_werkzeug():
    app = create_app([], error_handlers=default_error_handlers)
    with app.test_client() as client:
        response = client.get("/foo")

    assert response.json == {
        "error": (
            "(NotFound) 404 Not Found: The requested URL was not found on the server. "
            "If you entered the URL manually please check your spelling and try again."
        )
    }
    assert response.status_code == 404
Example #8
0
def test_database_injection():
    @json_response
    @inject_db
    def view(db):
        return db.execute("select 5").scalar()

    app = create_app(
        routes=[Route.to("GET", "/foo", view)],
        callbacks=[callback_factory(sqlalchemy_database, config, key="db")],
    )
    with app.test_client() as client:
        response = client.get("/foo")

    assert response.json == 5
    assert response.status_code == 200
Example #9
0
def test_config_applied():
    app = create_app(config={"foo": "bar"})

    assert app.config["foo"] == "bar"
Example #10
0
def test_non_proxied():
    create_app([], proxied=False)