File size: 2,103 Bytes
cb0011e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 | import pytest
from miniconf import Config, ConfigError, MissingKeyError, load_layers
def test_plain_values():
cfg = Config({"name": "app", "port": 8080})
assert cfg["name"] == "app"
assert cfg["port"] == 8080
assert len(cfg) == 2
def test_simple_reference():
cfg = Config({"host": "localhost", "url": "http://${host}/"})
assert cfg["url"] == "http://localhost/"
def test_two_references_in_one_value():
cfg = Config({"host": "db", "port": 5432, "dsn": "${host}:${port}"})
assert cfg["dsn"] == "db:5432"
def test_chained_reference():
cfg = Config({"a": "x", "b": "${a}-y", "c": "${b}-z"})
assert cfg["c"] == "x-y-z"
def test_missing_key_raises():
cfg = Config({"a": "${nope}"})
with pytest.raises(MissingKeyError):
cfg["a"]
def test_missing_key_message():
cfg = Config({"a": "${nope}"})
with pytest.raises(MissingKeyError) as excinfo:
cfg["a"]
assert str(excinfo.value) == "missing key 'nope' referenced from 'a'"
def test_non_string_passthrough():
cfg = Config({"n": 3, "flag": True, "nested": {"x": 1}})
assert cfg["n"] == 3
assert cfg["flag"] is True
assert cfg["nested"] == {"x": 1}
def test_get_returns_default_for_unknown_key():
cfg = Config({"a": "1"})
assert cfg.get("zzz") is None
assert cfg.get("zzz", "dflt") == "dflt"
def test_getitem_raises_keyerror_for_unknown_key():
cfg = Config({"a": "1"})
with pytest.raises(KeyError):
cfg["zzz"]
def test_raw_does_not_resolve():
cfg = Config({"a": "1", "b": "${a}"})
assert cfg.raw("b") == "${a}"
def test_from_layers_override():
cfg = Config.from_layers({"a": "1", "b": "2"}, {"b": "3"})
assert cfg.as_dict() == {"a": "1", "b": "3"}
def test_load_layers_rejects_non_dict():
with pytest.raises(ConfigError):
load_layers({"a": 1}, ["not", "a", "dict"])
def test_as_dict_resolves_all():
cfg = Config({"a": "1", "b": "${a}2"})
assert cfg.as_dict() == {"a": "1", "b": "12"}
def test_config_rejects_non_dict():
with pytest.raises(TypeError):
Config([("a", 1)])
|