Ejemplo n.º 1
0
def test_get_dict_different_types():  # type: ignore
    cfg = ConfigurationSet(
        config_from_dict(DICT3_1, lowercase_keys=True),
        config_from_dict(DICT3_2, lowercase_keys=True),  # a2 is ignored here
        config_from_dict(DICT3_3, lowercase_keys=True),
    )

    assert cfg.get_dict("a2") == {
        "b2.c1": 10,
        "b2.c2": "YWJjZGVmZ2g=",
        "b2.c3": "abcdefgh",
        "g2": 10,
        "w2": 123,
        "w3": "abc",
    }
    assert cfg.a2.as_dict() == {
        "b2.c1": 10,
        "b2.c2": "YWJjZGVmZ2g=",
        "b2.c3": "abcdefgh",
        "g2": 10,
        "w2": 123,
        "w3": "abc",
    }
    assert dict(cfg.a2) == {
        "b2.c1": 10,
        "b2.c2": "YWJjZGVmZ2g=",
        "b2.c3": "abcdefgh",
        "g2": 10,
        "w2": 123,
        "w3": "abc",
    }

    with pytest.raises(TypeError):  # the first configuration overrides the type
        assert cfg.get_dict("z1") is Exception
    assert cfg.z1 == 100
def test_get_dict_different_types():  # type: ignore
    cfg = ConfigurationSet(
        config_from_dict(DICT3_1),
        config_from_dict(DICT3_2),  # a2 is ignored here
        config_from_dict(DICT3_3),
    )

    assert cfg.get_dict("a2") == {
        'b2.c1': 10,
        'b2.c2': 'YWJjZGVmZ2g=',
        'b2.c3': 'abcdefgh',
        'g2': 10,
        'w2': 123,
        'w3': 'abc'
    }
    assert cfg.a2.as_dict() == {
        'b2.c1': 10,
        'b2.c2': 'YWJjZGVmZ2g=',
        'b2.c3': 'abcdefgh',
        'g2': 10,
        'w2': 123,
        'w3': 'abc'
    }
    assert dict(cfg.a2) == {
        'b2.c1': 10,
        'b2.c2': 'YWJjZGVmZ2g=',
        'b2.c3': 'abcdefgh',
        'g2': 10,
        'w2': 123,
        'w3': 'abc'
    }

    with raises(TypeError):  # the first configuration overrides the type
        assert cfg.get_dict("z1") is Exception
    assert cfg.z1 == 100
def test_get_dict():  # type: ignore
    cfg = ConfigurationSet(config_from_dict(DICT2_1),
                           config_from_dict(DICT2_2),
                           config_from_env(prefix=PREFIX))

    assert cfg.get_dict("a2") == {
        'b1.c1': 'f',
        'b1.c2': False,
        'b1.c3': None,
        'b2.c1': 10,
        'b2.c2': 'YWJjZGVmZ2g=',
        'b2.c3': 'abcdefgh'
    }
    assert cfg.a2.as_dict() == {
        'b1.c1': 'f',
        'b1.c2': False,
        'b1.c3': None,
        'b2.c1': 10,
        'b2.c2': 'YWJjZGVmZ2g=',
        'b2.c3': 'abcdefgh'
    }
    assert dict(cfg.a2) == {
        'b1.c1': 'f',
        'b1.c2': False,
        'b1.c3': None,
        'b2.c1': 10,
        'b2.c2': 'YWJjZGVmZ2g=',
        'b2.c3': 'abcdefgh'
    }
    with raises(KeyError):
        assert cfg.get_dict("a3") is Exception

    assert set(
        cfg.a2.values()) == {'f', False, None, 10, 'YWJjZGVmZ2g=', 'abcdefgh'}
    assert dict(cfg.a2) == dict(cfg.a2.items())
def test_get_dict():  # type: ignore
    cfg = ConfigurationSet(
        config_from_dict(DICT2_1, lowercase_keys=True),
        config_from_dict(DICT2_2, lowercase_keys=True),
        config_from_env(prefix=PREFIX, lowercase_keys=True),
    )

    a2 = {
        "b2.c1": 10,
        "b1.c1": "f",
        "b1.c2": False,
        "b1.c3": None,
        "b2.c1": 10,
        "b2.c2": "YWJjZGVmZ2g=",
        "b2.c3": "abcdefgh",
    }
    a2nested = {
        "b1": {
            "c1": "f",
            "c2": False,
            "c3": None
        },
        "b2": {
            "c1": 10,
            "c2": "YWJjZGVmZ2g=",
            "c3": "abcdefgh"
        },
    }

    assert cfg.get_dict("a2") == a2
    assert cfg.a2.as_dict() == a2
    assert dict(cfg.a2) == a2nested
    with cfg.dotted_iter():
        assert cfg.get_dict("a2") == a2
        assert cfg.a2.as_dict() == a2
        # note that this still returns he nested dict since the dotted iteration
        # impacts only the parent cfg, not cfg.a
        assert dict(cfg.a2) == a2nested
        # to use dotted iteration for children, we need to explicitly set it
        with cfg.a2.dotted_iter() as cfg_a2:
            assert dict(cfg_a2) == a2

    with pytest.raises(KeyError):
        assert cfg.get_dict("a3") is Exception

    assert dict(cfg.a2) == dict(cfg.a2.items())
def test_get_dict_different_types():  # type: ignore
    cfg = ConfigurationSet(
        config_from_dict(DICT3_1, lowercase_keys=True),
        config_from_dict(DICT3_2, lowercase_keys=True),  # a2 is ignored here
        config_from_dict(DICT3_3, lowercase_keys=True),
    )

    a2 = {
        "b2.c1": 10,
        "b2.c2": "YWJjZGVmZ2g=",
        "b2.c3": "abcdefgh",
        "g2": 10,
        "w2": 123,
        "w3": "abc",
    }
    a2nested = {
        "b2": {
            "c1": 10,
            "c2": "YWJjZGVmZ2g=",
            "c3": "abcdefgh"
        },
        "g2": 10,
        "w2": 123,
        "w3": "abc",
    }

    assert cfg.get_dict("a2") == a2
    assert cfg.a2.as_dict() == a2
    assert dict(cfg.a2) == a2nested

    with cfg.dotted_iter():
        assert cfg.get_dict("a2") == a2
        assert cfg.a2.as_dict() == a2
        # note that this still returns he nested dict since the dotted iteration
        # impacts only the parent cfg, not cfg.a
        assert dict(cfg.a2) == a2nested
        # to use dotted iteration for children, we need to explicitly set it
        with cfg.a2.dotted_iter() as cfg_a2:
            assert dict(cfg_a2) == a2

    with pytest.raises(
            TypeError):  # the first configuration overrides the type
        assert cfg.get_dict("z1") is Exception
    assert cfg.z1 == 100
def test_same_as_configuration():  # type: ignore
    cfg = config_from_dict(DICT2_1, lowercase_keys=True)

    cfgset = ConfigurationSet(config_from_dict(DICT2_1, lowercase_keys=True))

    assert cfg.get_dict("a2") == cfgset.get_dict("a2")
    assert cfg.a2.as_dict() == cfgset.a2.as_dict()
    assert dict(cfg.a2) == dict(cfgset.a2)

    assert dict(cfg.a2) == dict(cfg.a2.items())
    assert dict(cfgset.a2) == dict(cfgset.a2.items())

    assert cfg.as_dict() == cfgset.as_dict()
    assert dict(cfg) == dict(cfgset)
Ejemplo n.º 7
0
def test_get_dict():  # type: ignore
    cfg = ConfigurationSet(
        config_from_dict(DICT2_1, lowercase_keys=True),
        config_from_dict(DICT2_2, lowercase_keys=True),
        config_from_env(prefix=PREFIX, lowercase_keys=True),
    )

    assert cfg.get_dict("a2") == {
        "b1.c1": "f",
        "b1.c2": False,
        "b1.c3": None,
        "b2.c1": 10,
        "b2.c2": "YWJjZGVmZ2g=",
        "b2.c3": "abcdefgh",
    }
    assert cfg.a2.as_dict() == {
        "b1.c1": "f",
        "b1.c2": False,
        "b1.c3": None,
        "b2.c1": 10,
        "b2.c2": "YWJjZGVmZ2g=",
        "b2.c3": "abcdefgh",
    }
    assert dict(cfg.a2) == {
        "b1.c1": "f",
        "b1.c2": False,
        "b1.c3": None,
        "b2.c1": 10,
        "b2.c2": "YWJjZGVmZ2g=",
        "b2.c3": "abcdefgh",
    }
    with raises(KeyError):
        assert cfg.get_dict("a3") is Exception

    assert set(
        cfg.a2.values()) == {"f", False, None, 10, "YWJjZGVmZ2g=", "abcdefgh"}
    assert dict(cfg.a2) == dict(cfg.a2.items())