Example #1
0
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
Example #2
0
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_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
Example #4
0
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'
Example #5
0
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'
Example #6
0
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_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']}
def test_list_root():

    tree = Tree()

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

    schema = tree.build_schema()
    assert schema([1])
Example #9
0
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
Example #10
0
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
Example #11
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]})
Example #12
0
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}
Example #13
0
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
Example #14
0
def test_simple_param_default():

    tree = Tree()

    @tree.root()
    class Config():

        param_simpe_1 = 42

    cfg = tree.load({})

    assert cfg.param_simpe_1 == 42
Example #15
0
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
Example #16
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
Example #17
0
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
Example #18
0
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'})
Example #19
0
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}
Example #20
0
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'
Example #21
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']})
Example #22
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})
Example #23
0
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
Example #24
0
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}
Example #25
0
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'
Example #26
0
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
Example #27
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})
Example #28
0
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
Example #29
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({})
Example #30
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}