def test_to_dict_simple(): a = [{"a": 1, PATH_KEY: "x"}] loader = DictLoader.from_dicts(a) out = loader.group_by_key(key="a") assert isinstance(out, dict), "to_dict returns a dict" assert len(out) == 1 assert out[a[0]["a"]] == a[0]
def test_duplicate_skip_errors(): a = [{"name": 1, PATH_KEY: "x"}, {"name": 1, PATH_KEY: "y"}] try: loader = DictLoader.from_dicts(a) loader.group_by_key(allow_duplicates=True) except KeyError: pytest.fail("Exception raised despite skip_errors set")
def test_to_dict_transform(): a = [{"a": "aa", "b": "bb", PATH_KEY: "x"}] loader = DictLoader.from_dicts(a) transformator = lambda d: d["b"] out = loader.group_by_key(key="a", transformator=transformator) assert isinstance(out, dict), "to_dict returns a dict" assert len(out) == 1 assert out[a[0]["a"]] == a[0]["b"]
def test_load_dicts(generator, num_files, expected_size, extensions): tmp_dir = create_dir(num_files, generator, extensions) loader = DictLoader.from_path(tmp_dir, skip_errors=True) dicts = list(loader.items) assert len(dicts) == expected_size, "number of loaded files matches" for d in dicts: assert isinstance(d, dict) assert d, "loaded dict is not empty" assert "a" in d, "dict loaded correctly" assert d["a"] < num_files for f in tmp_dir.iterdir(): f.unlink() tmp_dir.rmdir()
def test_group_by_file(): a = [ {"name": 1, PATH_KEY: "x"}, {"name": 1, PATH_KEY: "y"}, {"name": 2, PATH_KEY: "y"}, {"name": 2}, ] loader = DictLoader.from_dicts(a) out = dict(loader.group_by_file()) assert out == { "x": [{"name": 1, PATH_KEY: "x"}], "y": [{"name": 1, PATH_KEY: "y"}, {"name": 2, PATH_KEY: "y"}], DEFAULT_PATH_KEY: [{"name": 2}], }
def load_connection_settings(): """ Retrieve connections from specified yaml.""" directory = Path(os.environ.get(_ENV_VAR_FOR_FILE, _DEFAULT_DIRECTORY)) loader = DictLoader.from_path(directory) return loader
def load_connection_settings(directory): """ Retrieve connections from specified yaml.""" loader = DictLoader.from_path(directory) return list(loader.directory())
def test_group_by_unordered(): a = [{PATH_KEY: "a"}, {PATH_KEY: "c"}, {PATH_KEY: "b"}, {PATH_KEY: "a"}] loader = DictLoader.from_dicts(a) out = loader.group_by_file() assert len(out["a"]) == 2
def test_duplicate(): a = [{"name": 1, PATH_KEY: "x"}, {"name": 1, PATH_KEY: "y"}] loader = DictLoader.from_dicts(a) with pytest.raises(KeyError): loader.group_by_key()