예제 #1
0
def test_real_duty2():
    initial_configs_dir = os.path.join(
        os.path.dirname(__file__), "files", "configs"
    )
    with tempfile.TemporaryDirectory(
        dir=os.path.join(os.path.dirname(__file__), "files")
    ) as temp:
        make_configs(temp, initial_configs_dir)

        with open(os.path.join(temp, "master.json")) as mf, open(
            os.path.join(temp, "big1.json")
        ) as b1f, open(os.path.join(temp, "big2.json")) as b2f, open(
            os.path.join(initial_configs_dir, "big1.json")
        ) as ib1, open(
            os.path.join(initial_configs_dir, "big2.json")
        ) as ib2:
            mc = json.loads(mf.read())
            b1 = json.loads(b1f.read())
            b2 = json.loads(b2f.read())

            ib1 = json.loads(ib1.read())
            ib2 = json.loads(ib2.read())

        restored1 = Splitter(mc) + Splitter(b1)
        restored1 = restored1.as_dict()

        restored2 = Splitter(mc) + Splitter(b2)
        restored2 = restored2.as_dict()

        assert all([ib1[k] == restored1[k] for k in restored1.keys()])
        assert all([ib2[k] == restored2[k] for k in restored2.keys()])
예제 #2
0
def test_nested_add():
    with open(
        os.path.join(os.path.dirname(__file__), "files/conf1.json"), "r"
    ) as f1:
        d1 = json.loads(f1.read())

    dd1 = Splitter(d1)
    dd2 = Splitter(
        dict_={
            "deployment": {
                "files": {"example-resource-file1": {"new_key": "new_value"}}
            },
            "x": "y",
        }
    )

    result = dd1 + dd2
    assert ("x",) in result.keys()
    assert "y" in result.values()
    assert (
        "deployment",
        "files",
        "example-resource-file1",
        "sourceUrl",
    ) in result.keys()
    assert (
        "deployment",
        "files",
        "example-resource-file1",
        "new_key",
    ) in result.keys()
    assert "new_value" in result.values()
예제 #3
0
def test_simple_add():
    d1 = Splitter(dict_={"a": "b"})
    d2 = Splitter(dict_={"c": "d"})

    result = d1 + d2

    assert ("a",) in result.keys()
    assert ("c",) in result.keys()
    assert "b" in result.values()
    assert "d" in result.values()
예제 #4
0
def test_values():
    cm = Splitter(example_dict)
    assert isinstance(cm.values(), list)
    assert cm.values()[0] == "value"

    cm2 = Splitter(example_nested_dict)
    assert cm2.values() == ["value", "nested_value1", 1, "world", 0.2, False]

    cm3 = Splitter(example_nested_dict_with_list_values)
    assert cm3.values() == ["value", "simple_value", 0, 1, 2, 3, 4, 5, "s"]
예제 #5
0
def make_configs(target_dir: str, scan_dir: str):
    master_conf = Splitter(dict_={})
    for file in os.listdir(scan_dir):
        path = os.path.join(scan_dir, file)
        if os.path.isdir(path):
            continue
        ext = Path(path).suffix
        with open(path, "r") as f:
            if ext == ".json":
                conf = Splitter(dict_=json.loads(f.read()))

        if not master_conf:
            master_conf = conf
        else:
            master_conf = master_conf ^ conf

    # save master config
    with open(os.path.join(target_dir, "master.json"), "w") as mf:

        mf.write(json.dumps(master_conf.as_dict()))

    for file in os.listdir(scan_dir):
        path = os.path.join(scan_dir, file)
        if os.path.isdir(path):
            continue
        ext = Path(path).suffix

        with open(path, "r") as f:
            if ext == ".json":
                conf = Splitter(dict_=json.loads(f.read()))

        conf = conf - master_conf
        with open(os.path.join(target_dir, file), "w") as cf:
            cf.write(json.dumps(conf.as_dict()))
예제 #6
0
def test_list_add():
    with open(
        os.path.join(os.path.dirname(__file__), "files/conf1.json"), "r"
    ) as f1:
        d1 = json.loads(f1.read())

    dd1 = Splitter(d1)
    dd2 = Splitter(
        dict_={"x": "y", "handlers": [None, {"urlRegex": "//[.*]"}]}
    )

    result = dd1 + dd2

    assert ("x",) in result.keys()
    assert "y" in result.values()
    assert ("handlers", "*0", "urlRegex") in result.keys()
    assert ("handlers", "*1", "urlRegex") in result.keys()
    assert "/.*" in result.values()
    assert "//[.*]" in result.values()

    assert result["handlers.*0.urlRegex"] == "/.*"
    assert result["handlers.*1.urlRegex"] == "//[.*]"
예제 #7
0
def test_iter_list():
    cm = Splitter(example_nested_dict_with_list_values)
    assert ("key", ) in cm.keys()
    assert ("nested_key", "list_key", "*0") in cm.keys()
    assert ("nested_key", "list_key", "*1") in cm.keys()
    assert ("nested_key", "list_key", "*6", "ps") in cm.keys()

    assert "s" in cm.values()
예제 #8
0
def test_getitem():
    cm = Splitter(example_nested_dict_with_list_values)

    assert cm["key"] == "value"
    assert cm["nested_key.simple_key"] == "simple_value"
    assert cm[("nested_key", "simple_key")] == "simple_value"
    assert cm["nested_key.list_key.*2"] == 2
    assert cm[("nested_key", "list_key", "*2")] == 2

    with pytest.raises(KeyError):
        a = cm["nested_key"]

    with pytest.raises(KeyError):
        a = cm[("keyyyy", )]
예제 #9
0
def test_nested_setitem():
    cm = Splitter(dict_={})
    cm[("a", "b", "c")] = "d"

    assert ("a", "b", "c") in cm.keys()
    assert "d" in cm.values()

    cm[("a", "b", "c")] = "e"
    assert "d" not in cm.values()
    assert "e" in cm.values()

    cm[("a", "b", "x")] = "y"
    assert ("a", "b", "c") in cm.keys()
    assert ("a", "b", "x") in cm.keys()
    assert "e" in cm.values()
    assert "y" in cm.values()
예제 #10
0
def test_delitem_nested():
    cm = Splitter(example_nested_dict)

    del cm[("key2", "nested_key1")]

    assert "key2" in cm.underlying.keys()
    assert ("key2", "nested_key1") not in cm.keys()
    assert ("key2", "nested_key2") in cm.keys()

    del cm["key2.another_dict_key.hello"]
    assert ("key2", "nested_key2") in cm.keys()
    assert ("key2", "another_dict_key", "bool") in cm.keys()
    assert ("key2", "another_dict_key", "hello") not in cm.keys()
예제 #11
0
def test_subtract():
    with open(
        os.path.join(os.path.dirname(__file__), "files/conf1.json"), "r"
    ) as f1, open(
        os.path.join(os.path.dirname(__file__), "files/conf2.json"), "r"
    ) as f2:
        d1 = json.loads(f1.read())
        d2 = json.loads(f2.read())

    dd1 = Splitter(d1)
    dd2 = Splitter(d2)

    result = dd1 - dd2
    assert ("threadsafe",) in result.keys()
    assert (
        "deployment",
        "files",
        "example-resource-file1",
        "sourceUrl",
    ) in result.keys()

    assert ("id",) not in result.keys()
    assert ("runtime",) not in result.keys()
    assert ("handlers", "*0", "urlRegex") not in result.keys()
    assert (
        "deployment",
        "files",
        "example-resource-file1",
        "protocol",
    ) not in result.keys()
    assert (
        "deployment",
        "files",
        "images/example-resource-file2",
        "protocol",
    ) not in result.keys()
예제 #12
0
def test_list_setitem():
    cm = Splitter(example_dict)
    cm["list_key"] = [0]

    assert ("list_key", "*0") in cm.keys()
    assert 0 in cm.values()

    cm["list_key.*1"] = 1

    assert ("list_key", "*1") in cm.keys()
    assert 1 in cm.values()

    assert cm.underlying["list_key"] == [0, 1]

    cm["one.two.three.*0"] = "a"
    cm["one.two.three.*1"] = "b"
    cm["one.two.three.*2"] = "c"

    assert all(["a" in cm.values(), "b" in cm.values(), "c" in cm.values()])
    assert cm["one.two.three.*2"] == "c"
    assert cm.underlying["one"]["two"]["three"] == ["a", "b", "c"]
예제 #13
0
def test_setitem():
    cm = Splitter(dict_={})
    cm["test"] = "test_value"

    assert ("test", ) in cm.keys()
    assert "test_value" in cm.values()

    cm["test2"] = {"a": "b"}
    assert ("test2", "a") in cm.keys()
    assert "b" in cm.values()

    with pytest.raises(KeyError):
        cm["some_key"]["abc"] = 256

    cm["some_key"] = {}
    cm["some_key"]["abc"] = 256
    assert ("some_key", "abc") in cm.keys()
    assert 256 in cm.values()
예제 #14
0
def test_iter_nested():
    cm = Splitter(example_nested_dict)
    assert ("key", ) in cm.keys()
    assert ("key2", "nested_key1") in cm.keys()
    assert ("key2", "another_dict_key", "float") in cm.keys()
    assert ("key2", ) not in cm.keys()
예제 #15
0
def test_simple_intersection():
    with open(
        os.path.join(os.path.dirname(__file__), "files/conf1.json"), "r"
    ) as f1, open(
        os.path.join(os.path.dirname(__file__), "files/conf2.json"), "r"
    ) as f2:
        d1 = json.loads(f1.read())
        d2 = json.loads(f2.read())

    dd1 = Splitter(d1)
    dd2 = Splitter(d2)

    intersection = dd1 ^ dd2

    assert ("id",) in intersection.keys()
    assert ("runtime",) in intersection.keys()
    assert ("handlers", "*0", "urlRegex") in intersection.keys()
    assert (
        "deployment",
        "files",
        "example-resource-file1",
        "protocol",
    ) in intersection.keys()
    assert (
        "deployment",
        "files",
        "images/example-resource-file2",
        "protocol",
    ) in intersection.keys()

    assert ("threadsafe",) not in intersection.keys()
    assert ("use",) not in intersection.keys()
    assert (
        "handlers",
        "*0",
        "script",
        "scriptPath",
    ) not in intersection.keys()
    assert (
        "deployment",
        "files",
        "example-resource-file1",
        "sourceUrl",
    ) not in intersection.keys()
    assert (
        "deployment",
        "files",
        "images/example-resource-file2",
        "sourceUrl",
    ) not in intersection.keys()
    assert (
        "deployment",
        "files",
        "example-resource-file1",
        "compress",
    ) not in intersection.keys()
    assert (
        "deployment",
        "files",
        "images/example-resource-file2",
        "compress",
    ) not in intersection.keys()
예제 #16
0
def test_construct():
    cm = Splitter(example_dict)
    assert example_dict == cm.underlying
    assert cm.kd == "."
    assert cm.ld == "*"
예제 #17
0
def test_delitem():
    cm = Splitter(example_dict)
    del cm["key2"]
    assert ("key2", ) not in cm.keys()
    with pytest.raises(KeyError):
        del cm["abc"]["xyz"]
예제 #18
0
def test_len():
    cm = Splitter({"test": "1", "list": [1, 2, 3]})
    assert len(cm) == 4

    cm = Splitter({"test": "1", "list": [1, 2, 3]}, convert_lists=False)
    assert len(cm) == 2
예제 #19
0
def test_change_delimiters():
    cm = Splitter(example_dict, keys_delimiter="-", list_delimiter="+")
    assert cm.kd == "-"
    assert cm.ld == "+"
예제 #20
0
def test_iter_list_disabled():
    cm = Splitter(example_nested_dict_with_list_values, convert_lists=False)
    assert ("key", ) in cm.keys()
    assert ("nested_key", "list_key") in cm.keys()
    assert ("nested_key", "list_key", "*0") not in cm.keys()
예제 #21
0
def test_iter():
    cm = Splitter(example_dict)
    assert ("key", ) in cm.keys()
    assert ("key2", ) in cm.keys()
    assert ("not_exist_key", ) not in cm.keys()
예제 #22
0
def test_keys():
    cm = Splitter(example_dict)
    assert isinstance(cm.keys(), list)
    assert all([isinstance(k, tuple) for k in cm.keys()])
    assert cm.keys()[0] == ("key", )

    cm2 = Splitter(example_nested_dict)
    assert cm2.keys() == [
        ("key", ),
        ("key2", "nested_key1"),
        ("key2", "nested_key2"),
        ("key2", "another_dict_key", "hello"),
        ("key2", "another_dict_key", "float"),
        ("key2", "another_dict_key", "bool"),
    ]

    cm3 = Splitter(example_nested_dict_with_list_values)
    assert ("nested_key", "list_key", "*1") in cm3.keys()