async def test_async_get_connection_passed_to_config():
    async def get_conn():
        return "some connection"

    class ForConnectionSerializer(ModelSerializer):
        class Meta:
            model = models.users
            fields = "__all__"

    rest_config = {"get_connection": get_conn}
    get_base_app(rest_config)  # init app with config
    db_service = await ForConnectionSerializer().get_db_service()
    assert db_service.connection is await get_conn()
async def test_config_setup_with_async_get_connection():
    async def get_conn():
        return "some connection"

    rest_config = {"get_connection": get_conn}
    app = get_base_app(rest_config)
    cfg = app[APP_CONFIG_KEY]
    assert cfg.get_connection is get_conn, "wrong custom `get_connection` applied to settings"
    assert asyncio.iscoroutinefunction(
        cfg.get_connection), "`get_connection` is not async"
    assert await cfg.get_connection() == await get_conn()
def test_default_config_setup():
    app = get_base_app()
    assert APP_CONFIG_KEY in app, "rest config isn't presented in app"
    cfg = app[APP_CONFIG_KEY]
    assert hasattr(
        cfg,
        "get_connection"), "config doesn't have `get_connection` attribute"
    assert hasattr(
        cfg,
        "db_service_class"), "config doesn't have `db_service_class` attribute"
    assert hasattr(cfg, "_db_orm_mapping")
    assert cfg.app_connection_property == DEFAULT_APP_CONN_PROP
    assert cfg.schema_type == PG_SA
示例#4
0
def get_app(rest_config: typing.Mapping = None):
    app = get_base_app(rest_config=rest_config)
    app_conn_prop = app[APP_CONFIG_KEY].app_connection_property
    app.on_startup.append(partial(init_pg, app_conn_prop))
    app.on_cleanup.append(partial(close_pg, app_conn_prop))
    return app
def test_invalid_schema_type():
    rest_config = {"schema_type": "invalid"}
    with pytest.raises(AssertionError, match="`schema_type` has to be one of"):
        get_base_app(rest_config)
def test_config_invalid_app_connection_property(conn_prop):
    rest_config = {"app_connection_property": conn_prop}
    with pytest.raises(AssertionError, match="app_connection_property"):
        get_base_app(rest_config)
def test_config_valid_app_connection_property(conn_prop):
    rest_config = {"app_connection_property": conn_prop}
    app = get_base_app(rest_config)
    cfg = app[APP_CONFIG_KEY]
    assert cfg.app_connection_property == conn_prop
def test_wrong_get_connection_config_setup(get_connection):
    rest_config = {"get_connection": get_connection}
    with pytest.raises(AssertionError,
                       match="`get_connection` has to be async callable"):
        get_base_app(rest_config)