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)])