Beispiel #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]
Beispiel #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
Beispiel #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
Beispiel #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
Beispiel #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
Beispiel #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."
Beispiel #7
0
def test_database_error_recovery():
    @json_response
    @inject_db
    def view(db):
        raise ValueError()

    @json_response
    @inject_db
    def view2(db):
        return db.execute("select 5").scalar()

    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 == 500

        response = client.get("/bar")

    assert response.json == 5
    assert response.status_code == 200
Beispiel #8
0
def test_parse_instance():
    expected_result = Route.to("GET", "/foo", view)
    result = Route.parse(expected_result)
    assert expected_result == result
Beispiel #9
0
def test_invalid_parse():
    in_route = (1, 2, 3, 4, 5, 6)
    with pytest.raises(ValueError) as e:
        Route.parse(in_route)
    assert str(e.value) == "Could not parse (1, 2, 3, 4, 5, 6) as route"
Beispiel #10
0
def test_parse_dict():
    in_route = dict(method="GET", path="/foo", view=view)
    result = Route.parse(in_route)

    expected_result = Route.to("GET", "/foo", view)
    assert expected_result == result
Beispiel #11
0
def test_parse_with_kwargs():
    in_route = ("GET", "/foo", view, dict(endpoint="woah"))
    result = Route.parse(in_route)

    expected_result = Route.to("GET", "/foo", view, endpoint="woah")
    assert expected_result == result
Beispiel #12
0
def test_parse():
    in_route = ("GET", "/foo", view)
    result = Route.parse(in_route)

    expected_result = Route.to("GET", "/foo", view)
    assert expected_result == result