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 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
|
import enum
import os
import unittest
import pytest
import confuse
from . import _root
class TypeCheckTest(unittest.TestCase):
def test_str_type_correct(self):
config = _root({"foo": "bar"})
value = config["foo"].get(str)
assert value == "bar"
def test_str_type_incorrect(self):
config = _root({"foo": 2})
with pytest.raises(confuse.ConfigTypeError):
config["foo"].get(str)
def test_int_type_correct(self):
config = _root({"foo": 2})
value = config["foo"].get(int)
assert value == 2
def test_int_type_incorrect(self):
config = _root({"foo": "bar"})
with pytest.raises(confuse.ConfigTypeError):
config["foo"].get(int)
class BuiltInValidatorTest(unittest.TestCase):
def test_as_filename_with_non_file_source(self):
config = _root({"foo": "foo/bar"})
value = config["foo"].as_filename()
assert value == os.path.join(os.getcwd(), "foo", "bar")
def test_as_filename_with_file_source(self):
source = confuse.ConfigSource({"foo": "foo/bar"}, filename="/baz/config.yaml")
config = _root(source)
config.config_dir = lambda: "/config/path" # type: ignore[attr-defined]
value = config["foo"].as_filename()
assert value == os.path.realpath("/config/path/foo/bar")
def test_as_filename_with_default_source(self):
source = confuse.ConfigSource(
{"foo": "foo/bar"}, filename="/baz/config.yaml", default=True
)
config = _root(source)
config.config_dir = lambda: "/config/path" # type: ignore[attr-defined]
value = config["foo"].as_filename()
assert value == os.path.realpath("/config/path/foo/bar")
def test_as_filename_wrong_type(self):
config = _root({"foo": None})
with pytest.raises(confuse.ConfigTypeError):
config["foo"].as_filename()
def test_as_path(self):
config = _root({"foo": "foo/bar"})
path_str = os.path.join(os.getcwd(), "foo", "bar")
try:
import pathlib
except ImportError:
with pytest.raises(ImportError):
value = config["foo"].as_path()
else:
value = config["foo"].as_path()
path = pathlib.Path(path_str)
assert value == path
def test_as_choice_correct(self):
config = _root({"foo": "bar"})
value = config["foo"].as_choice(["foo", "bar", "baz"])
assert value == "bar"
def test_as_choice_error(self):
config = _root({"foo": "bar"})
with pytest.raises(confuse.ConfigValueError):
config["foo"].as_choice(["foo", "baz"])
def test_as_choice_with_dict(self):
config = _root({"foo": "bar"})
res = config["foo"].as_choice(
{
"bar": "baz",
"x": "y",
}
)
assert res == "baz"
def test_as_choice_with_enum(self):
class Foobar(enum.Enum):
Foo = "bar"
config = _root({"foo": Foobar.Foo.value})
res = config["foo"].as_choice(Foobar)
assert res == Foobar.Foo
def test_as_choice_with_enum_error(self):
class Foobar(enum.Enum):
Foo = "bar"
config = _root({"foo": "foo"})
with pytest.raises(confuse.ConfigValueError):
config["foo"].as_choice(Foobar)
def test_as_number_float(self):
config = _root({"f": 1.0})
config["f"].as_number()
def test_as_number_int(self):
config = _root({"i": 2})
config["i"].as_number()
def test_as_number_string(self):
config = _root({"s": "a"})
with pytest.raises(confuse.ConfigTypeError):
config["s"].as_number()
def test_as_str_seq_str(self):
config = _root({"k": "a b c"})
assert config["k"].as_str_seq() == ["a", "b", "c"]
def test_as_str_seq_list(self):
config = _root({"k": ["a b", "c"]})
assert config["k"].as_str_seq() == ["a b", "c"]
def test_as_str(self):
config = _root({"s": "foo"})
config["s"].as_str()
def test_as_str_non_string(self):
config = _root({"f": 1.0})
with pytest.raises(confuse.ConfigTypeError):
config["f"].as_str()
def test_as_str_expanded(self):
config = _root({"s": "${CONFUSE_TEST_VAR}/bar"})
os.environ["CONFUSE_TEST_VAR"] = "foo"
assert config["s"].as_str_expanded() == "foo/bar"
def test_as_pairs(self):
config = _root({"k": [{"a": "A"}, "b", ["c", "C"]]})
assert [("a", "A"), ("b", None), ("c", "C")] == config["k"].as_pairs()
|