Ejemplo n.º 1
0
def test_to_dict_simple():
    a = [{"a": 1, PATH_KEY: "x"}]
    loader = Dicts.from_dicts(a)
    out = loader.map_by(key="a", default="_")
    assert isinstance(out, dict), "to_dict returns a dict"
    assert len(out) == 1
    assert out[a[0]["a"]] == a[0]
Ejemplo n.º 2
0
def test_to_dict_transform():
    a = [{"a": "aa", "b": "bb", PATH_KEY: "x"}]
    loader = Dicts.from_dicts(a)
    pick_b = lambda d: {"b": d["b"]}
    out = loader.items_as(pick_b).map_by(key="b", default="_")
    assert isinstance(out, dict), "to_dict returns a dict"
    assert len(out) == 1
    assert out == {"bb": {"b": "bb"}}
Ejemplo n.º 3
0
def test_load_dicts(generator, num_files, expected_size, extensions):
    tmp_dir = create_dir(num_files, generator, extensions)
    loader = Dicts.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()
Ejemplo n.º 4
0
def test_filter():
    loader = Dicts.from_dicts([
        {
            "animal": "cat",
            "size": 100
        },
        {
            "animal": "dog",
            "size": 100
        },
        {
            "animal": "rat",
            "size": 1
        },
    ])

    animal = loader.filter(lambda d: d["animal"] == "rat").map_by(
        "animal", "_")
    assert animal == {"rat": {"animal": "rat", "size": 1}}
Ejemplo n.º 5
0
def test_cast_then_key():

    data = [
        {
            "animal": "cat",
            "size": "100"
        },
        {
            "animal": "dog",
            "size": "100"
        },
        {
            "animal": "rat",
            "size": "1"
        },
        {
            "animal": "bat",
            "size": "5",
            "disabled": True
        },
    ]

    def animal_size(animal: Animal):
        return animal.size

    result = Dicts.from_dicts(data).items_as(Animal).key_by(animal_size, "_")
    assert result["1"] == [Animal({"animal": "rat", "size": "1"})]
    assert result["100"] == [
        Animal({
            "animal": "cat",
            "size": "100"
        }),
        Animal({
            "animal": "dog",
            "size": "100"
        }),
    ]
Ejemplo n.º 6
0
def test_group_by_file():
    a = [
        {
            "name": 1,
            PATH_KEY: "x"
        },
        {
            "name": 1,
            PATH_KEY: "y"
        },
        {
            "name": 2,
            PATH_KEY: "y"
        },
        {
            "name": 2
        },
    ]
    loader = Dicts.from_dicts(a)
    out = dict(loader.key_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
        }],
    }
Ejemplo n.º 7
0
def test_duplicate():
    a = [{"name": 1, PATH_KEY: "x"}, {"name": 1, PATH_KEY: "y"}]
    loader = Dicts.from_dicts(a)
    with pytest.raises(ValueError):
        loader.map_by(key="name", default="_")
Ejemplo n.º 8
0
def test_group_by_unordered():
    a = [{PATH_KEY: "a"}, {PATH_KEY: "c"}, {PATH_KEY: "b"}, {PATH_KEY: "a"}]
    loader = Dicts.from_dicts(a)
    out = loader.key_by_file()
    assert len(out["a"]) == 2
Ejemplo n.º 9
0
def load_connection_settings():
    """ Retrieve connections from specified yaml."""
    directory = Path(os.environ.get(_ENV_VAR_FOR_FILE, _DEFAULT_DIRECTORY))
    loader = Dicts.from_path(directory)
    return loader