def test_env_future_value_get(): env = Environment({"<key>": "banana"}) value = env("<key>") assert isinstance(value, FutureValue) assert value.get() == "banana"
def test_env_config_get_existing_key(): env = Environment({"HOST": "127.0.0.1"}) class Config(cabina.Config, cabina.Section): API_HOST = env.str("HOST") assert Config.API_HOST == "127.0.0.1"
def test_env_config_get_key_nonexisting_with_default(): env = Environment({"API_HOST": "127.0.0.1"}) class Config(cabina.Config, cabina.Section): API_HOST = env.str("HOST", default="localhost") assert Config.API_HOST == "localhost"
def test_env_section_get_key_nonexisting_with_default(): env = Environment({"API_HOST": "127.0.0.1"}) class Section(cabina.Section): API_HOST = env.str("HOST", default="localhost") assert Section.API_HOST == "localhost"
def test_env_section_get_existing_key(): env = Environment({"HOST": "127.0.0.1"}) class Section(cabina.Section): API_HOST = env.str("HOST") assert Section.API_HOST == "127.0.0.1"
def test_env_future_value_get_nonexisting_key(): env = Environment({}) value = env("<key>") assert isinstance(value, FutureValue) with raises(EnvKeyError) as exc_info: value.get() assert exc_info.type is EnvKeyError assert str(exc_info.value) == "'<key>' does not exist"
def test_env_future_value_get_cached(): environ = {"<key>": "value-1"} env = Environment(environ) value = env("<key>") assert isinstance(value, FutureValue) assert value.get() == "value-1" environ["<key>"] = "value-2" assert value.get() == "value-1"
def test_env_future_value_get_default_value(): env = Environment({}) value = env("<key>", None) assert isinstance(value, FutureValue) assert value.get() is None value = env("<key>", "default") assert isinstance(value, FutureValue) assert value.get() == "default"
def test_env_section_get_nonexisting_key(): env = Environment({}) class Section(cabina.Section): API_HOST = env.str("HOST") with raises(Exception) as exc_info: Section.API_HOST assert exc_info.type is EnvKeyError assert str(exc_info.value) == "'HOST' does not exist"
def test_env_config_prefetch(): env = Environment({ "HOST": "localhost", "PORT": "8080", }) class Config(cabina.Config, cabina.Section): API_HOST = env.str("HOST") API_PORT = env.int("PORT") Config.prefetch()
def test_env_config_prefetch_with_invalid_values(): env = Environment({"PORT": "number"}) class Config(cabina.Config, cabina.Section): API_HOST = env.str("HOST") API_PORT = env.int("PORT") with raises(Exception) as exc_info: Config.prefetch() message = "\n".join([ "Failed to prefetch:", "- Config.API_HOST: 'HOST' does not exist", "- Config.API_PORT: Failed to parse 'number' as int", ]) assert exc_info.type is ConfigEnvError assert str(exc_info.value) == message
def test_env_config_prefetch_with_sections(): env = Environment({"DEBUG": "yes"}) class Config(cabina.Config, cabina.Section): TZ = env.str("TZ") DEBUG = env.bool("DEBUG") class Main(cabina.Section): API_HOST = env.str("HOST", default="localhost") API_PORT = env.int("PORT") with raises(Exception) as exc_info: Config.prefetch() message = "\n".join([ "Failed to prefetch:", "- Config.TZ: 'TZ' does not exist", "- Config.Main.API_PORT: 'PORT' does not exist", ]) assert exc_info.type is ConfigEnvError assert str(exc_info.value) == message
def test_env_float_incorrect_default(): env = Environment({}) with raises(AssertionError): env.float("<key>", default="3.14")
def test_env_str(): env = Environment({"<key>": "banana "}) value = cast(FutureValue, env.str("<key>")) assert value.get() == "banana"
def test_env_raw_with_default(): env = Environment({"<key1>": "1"}) value = env.raw("<key2>", default="2") assert isinstance(value, FutureValue) assert value.get() == "2"
def test_env_raw(): env = Environment({"<key>": "1234"}) value = env.raw("<key>") assert isinstance(value, FutureValue) assert value.get() == "1234"
def test_env_get_with_default(): env = Environment({}) assert env.get("<key>", default=None) is None assert env.get("<key>", default="banana") == "banana"
def test_env_tuple_with_separator(): env = Environment({"<key>": "first second"}) value = cast(FutureValue, env.tuple("<key>", separator=" ")) assert value.get() == ("first", "second",)
def test_env_tuple(): env = Environment({"<key>": "first, second"}) value = cast(FutureValue, env.tuple("<key>")) assert value.get() == ("first", "second",)
def test_env_future_value_get_value_custom_parser(): env = Environment({"<key>": "1234"}) value = env("<key>", parser=int) assert isinstance(value, FutureValue) assert value.get() == 1234
def test_env_prefix(): env = Environment({"APP_NAME": "banana"}, prefix="APP_") assert env("NAME").get() == "banana" assert env.get("NAME") == "banana"
def test_env_repr(): empty_env = Environment({}) assert repr(empty_env) == "cabina.Environment({})" env = Environment({"<key>": "banana"}) assert repr(env) == "cabina.Environment({'<key>': 'banana'})"
def test_env_config_define_nonexisting_key(): env = Environment({}) class Config(cabina.Config, cabina.Section): API_HOST = env.str("HOST")
def test_env_float(): env = Environment({"<key>": "3.14"}) value = cast(FutureValue, env.float("<key>")) assert value.get() == 3.14
def test_env_str_incorrect_default(): env = Environment({}) with raises(AssertionError): env.str("<key>", default=None)
def test_env_tuple_incorrect_default(): env = Environment({}) with raises(AssertionError): env.tuple("<key>", default=[])
def test_env_get(): env = Environment({"<key>": "1234"}) assert env.get("<key>") == "1234"
def test_env_get_with_parser(): env = Environment({"<key>": "1234"}) assert env.get("<key>", parser=int) == 1234
def test_env_section_define_nonexisting_key(): env = Environment({}) class Section(cabina.Section): API_HOST = env.str("HOST")
def test_env_int_incorrect_default(): env = Environment({}) with raises(AssertionError): env.int("<key>", default="42")