예제 #1
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()
예제 #2
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()
예제 #3
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()
예제 #4
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()
예제 #5
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"]
예제 #6
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()
예제 #7
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()
예제 #8
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()
예제 #9
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"]