Exemple #1
0
def test_prefix():
    settings = LazySettings(filter_strategy=PrefixFilter("prefix"))
    _yaml = """
    prefix_a: a,b
    prefix_colors__white__code: "#FFFFFF"
    COLORS__white__name: "white"
    """
    load(settings, filename=_yaml)
    assert settings.a == "a,b"
    assert settings.COLORS.white.code == "#FFFFFF"
    with pytest.raises(AttributeError):
        settings.COLORS.white.name
Exemple #2
0
def test_prefix():
    settings = LazySettings(filter_strategy=PrefixFilter("prefix"))
    _json = """
    {
        "prefix_colors__yellow__code": "#FFCC00",
        "COLORS__yellow__name": "Yellow"
    }
    """
    load(settings, filename=_json)
    assert settings.COLORS.yellow.code == "#FFCC00"
    with pytest.raises(AttributeError):
        settings.COLORS.yellow.name
Exemple #3
0
def test_prefix():
    settings = LazySettings(filter_strategy=PrefixFilter("prefix"))
    ini = """
    prefix_a = "a,b"
    prefix_colors__white__code = '#FFFFFF'
    COLORS__white__name = 'white'
    """
    load(settings, filename=ini)
    assert settings.a == "a,b"
    assert settings.COLORS.white.code == "#FFFFFF"
    with pytest.raises(AttributeError):
        settings.COLORS.white.name
Exemple #4
0
def test_from_env_method_with_prefix(clean_env, tmpdir):
    data = {
        "default": {
            "prefix_a_default": "From default env"
        },
        "development": {
            "prefix_value": "From development env",
            "prefix_only_in_development": True,
        },
        "other": {
            "prefix_value": "From other env",
            "prefix_only_in_other": True,
            "not_prefixed": "no prefix",
        },
    }
    toml_path = str(tmpdir.join("base_settings.toml"))
    toml_loader.write(toml_path, data, merge=False)
    settings = LazySettings(
        settings_file=toml_path,
        environments=True,
        filter_strategy=PrefixFilter("prefix"),
    )
    settings.set("ARBITRARY_KEY", "arbitrary value")

    assert settings.VALUE == "From development env"
    assert settings.A_DEFAULT == "From default env"
    assert settings.ONLY_IN_DEVELOPMENT is True
    assert settings.ARBITRARY_KEY == "arbitrary value"
    assert settings.get("ONLY_IN_OTHER") is None

    # clone the settings object pointing to a new env
    other_settings = settings.from_env("other")
    assert other_settings.VALUE == "From other env"
    assert other_settings.A_DEFAULT == "From default env"
    assert other_settings.ONLY_IN_OTHER is True
    assert other_settings.get("ARBITRARY_KEY") is None
    assert other_settings.get("ONLY_IN_DEVELOPMENT") is None
    with pytest.raises(AttributeError):
        other_settings.not_prefixed
    with pytest.raises(AttributeError):
        # values set programatically are not cloned
        other_settings.ARBITRARY_KEY
    with pytest.raises(AttributeError):
        # values set only in a specific env not cloned
        other_settings.ONLY_IN_DEVELOPMENT
    # assert it is cached not created twice
    assert other_settings is settings.from_env("other")
Exemple #5
0
def test_envless_mode_with_prefix(tmpdir):
    data = {
        "prefix_foo": "bar",
        "hello": "world",
        "prefix_default": 1,
        "prefix_databases": {"default": {"port": 8080}},
    }
    toml_loader.write(str(tmpdir.join("settings.toml")), data)

    settings = LazySettings(
        settings_file="settings.toml", filter_strategy=PrefixFilter("prefix")
    )  # already the default
    assert settings.FOO == "bar"
    with pytest.raises(AttributeError):
        settings.HELLO
    assert settings.DEFAULT == 1
    assert settings.DATABASES.default.port == 8080
Exemple #6
0
def test_prefix_is_not_str_raises():
    with pytest.raises(TypeError):
        toml_loader.load(LazySettings(filter_strategy=PrefixFilter(int)))
    with pytest.raises(TypeError):
        toml_loader.load(LazySettings(filter_strategy=PrefixFilter(True)))
Exemple #7
0
    "HOST": "prod_server.com from toml",
    "PORT": 5000,
    "USERNAME": "******",
    "PASSWORD": "******",
    "LEVELS": ["debug", "info", "warning"],
    "MONEY": 500.5,
    "AGE": 42,
    "ENABLED": True,
    "WORKS": "toml_example in prod env",
    "CUSTOM": "this is custom from [production]",
    "TEST_LOADERS": {
        "dev": "test_dev",
        "prod": "test_prod"
    },
}

for key, value in assertions.items():
    found = settings.from_env("production").get(key)
    assert found == getattr(settings.from_env("production"), key)
    assert found == value, f"expected: {key}: [{value}] found: [{found}]"

settings_with_prefix = Dynaconf(
    settings_files=["settings.toml"],
    filter_strategy=PrefixFilter("prefix"),
    environments=True,
)
with settings_with_prefix.using_env("default"):
    assert settings_with_prefix.CUSTOM == "this is custom when we set a prefix"
assert (settings_with_prefix.from_env("production").CUSTOM ==
        "this is custom when we set a prefix")