Skip to content

test_flatten_key_dict_view

Testing of the FlattenKeyDictView class

Functions:

fd

fd(test_dict)

Test dictionary converted to a flatten form

Source code in tests/test_flatten_key_dict_view.py
14
15
16
17
@pytest.fixture
def fd(test_dict):
    """Test dictionary converted to a flatten form"""
    return FlattenKeyDictView(test_dict)

fd_sep_at

fd_sep_at(test_dict)

Test dictionary converted to a flatten form with custom flat key separator @

Source code in tests/test_flatten_key_dict_view.py
20
21
22
23
@pytest.fixture
def fd_sep_at(test_dict):
    """Test dictionary converted to a flatten form with custom flat key separator @"""
    return FlattenKeyDictView(test_dict, flat_key_separator="@")

test_contains

test_contains(fd)

Test the in operator

Source code in tests/test_flatten_key_dict_view.py
26
27
28
29
30
31
32
33
34
def test_contains(fd):
    """Test the `in` operator"""
    assert "a1" in fd
    assert "b" in fd
    assert "b.k1" in fd
    assert "b.k2" in fd
    assert "c" in fd
    assert "c.k1" in fd
    assert "c.k1.k11" in fd

test_custom_key_separator

test_custom_key_separator(fd_sep_at)

Test using @ as the custom key separator

Source code in tests/test_flatten_key_dict_view.py
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
def test_custom_key_separator(fd_sep_at):
    """Test using @ as the custom key separator"""
    # Test contains
    assert "b@k1" in fd_sep_at
    assert "b@k2" in fd_sep_at
    assert "c@k1" in fd_sep_at
    assert "c@k1@k11" in fd_sep_at
    # Test getitem
    assert fd_sep_at["b@k1"] == 3
    assert fd_sep_at["b@k2"] == 4
    assert fd_sep_at["c@k1"] == {"k11": True}
    assert fd_sep_at["c@k1@k11"]
    # Test setitem
    fd_sep_at["b@k1"] = 34
    assert fd_sep_at["b@k1"] == 34
    assert fd_sep_at["b"] == {"k1": 34, "k2": 4}
    fd_sep_at["d@k1"] = 44
    assert fd_sep_at["d"] == {"k1": 44}
    assert fd_sep_at["d@k1"] == 44
    # Test delitem
    del fd_sep_at["a1"]
    assert "a1" not in fd_sep_at
    del fd_sep_at["b@k1"]
    assert fd_sep_at["b"] == {"k2": 4}
    del fd_sep_at["c@k1@k11"]
    assert fd_sep_at["c@k1"] == {}

test_delitem

test_delitem(fd)

Test removing keys from the dict

Source code in tests/test_flatten_key_dict_view.py
63
64
65
66
67
68
69
70
def test_delitem(fd):
    """Test removing keys from the dict"""
    del fd["a1"]
    assert "a1" not in fd
    del fd["b.k1"]
    assert fd["b"] == {"k2": 4}
    del fd["c.k1.k11"]
    assert fd["c.k1"] == {}

test_dict

test_dict()

Default simple nested dictionary

Source code in tests/test_flatten_key_dict_view.py
 8
 9
10
11
@pytest.fixture
def test_dict():
    """Default simple nested dictionary"""
    return {"a1": 1, "b": {"k1": 3, "k2": 4}, "c": {"k1": {"k11": True}}}

test_getitem

test_getitem(fd)

Test accessing dict keys

Source code in tests/test_flatten_key_dict_view.py
37
38
39
40
41
42
43
44
45
def test_getitem(fd):
    """Test accessing dict keys"""
    assert fd["a1"] == 1
    assert fd["b"] == {"k1": 3, "k2": 4}
    assert fd["b.k1"] == 3
    assert fd["b.k2"] == 4
    assert fd["c"] == {"k1": {"k11": True}}
    assert fd["c.k1"] == {"k11": True}
    assert fd["c.k1.k11"]

test_keys

test_keys(fd)

Test returning of flatten keys

Source code in tests/test_flatten_key_dict_view.py
101
102
103
104
def test_keys(fd):
    """Test returning of flatten keys"""
    assert fd.ordered_keys() == ["a1", "b", "b.k1", "b.k2", "c", "c.k1", "c.k1.k11"]
    assert fd.ordered_leaf_keys() == ["a1", "b.k1", "b.k2", "c.k1.k11"]

test_raise_exception

test_raise_exception(fd)

Test corner cases with invalid or non-existing sub-keys

Source code in tests/test_flatten_key_dict_view.py
107
108
109
110
111
112
113
114
def test_raise_exception(fd):
    """Test corner cases with invalid or non-existing sub-keys"""
    with pytest.raises(KeyError, match="Parent .* is not a dictionary"):
        # b.k1 is a value, it can't contain k2!
        fd["b.k1.k2"]
    with pytest.raises(KeyError, match="Parent .* doesn't exist"):
        # e doesn't exist at all
        fd["e.k1"]

test_setitem

test_setitem(fd)

Test changing values of dict keys

Source code in tests/test_flatten_key_dict_view.py
48
49
50
51
52
53
54
55
56
57
58
59
60
def test_setitem(fd):
    """Test changing values of dict keys"""
    fd["a1"] = 43
    assert fd["a1"] == 43
    fd["b.k1"] = 34
    assert fd["b.k1"] == 34
    assert fd["b"] == {"k1": 34, "k2": 4}
    fd["d.k1"] = 44
    assert fd["d"] == {"k1": 44}
    assert fd["d.k1"] == 44
    # Add a non-exisiting long flat key with a value
    fd["e.k1.k11"] = 3.14
    assert fd["e.k1.k11"] == 3.14