Beispiel #1
0
    def test_factories(self) -> None:
        """Test all implemented factory equivalents."""

        # write all test data to local files
        for ftype, data in FACTORIES.items():
            with open(f'{TMPDIR}/{ftype}.{ftype}', mode='w') as output:
                output.write(data)

        # test both file-like object and file path modes of construction
        assert (Namespace(TEST_DICT) ==
                Namespace.from_toml(StringIO(TEST_TOML)) == Namespace.from_toml(f'{TMPDIR}/toml.toml') ==
                Namespace.from_yaml(StringIO(TEST_YAML)) == Namespace.from_yaml(f'{TMPDIR}/yaml.yaml') ==
                Namespace.from_json(StringIO(TEST_JSON)) == Namespace.from_json(f'{TMPDIR}/json.json'))
Beispiel #2
0
    def test_from_local(self) -> None:
        """Test Configuration.from_local factory method."""

        # initial local files
        for label, data in CONFIG_SOURCES.items():
            with open(f'{TMPDIR}/{label}.toml', mode='w') as output:
                output.write(data)

        # clean environment of any existing variables with the item
        prefix = 'CMDKIT'
        for var in dict(os.environ):
            if var.startswith(prefix):
                os.environ.pop(var)

        # populate environment with test variables
        for line in CONFIG_ENVIRON.strip().split('\n'):
            field, value = line.strip().split('=')
            os.environ[field] = value

        # build configuration
        default = Namespace.from_toml(StringIO(CONFIG_DEFAULT))
        cfg = Configuration.from_local(default=default, env=True, prefix=prefix,
                                       system=f'{TMPDIR}/system.toml',
                                       user=f'{TMPDIR}/user.toml',
                                       local=f'{TMPDIR}/local.toml')

        # verify namespace isolation
        assert cfg.namespaces['default'] == Namespace.from_toml(StringIO(CONFIG_DEFAULT))
        assert cfg.namespaces['system'] == Namespace.from_toml(StringIO(CONFIG_SYSTEM))
        assert cfg.namespaces['user'] == Namespace.from_toml(StringIO(CONFIG_USER))
        assert cfg.namespaces['local'] == Namespace.from_toml(StringIO(CONFIG_LOCAL))
        assert cfg.namespaces['env'] == Environ(prefix).reduce()

        # verify parameter lineage
        assert cfg['a']['var0'] == 'default_var0' and cfg.which('a', 'var0') == 'default'
        assert cfg['a']['var1'] == 'system_var1' and cfg.which('a', 'var1') == 'system'
        assert cfg['a']['var2'] == 'user_var2' and cfg.which('a', 'var2') == 'user'
        assert cfg['b']['var3'] == 'local_var3' and cfg.which('b', 'var3') == 'local'
        assert cfg['c']['var4'] == 'env_var4' and cfg.which('c', 'var4') == 'env'
        assert cfg['c']['var5'] == 'env_var5' and cfg.which('c', 'var5') == 'env'
Beispiel #3
0
    def test_init(self) -> None:
        """Test initialization."""

        # test namespaces
        ns_a = Namespace(TEST_DICT)
        ns_b = Namespace.from_toml(StringIO(TEST_TOML))
        ns_c = Namespace.from_yaml(StringIO(TEST_YAML))
        ns_d = Namespace.from_json(StringIO(TEST_JSON))

        # initialize construction results in member namespaces
        cfg = Configuration(A=ns_a, B=ns_b)
        assert dict(cfg) == cfg.namespaces['A'] == ns_a == cfg.namespaces['B'] == ns_b

        # extend the configuration
        cfg.extend(C=ns_c, D=ns_d)
        assert (cfg.namespaces['A'] == ns_a ==
                cfg.namespaces['B'] == ns_b ==
                cfg.namespaces['C'] == ns_c ==
                cfg.namespaces['D'] == ns_d ==
                dict(cfg))

        # keys() and values() are available
        assert list(cfg.keys()) == list(ns_a.keys())
        assert list(cfg.values()) == list(ns_a.values())