コード例 #1
0
def test_repeated_schema_generation():

    tree = Tree()

    @tree.root()
    class Config():

        param_simpe_1: int

    tree.build_schema()
    schema = tree.build_schema()
    assert len(schema.schema) == 1
コード例 #2
0
def test_node_go_deep():

    tree = Tree()

    @tree.root()
    class Config():

        @tree.node()
        class oidc():
            config_url = str

            @tree.node()
            class credential():
                client_id = str
                key = str

    source_data = {'oidc': {'config_url': 'teve', 'credential': {'client_id': 'muha', 'key': 'da_key'}}}

    schema = tree.build_schema()

    assert len(schema.schema) == 1
    with raises(MultipleInvalid):
        assert schema({'oidc': 42})
    with raises(MultipleInvalid):
        assert schema({'oidc': {}})
    with raises(MultipleInvalid):
        assert schema({'oidc': {'config_url': 'dd'}})

    assert schema(source_data) == source_data
コード例 #3
0
def test_list_root_without_class():

    tree = Tree()

    tree.set_root(tree.list_node([int]))

    schema = tree.build_schema()
    assert schema([1])
コード例 #4
0
def test_list_root():

    tree = Tree()

    @tree.list_root([int])
    class Config():
        pass

    schema = tree.build_schema()
    assert schema([1])
コード例 #5
0
def test_list_param_default():
    tree = Tree()

    @tree.root()
    class Config():

        param = [1, 2, str]

    schema = tree.build_schema()
    assert schema({'param': [1, 'teve']}) == {'param': [1, 'teve']}
コード例 #6
0
def test_list_param_default_only_values_various_types():
    tree = Tree()

    @tree.root()
    class Config():

        param = [1, 2, 'teve']

    schema = tree.build_schema()
    assert schema({}) == {'param': [1, 2, 'teve']}
    with raises(MultipleInvalid):
        schema({'param': [1.2]})
コード例 #7
0
def test_list_param_optional():
    tree = Tree()

    @tree.root()
    class Config():

        param_simpe_1 = tree.list_node([int], [])

    schema = tree.build_schema()

    assert len(schema.schema) == 1
    assert schema({'param_simpe_1': [42]}) == {'param_simpe_1': [42]}
    assert schema({}) == {'param_simpe_1': []}
    with raises(MultipleInvalid):
        assert schema({'param_simpe_1': ['teve']})
コード例 #8
0
def test_optional_simple_param_with_default_value(default, other):

    tree = Tree()

    @tree.root()
    class Config():

        param_simpe_1 = default

    schema = tree.build_schema()

    assert len(schema.schema) == 1
    assert schema({}) == {'param_simpe_1': default}
    assert schema({'param_simpe_1': other}) == {'param_simpe_1': other}
    with raises(MultipleInvalid):
        assert schema({'param_simpe_2': other})
コード例 #9
0
ファイル: test_custom_items.py プロジェクト: voidpp/configpp
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'
コード例 #10
0
def test_dict_node_optional():
    tree = Tree()

    @tree.root()
    class Config():

        servers = tree.dict_node(str, ExampleServerConfig, {})

    schema = tree.build_schema()
    source_data = {'servers':{'server1': {'host': 'teve', 'port': 42}, 'server2': {'host': 'muha', 'port': 42}}}

    assert len(schema.schema) == 1
    assert schema(source_data) == source_data
    assert schema({}) == {'servers': {}}
    with raises(MultipleInvalid):
        assert schema({'servers': 42})
コード例 #11
0
def test_simple_param_class_instance():

    tree = Tree()

    @tree.root()
    class Config():

        param_node_1 = ExampleServerConfig('teve', 42)

    source_data = {'param_node_1': {'host': 'teve', 'port': 42}}

    schema = tree.build_schema()
    print(schema)

    assert len(schema.schema) == 1
    assert schema({}) == source_data
    assert schema(source_data) == source_data
コード例 #12
0
def test_dict_root():

    tree = Tree()

    @tree.dict_root(str, ExampleServerConfig)
    class Config():
        pass

    schema = tree.build_schema()
    source_data = {'server1': {'host': 'teve', 'port': 42}, 'server2': {'host': 'muha', 'port': 42}}

    assert len(schema.schema) == 1
    assert schema(source_data) == source_data
    with raises(MultipleInvalid):
        assert schema({12: {'host': '', 'port': 42}})
    with raises(MultipleInvalid):
        assert schema({'server': {'host': '', 'port': ''}})
コード例 #13
0
def test_schema_var_annotation_primitive_type():

    tree = Tree()

    @tree.root()
    class Config():

        param_simpe_1: int

    schema = tree.build_schema()

    data = {'param_simpe_1': 42}

    assert schema(data) == data

    with raises(MultipleInvalid):
        assert schema({})
コード例 #14
0
ファイル: test_settings.py プロジェクト: voidpp/configpp
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
コード例 #15
0
def test_optional_and_required():

    tree = Tree()

    @tree.root()
    class Config():

        param1 = 42
        param2 = str

    schema = tree.build_schema()
    with raises(MultipleInvalid):
        assert schema({})
    with raises(MultipleInvalid):
        assert schema({'param1': 84})

    assert schema({'param2': 'teve'}) == {'param2': 'teve', 'param1': 42}
コード例 #16
0
def test_required_simple_param(type, value):

    tree = Tree()

    @tree.root()
    class Config():

        param_simpe_1 = type

    schema = tree.build_schema()

    assert len(schema.schema) == 1
    assert schema({'param_simpe_1': value}) == {'param_simpe_1': value}
    with raises(MultipleInvalid):
        assert schema({})
    with raises(MultipleInvalid):
        assert schema({'param_simpe_2': value})
コード例 #17
0
def test_detailed_leaf():

    tree = Tree()

    @tree.root()
    class Config():

        param_simpe_1 = tree.leaf(All(int, Range(min = 0)))

    schema = tree.build_schema()

    assert len(schema.schema) == 1
    assert schema({'param_simpe_1': 7}) == {'param_simpe_1': 7}

    with raises(MultipleInvalid):
        schema({})
    with raises(MultipleInvalid):
        schema({'param_simpe_1': -1})
コード例 #18
0
def test_node_basics_with_defaults_only():

    tree = Tree()

    @tree.root()
    class Config():

        @tree.node()
        class node1():

            @tree.node()
            class node2():
                param1 = 42
                param2 = ''

    schema = tree.build_schema()

    assert schema({}) == {'node1': {'node2': {'param1': 42, 'param2': ''}}}
コード例 #19
0
def test_list_param_required():
    tree = Tree()

    @tree.root()
    class Config():

        param_simpe_1 = [int]

    schema = tree.build_schema()

    assert len(schema.schema) == 1
    assert schema({'param_simpe_1': [42]}) == {'param_simpe_1': [42]}
    assert schema({'param_simpe_1': []}) == {'param_simpe_1': []}
    with raises(MultipleInvalid):
        assert schema({'param_simpe_1': 42})
    with raises(MultipleInvalid):
        assert schema({})
    with raises(MultipleInvalid):
        assert schema({'param_simpe_1': ['teve']})
コード例 #20
0
def test_schema_var_annotation_dict():

    StringDict = Dict[str, int]

    tree = Tree()

    @tree.root()
    class Config():

        param_simpe_1: StringDict

    schema = tree.build_schema()

    data = {'param_simpe_1': {'teve': 42}}

    assert schema(data) == data

    with raises(MultipleInvalid):
        assert schema({})
    with raises(MultipleInvalid):
        assert schema({'param_simpe_1': {'k1': 'v1'}})
コード例 #21
0
ファイル: test_custom_items.py プロジェクト: voidpp/configpp
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'
コード例 #22
0
def test_node_external_class():

    tree = Tree()

    class ServerConfig():
        host = str
        port = int

    @tree.root()
    class Config():

        param_node_1 = tree.node()(ServerConfig)

    source_data = {'param_node_1': {'host': 'teve', 'port': 42}}

    schema = tree.build_schema()

    assert len(schema.schema) == 1
    with raises(MultipleInvalid):
        assert schema({'param_node_1': 42})
    assert schema(source_data) == source_data
コード例 #23
0
def test_node_basics():

    tree = Tree()

    @tree.root()
    class Config():

        @tree.node()
        class param_node_1():
            host = str
            port = int

    source_data = {'param_node_1': {'host': 'teve', 'port': 42}}

    schema = tree.build_schema()

    assert len(schema.schema) == 1
    with raises(MultipleInvalid):
        assert schema({})
    with raises(MultipleInvalid):
        assert schema({'param_node_1': 42})
    assert schema(source_data) == source_data
コード例 #24
0
ファイル: test_custom_items.py プロジェクト: voidpp/configpp
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
コード例 #25
0
def test_schema_var_annotation_list():

    StringList = List[str]

    tree = Tree()

    @tree.root()
    class Config():

        param_simpe_1: StringList

    schema = tree.build_schema()

    data = {'param_simpe_1': ['teve']}

    assert schema(data) == data

    with raises(MultipleInvalid):
        schema({})
    with raises(MultipleInvalid):
        schema({'param_simpe_1': 42})
    with raises(MultipleInvalid):
        schema({'param_simpe_1': [42]})
コード例 #26
0
def test_list_of_objects():

    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.list_node([ServerConfig])

    source_data = {'servers':[{'url': 'http://teve.hu', 'credentials': {'client_id': 'id1', 'secret': '42'}}]}
    source_data2 = deepcopy(source_data)
    source_data2['servers'][0]['credentials']['client_id'] = 42
    source_data3 = deepcopy(source_data)
    del source_data3['servers'][0]['credentials']['secret']

    schema = tree.build_schema()
    assert len(schema.schema) == 1
    assert schema(source_data) == source_data
    assert schema({'servers': []}) == {'servers': []}
    with raises(MultipleInvalid):
        assert schema(source_data2)
    with raises(MultipleInvalid):
        assert schema(source_data3)
コード例 #27
0
def test_missing_root():
    tree = Tree()

    with raises(ConfigTreeBuilderException):
        tree.build_schema()