def test_pure_leaf_factory(): tree = Tree() @tree.root() class Config(): var1 = LeafFactory(int, 42) assert tree.load({}).var1 == 42 with raises(MultipleInvalid): assert tree.load({'var1': 'teve'})
def test_deep_dump(): tree = Tree() @tree.root() class Config(): @tree.node() class oidc(): url = str @tree.node() class credentials(): client_id = str key = str source_data = { 'oidc': { 'url': 'teve', 'credentials': { 'client_id': 'muha', 'key': 'da_key' } } } cfg = tree.load(source_data) cfg.oidc.credentials.client_id = 'teve' data = tree.dump(cfg) assert data['oidc']['credentials']['client_id'] == 'teve'
def test_dict_node_load(): class ServerConfig(NodeBase): host = str port = int def serialize(self): return self.__dict__ tree = Tree() @tree.root() class Config(): servers = tree.dict_node(str, ServerConfig) source_data = {'servers':{'server1': {'host': 'teve', 'port': 42}, 'server2': {'host': 'muha', 'port': 422}}} cfg = tree.load(source_data) assert cfg.servers['server1'].host == 'teve' assert cfg.servers['server1'].port == 42 assert cfg.servers['server2'].host == 'muha' assert cfg.servers['server2'].port == 422
def test_deep_dict_node_load(): class ServerConfig(NodeBase): url = str def serialize(self): return self.__dict__ class credentials(NodeBase): client_id = str secret = str def serialize(self): return self.__dict__ tree = Tree() @tree.root() class Config(): servers = tree.dict_node(str, ServerConfig) source_data = {'servers':{'server1': {'url': 'http://teve.hu', 'credentials': {'client_id': 'id1', 'secret': '42'}}}} cfg = tree.load(source_data) assert cfg.servers['server1'].url == 'http://teve.hu' assert cfg.servers['server1'].credentials.client_id == 'id1' assert cfg.servers['server1'].credentials.secret == '42'
def test_list_node_dump_one_type(): class ServerConfig(NodeBase): host = str port = int def serialize(self): return self.__dict__ tree = Tree() @tree.root() class Config(): servers = tree.list_node([ServerConfig]) source_data = { 'servers': [{ 'host': 'teve', 'port': 42 }, { 'host': 'muha', 'port': 422 }] } cfg = tree.load(source_data) cfg.servers[0].port = 84 data = tree.dump(cfg) assert data['servers'][0]['port'] == 84
def test_list_node_load_one_type(): class ServerConfig(NodeBase): host = str port = int def serialize(self): return self.__dict__ tree = Tree() @tree.root() class Config(): servers = tree.list_node([ServerConfig]) source_data = {'servers':[{'host': 'teve', 'port': 42}, {'host': 'muha', 'port': 422}]} cfg = tree.load(source_data) assert cfg.servers[0].host == 'teve' assert cfg.servers[0].port == 42 assert cfg.servers[1].host == 'muha' assert cfg.servers[1].port == 422
def test_load_datetime(): tree = Tree() @tree.root() class Config(): param_leaf_1 = datetime cfg = tree.load({'param_leaf_1': '2018-04-02 14:42:42'}) assert cfg.param_leaf_1.day
def test_load_datetime_default(): tree = Tree() @tree.root() class Config(): param_leaf_1 = datetime(2018, 4, 2, 14, 42, 12) cfg = tree.load({}) assert cfg.param_leaf_1.day == 2
def test_load_convert_underscores_to_hypens(): tree = Tree(Settings(convert_underscores_to_hypens = True)) @tree.root() class Config(): param_simpe_1 = int cfg = tree.load({'param-simpe-1': 42}) assert cfg.param_simpe_1 == 42
def test_simple_param_default(): tree = Tree() @tree.root() class Config(): param_simpe_1 = 42 cfg = tree.load({}) assert cfg.param_simpe_1 == 42
def test_simple_param_required(): tree = Tree() @tree.root() class Config(): param_simpe_1 = int cfg = tree.load({'param_simpe_1': 42}) assert cfg.param_simpe_1 == 42
def test_dump_method_name_in_node_classes(): tree = Tree(Settings(dump_method_name_in_node_classes = 'dump')) @tree.root() class Config(): param_simpe_1 = 42 cfg = tree.load({}) assert cfg.dump() == {'param_simpe_1': 42}
def test_load_simple_param_var_annotation(): tree = Tree() @tree.root() class Config(): param_simpe_1: int cfg = tree.load({'param_simpe_1': 42}) assert cfg.param_simpe_1 == 42
def test_dump_convert_underscores_to_hypens_and_camel_case_to_hypens(): tree = Tree(Settings(convert_underscores_to_hypens = True, convert_camel_case_to_hypens = True)) @tree.root() class Config(): param_simpe_1 = 42 cfg = tree.load({}) cfg.param_simpe_1 = 84 data = tree.dump(cfg) assert data == {'param-simpe-1': 84}
def test_datetime_dump(): tree = Tree() @tree.root() class Config(): param_leaf_1 = datetime cfg = tree.load({'param_leaf_1': '2018-04-02 14:42:42'}) cfg.param_leaf_1 = cfg.param_leaf_1.replace(day=8) data = tree.dump(cfg) assert data['param_leaf_1'] == '2018-04-08T14:42:42'
def test_dump_simple_param(): tree = Tree() @tree.root() class Config(): param_simpe_1 = 42 cfg = tree.load({}) cfg.param_simpe_1 = 84 data = tree.dump(cfg) assert data['param_simpe_1'] == 84
def test_dump_method_name_in_node_classes_sub(): tree = Tree(Settings(dump_method_name_in_node_classes = 'dump')) @tree.root() class Config(): @tree.node() class node1(): param = int cfg = tree.load({'node1': {'param': 42}}) assert cfg.dump() == {'node1': {'param': 42}} assert cfg.node1.dump() == {'param': 42}
def test_load_database_sqlite(): tree = Tree() @tree.root() class Config(): param = DatabaseLeaf print(tree.build_schema()) cfg = tree.load({'param': 'sqlite:///teve.db'}) # type: Config assert cfg.param.driver == 'sqlite' assert cfg.param.name == 'teve.db' assert cfg.param.uri == 'sqlite:///teve.db'
def test_load_int_enum(): class Camel(IntEnum): ONE = 1 TWO = 2 tree = Tree() @tree.root() class Config(): param = Camel cfg = tree.load({'param': 1}) assert cfg.param == Camel.ONE
def test_load_enum_default(): class Animal(Enum): CAT = 'cat' DOG = 'dog' PLATYPUS = 'platypus' tree = Tree() @tree.root() class Config(): param = Animal.CAT cfg = tree.load({}) assert cfg.param == Animal.CAT
def test_load_convert_camel_case_to_hypens(): tree = Tree(Settings(convert_camel_case_to_hypens = True)) @tree.root() class Config(): @tree.node() class TypicalClassName(): param = int print(tree.build_schema()) cfg = tree.load({'typical-class-name': {'param': 42}}) assert cfg.TypicalClassName.param == 42
def test_dump_convert_camel_case_to_hypens(): tree = Tree(Settings(convert_camel_case_to_hypens = True)) @tree.root() class Config(): @tree.node() class TypicalClassName(): param = int cfg = tree.load({'typical-class-name': {'param': 42}}) cfg.TypicalClassName.param = 84 data = tree.dump(cfg) assert data == {'typical-class-name': {'param': 84}}
def test_dump_enum(): class Animal(Enum): CAT = 'cat' DOG = 'dog' PLATYPUS = 'platypus' tree = Tree() @tree.root() class Config(): param = Animal cfg = tree.load({'param': 'cat'}) cfg.param = Animal.PLATYPUS data = tree.dump(cfg) assert data == {'param': 'platypus'}
def test_load_database(): tree = Tree() @tree.root() class Config(): param = DatabaseLeaf print(tree.build_schema()) cfg = tree.load({'param': 'teve://*****:*****@domain.hu/dbname'}) # type: Config assert cfg.param.driver == 'teve' assert cfg.param.username == 'user' assert cfg.param.password == 'pass' assert cfg.param.host == 'domain.hu' assert cfg.param.name == 'dbname' assert not cfg.param.port assert cfg.param.uri == 'teve://*****:*****@domain.hu/dbname'
def test_deep_load(): tree = Tree() @tree.root() class Config(): @tree.node() class oidc(): url = str @tree.node() class credentials(): client_id = str key = str source_data = {'oidc': {'url': 'teve', 'credentials': {'client_id': 'muha', 'key': 'da_key'}}} cfg = tree.load(source_data) assert cfg.oidc.url == 'teve' assert cfg.oidc.credentials.client_id == 'muha' assert cfg.oidc.credentials.key == 'da_key'
def test_load_logger_config(): tree = Tree() @tree.root() class Config(): param = PythonLoggerLeaf print(tree.build_schema()) data = { "disable_existing_loggers": True, "formatters": { "standard": { "format": "%(asctime)s - %(levelname)s - %(filename)s:%(lineno)d - %(name)s: %(message)s" } }, "handlers": { "console": { "class": "logging.StreamHandler", "formatter": "standard", "level": "DEBUG" }, }, "loggers": { "teve": { "handlers": ["console"], "level": "DEBUG", "propagate": True }, }, "version": 1 } cfg = tree.load({'param': data}) # type: Config assert cfg.param == data
from configpp.tree import Tree tree = Tree() @tree.root() class Config(): param_simple_1 = 42 cfg = tree.load({}) print(cfg.param_simple_1)