Пример #1
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"]
Пример #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_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()
Пример #4
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()
Пример #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"]