Ejemplo n.º 1
0
def test_dump_and_get_example(set_env, env: Env):
    set_env(
        {
            "VAR_1": "some string",
            "VAR_2": "4",
            "VAR_3": "4.5",
            "VAR_4": "True,False",
            "EXTRA_VAR": "dont read this",
        }
    )
    env.str("VAR_1", default="some default")
    env.int("VAR_2")
    env.decimal("VAR_3")
    env.list("VAR_4", subcast=bool)
    env.json("NON_EXISTING", default={})
    assert env.dump() == {
        "VAR_1": ParsedValue("some string", "str", True),
        "VAR_2": ParsedValue(4, "int", False),
        "VAR_3": ParsedValue(D("4.5"), "decimal", False),
        "VAR_4": ParsedValue([True, False], "list", False),
        "NON_EXISTING": ParsedValue({}, "json", True),
    }
    assert env.get_example() == (
        "NON_EXISTING=Optional[json]\n"
        "VAR_1=Optional[str]\n"
        "VAR_2=int\n"
        "VAR_3=decimal\n"
        "VAR_4=list\n"
    )
Ejemplo n.º 2
0
def main() -> None:
    def shutdown(sig: int, frame_type: FrameType | None) -> None:
        if conn.websocket is not None and conn.loop is not None:
            for task in asyncio.all_tasks(conn.loop):
                task.cancel()
            coro = conn.websocket.close()
            asyncio.run_coroutine_threadsafe(coro, conn.loop)

    env = Env()
    env.read_env()

    host = env.str("SHOWDOWN_HOST")
    port = env.int("SHOWDOWN_PORT")
    protocol = "wss" if port == 443 else "ws"
    url = f"{protocol}://{host}:{port}/showdown/websocket"

    conn = Connection(
        url=url,
        username=env.str("USERNAME"),
        password=env.str("PASSWORD"),
        avatar=env.str("AVATAR", default=""),
        statustext=env.str("STATUSTEXT", default=""),
        rooms=env.list("ROOMS", default=[]),
        main_room=env.str("MAIN_ROOM"),
        command_character=env.str("COMMAND_CHARACTER"),
        administrators=env.list("ADMINISTRATORS", default=[]),
        webhooks=env.json("WEBHOOKS", default={}),
    )

    signal.signal(signal.SIGINT, shutdown)

    conn.open_connection()
Ejemplo n.º 3
0
def test_validators_that_return(set_env, env: Env):
    def is_positive(val):
        return val > 0

    def is_negative(val):
        return val < 0

    set_env({"AN_INTEGER": "982"})

    assert env.int("AN_INTEGER", validate=is_positive) == 982
    assert env.int("AN_INTEGER", validate=(is_positive, is_positive)) == 982

    with pytest.raises(Exception):
        env.int("AN_INTEGER", validate=is_negative)
    with pytest.raises(Exception):
        env.int("AN_INTEGER", validate=(is_positive, is_negative))
Ejemplo n.º 4
0
def test_default_to_none(env: Env):
    assert env.int("THIS_IS_NOT_IN_ENV", default=None) is None
Ejemplo n.º 5
0
def test_name_starts_with_number(env: Env):
    with pytest.raises(ValueError) as exc_info:
        env.int("7HIS_STARTS_WITH_NUMBER")
    err_msg = str(exc_info.value)
    assert "Environment variable name can not start with a number" in err_msg
    assert "7HIS_STARTS_WITH_NUMBER" in err_msg
Ejemplo n.º 6
0
def test_invalid_char_in_name(env: Env):
    with pytest.raises(ValueError) as exc_info:
        env.int("HERES_AN_INVALID_CHAR_=")
    err_msg = str(exc_info.value)
    assert "Environment variable name contains invalid character(s)" in err_msg
    assert "HERES_AN_INVALID_CHAR_=" in err_msg
Ejemplo n.º 7
0
def test_empty_str_name(env: Env):
    with pytest.raises(ValueError) as exc_info:
        env.int("")
    assert "Environment variable name can not be an empty string" in str(exc_info.value)
Ejemplo n.º 8
0
def test_missing(env: Env):
    with pytest.raises(Exception) as exc_info:
        env.int("THIS_IS_NOT_IN_ENV")
    assert '"THIS_IS_NOT_IN_ENV" is missing' in str(exc_info.value)