예제 #1
0
def test_config_dict_get():
    s = types.ConfigDict()
    with pytest.raises(KeyError):
        s.get("foo")

    s = types.ConfigDict({"foo": 42})
    assert s.get("foo") == 42
    with pytest.raises(KeyError):
        s.get("bar")

    s = types.ConfigDict({"foo": 42, "bar": {"baz": 43}})
    assert s.get("foo") == 42
    assert s.get("bar") == {"baz": 43}
    assert s.get("bar.baz") == 43
예제 #2
0
def test_config_dict_supports_array_indexes():
    s = types.ConfigDict()
    s.set("foo.2", "bar")
    assert s.get("foo") == [None, None, "bar"]
    assert s.get("foo.0") is None
    assert s.get("foo.1") is None
    assert s.get("foo.2") == "bar"

    with pytest.raises(ValueError) as exc:
        s.set("foo.-42", "Cannot happpen")
    assert str(exc.value) == "Invalid key `foo.-42`"

    with pytest.raises(KeyError):
        s.get("foo.3")

    with pytest.raises(KeyError):
        s.get("foo.-1")

    s.set("foo.0", 42)
    assert s.get("foo") == [42, None, "bar"]

    s.delete("foo.1")
    assert s.get("foo") == [42, "bar"]

    with pytest.raises(ValueError) as exc:
        s.delete("foo.-1")
    assert str(exc.value) == "Invalid key `foo.-1`"
예제 #3
0
def test_config_dict_reset():
    s = types.ConfigDict({"moo": 1})
    assert s.get("moo") == 1

    s.reset({"foo": 42, "bar": {"baz": 43}})
    with pytest.raises(KeyError):
        s.get("moo")
    assert s.as_dict() == {"foo": 42, "bar": {"baz": 43}}
예제 #4
0
파일: server.py 프로젝트: ecmwf/servicelib
    def __init__(self, fname):
        self.fname = fname
        self.config = types.ConfigDict()

        with open(fname, "rb") as f:
            self.config.reset(yaml.safe_load(f))

        self.read_only = os.environ.get("SERVICELIB_CONFIG_SERVER_READ_ONLY") == "true"
        self.lock = threading.Lock()
예제 #5
0
def test_config_dict_set():
    s = types.ConfigDict()
    with pytest.raises(KeyError):
        s.get("foo")

    s.set("foo", 42)
    assert s.get("foo") == 42

    s.set("bar.baz", 43)
    assert s.get("bar") == {"baz": 43}
    assert s.get("bar.baz") == 43
    with pytest.raises(ValueError):
        s.set("", 42)
예제 #6
0
def test_config_dict_delete():
    s = types.ConfigDict({"foo": 42, "bar": {"baz": 43}})

    with pytest.raises(KeyError):
        s.delete("no-such-key")

    with pytest.raises(KeyError):
        s.delete("no-such-key.no-such-key-either")

    with pytest.raises(KeyError):
        s.delete("foo.no-such-key")

    s.delete("foo")
    with pytest.raises(KeyError):
        s.get("foo")

    s.delete("bar")
    with pytest.raises(KeyError):
        s.get("bar.baz")
    with pytest.raises(KeyError):
        s.get("bar")
예제 #7
0
def test_config_dict_as_dict():
    s = types.ConfigDict({"foo": 42, "bar": {"baz": 43}})
    assert s.as_dict() == {"foo": 42, "bar": {"baz": 43}}