Skip to content

test_card_file_modify

Functions:

all_datatypes_dict

all_datatypes_dict() -> dict
Source code in tests/test_card_file_modify.py
21
22
23
@pytest.fixture
def all_datatypes_dict() -> dict:
    return _decode_file("all_datatypes.card")

all_datatypes_modified_str

all_datatypes_modified_str() -> str
Source code in tests/test_card_file_modify.py
31
32
33
@pytest.fixture
def all_datatypes_modified_str() -> str:
    return _read_file("all_datatypes.card.modified")

all_datatypes_str

all_datatypes_str() -> str
Source code in tests/test_card_file_modify.py
26
27
28
@pytest.fixture
def all_datatypes_str() -> str:
    return _read_file("all_datatypes.card")

test_append_kv

test_append_kv(three_kv_str, three_kv_dict)

Test appending new key-value pair at the section end

Source code in tests/test_card_file_modify.py
135
136
137
138
def test_append_kv(three_kv_str, three_kv_dict):
    """Test appending new key-value pair at the section end"""
    three_kv_dict["d"] = 42
    assert modifys(three_kv_str, three_kv_dict) == "[null]\na=1\nb=2\nc=3\nd=42"

test_append_kv_header

test_append_kv_header(three_kv_str, three_kv_dict)

Test appending new key-value pair with a header at the section end

Source code in tests/test_card_file_modify.py
141
142
143
144
145
146
147
def test_append_kv_header(three_kv_str, three_kv_dict):
    """Test appending new key-value pair with a header at the section end"""
    three_kv_dict["d"] = 42
    assert (
        modifys(three_kv_str, three_kv_dict, insert_header="New values")
        == "[null]\na=1\nb=2\nc=3\n# New values\nd=42"
    )

test_append_section

test_append_section(
    three_section_kv_str, three_section_kv_dict
)

Test appending new sections

Source code in tests/test_card_file_modify.py
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
def test_append_section(three_section_kv_str, three_section_kv_dict):
    """Test appending new sections"""
    append_with_header = three_section_kv_dict.copy()
    append_with_header["s4"] = {"k1": 33, "k2": 42}
    assert (
        modifys(three_section_kv_str, append_with_header)
        == "[s1]\na=1\n[s2]\nb=2\n[s3]\nc=3\n[s4]\nk1=33\nk2=42"
    )
    append_two = three_section_kv_dict.copy()
    append_two["s4"] = {}
    append_two["s5"] = {"k1": 33, "k2": 42}
    assert (
        modifys(three_section_kv_str, append_two)
        == "[s1]\na=1\n[s2]\nb=2\n[s3]\nc=3\n[s4]\n[s5]\nk1=33\nk2=42"
    )
    append_with_header = three_section_kv_dict.copy()
    append_with_header["s4"] = {"k1": 33, "k2": 42}
    assert (
        modifys(three_section_kv_str, append_with_header, insert_header="New sections")
        == "[s1]\na=1\n[s2]\nb=2\n[s3]\nc=3\n# New sections\n[s4]\nk1=33\nk2=42"
    )

test_modify_first_kv

test_modify_first_kv(three_kv_str, three_kv_dict)

Test modifying first key-value pair

Source code in tests/test_card_file_modify.py
117
118
119
120
def test_modify_first_kv(three_kv_str, three_kv_dict):
    """Test modifying first key-value pair"""
    three_kv_dict["null"]["a"] = 42
    assert modifys(three_kv_str, three_kv_dict) == "[null]\na=42\nb=2\nc=3"

test_modify_last_kv

test_modify_last_kv(three_kv_str, three_kv_dict)

Test modifying last key-value pair

Source code in tests/test_card_file_modify.py
129
130
131
132
def test_modify_last_kv(three_kv_str, three_kv_dict):
    """Test modifying last key-value pair"""
    three_kv_dict["null"]["c"] = 42
    assert modifys(three_kv_str, three_kv_dict) == "[null]\na=1\nb=2\nc=42"

test_modify_middle_kv

test_modify_middle_kv(three_kv_str, three_kv_dict)

Test modifying middle key-value pair

Source code in tests/test_card_file_modify.py
123
124
125
126
def test_modify_middle_kv(three_kv_str, three_kv_dict):
    """Test modifying middle key-value pair"""
    three_kv_dict["null"]["b"] = 42
    assert modifys(three_kv_str, three_kv_dict) == "[null]\na=1\nb=42\nc=3"

test_modify_section

test_modify_section(
    three_section_kv_str, three_section_kv_dict
)

Test modifying whole sections

Source code in tests/test_card_file_modify.py
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
def test_modify_section(three_section_kv_str, three_section_kv_dict):
    """Test modifying whole sections"""
    modify_first = three_section_kv_dict.copy()
    modify_first["s1"] = {"k1": 33, "a": 1}
    assert (
        modifys(three_section_kv_str, modify_first)
        == "[s1]\na=1\nk1=33\n[s2]\nb=2\n[s3]\nc=3"
    )
    modfiy_mid = three_section_kv_dict.copy()
    modfiy_mid["s2"] = {"k1": 43, "k2": 44}
    assert (
        modifys(three_section_kv_str, modfiy_mid)
        == "[s1]\na=1\n[s2]\nk1=43\nk2=44\n[s3]\nc=3"
    )
    modify_last = three_section_kv_dict.copy()
    modify_last["s3"] = {"c": 3, "k1": 53}
    assert (
        modifys(three_section_kv_str, modify_last)
        == "[s1]\na=1\n[s2]\nb=2\n[s3]\nc=3\nk1=53"
    )

test_modify_values

test_modify_values(
    all_datatypes_dict,
    all_datatypes_str,
    all_datatypes_modified_str,
)
Source code in tests/test_card_file_modify.py
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
def test_modify_values(
    all_datatypes_dict, all_datatypes_str, all_datatypes_modified_str
):
    modified_dict = all_datatypes_dict
    # Remove existing keys
    modified_dict["logical"].pop("y2")
    modified_dict["date"].pop("end")
    modified_dict["empty"].pop("e1")
    # Modify existing keys
    modified_dict["logical"]["y1"] = False
    modified_dict["logical"]["n3"] = True
    modified_dict["logical"]["n4"] = True
    modified_dict["list"]["l1"] = [1, 2, 3]
    modified_dict["list"]["l5"] = [[1, 2], ["a", "b"]]
    modified_dict["list"]["l7"] = [42, False, True, "new_text", '"quotes"']
    modified_dict["date"]["begin"] = "2025-01-01"
    modified_dict["empty"]["e2"] = None
    modified_dict["string"]["n1"] = None
    # Add new keys
    modified_dict["date"]["new_end"] = "2025-12-01"
    modified_dict["path"]["file4"] = "/usr/lib"
    modified_dict["empty"]["e4"] = None
    modified_dict["empty_section2"]["number"] = 3.14
    modified_dict["empty_section3"] = {"number": 1012}
    assert (
        modifys(
            all_datatypes_str,
            modified_dict,
            insert_header="These subsequent sections were inserted by us!",
        )
        # TODO: There is something wrong and the test file has always the LF (or newline) character(s) at the end
        == all_datatypes_modified_str.rstrip("\n")
    )

test_null_modify

test_null_modify(all_datatypes_dict, all_datatypes_str)

Test modification with no modifications!

Source code in tests/test_card_file_modify.py
36
37
38
39
40
41
def test_null_modify(all_datatypes_dict, all_datatypes_str):
    """Test modification with no modifications!"""
    assert modifys(
        all_datatypes_str,
        all_datatypes_dict,
    ) == all_datatypes_str.rstrip("\n")

test_remove_first_kv

test_remove_first_kv(three_kv_str, three_kv_dict)

Test removing first key-value pair

Source code in tests/test_card_file_modify.py
 99
100
101
102
def test_remove_first_kv(three_kv_str, three_kv_dict):
    """Test removing first key-value pair"""
    del three_kv_dict["null"]["a"]
    assert modifys(three_kv_str, three_kv_dict) == "[null]\nb=2\nc=3"

test_remove_last_kv

test_remove_last_kv(three_kv_str, three_kv_dict)

Test removing last key-value pair

Source code in tests/test_card_file_modify.py
111
112
113
114
def test_remove_last_kv(three_kv_str, three_kv_dict):
    """Test removing last key-value pair"""
    del three_kv_dict["null"]["c"]
    assert modifys(three_kv_str, three_kv_dict) == "[null]\na=1\nb=2"

test_remove_middle_kv

test_remove_middle_kv(three_kv_str, three_kv_dict)

Test removing middle key-value pair

Source code in tests/test_card_file_modify.py
105
106
107
108
def test_remove_middle_kv(three_kv_str, three_kv_dict):
    """Test removing middle key-value pair"""
    del three_kv_dict["null"]["b"]
    assert modifys(three_kv_str, three_kv_dict) == "[null]\na=1\nc=3"

test_remove_section

test_remove_section(
    three_section_kv_str, three_section_kv_dict
)

Test removing whole sections

Source code in tests/test_card_file_modify.py
150
151
152
153
154
155
156
157
158
159
160
def test_remove_section(three_section_kv_str, three_section_kv_dict):
    """Test removing whole sections"""
    remove_first = three_section_kv_dict.copy()
    del remove_first["s1"]
    assert modifys(three_section_kv_str, remove_first) == "[s2]\nb=2\n[s3]\nc=3"
    remove_mid = three_section_kv_dict.copy()
    del remove_mid["s2"]
    assert modifys(three_section_kv_str, remove_mid) == "[s1]\na=1\n[s3]\nc=3"
    remove_last = three_section_kv_dict.copy()
    del remove_last["s3"]
    assert modifys(three_section_kv_str, remove_last) == "[s1]\na=1\n[s2]\nb=2"

three_kv_dict

three_kv_dict(three_kv_str) -> dict
Source code in tests/test_card_file_modify.py
84
85
86
@pytest.fixture
def three_kv_dict(three_kv_str) -> dict:
    return loads(three_kv_str)

three_kv_str

three_kv_str() -> str
Source code in tests/test_card_file_modify.py
79
80
81
@pytest.fixture
def three_kv_str() -> str:
    return "[null]\na=1\nb=2\nc=3"

three_section_kv_dict

three_section_kv_dict(three_section_kv_str) -> dict
Source code in tests/test_card_file_modify.py
94
95
96
@pytest.fixture
def three_section_kv_dict(three_section_kv_str) -> dict:
    return loads(three_section_kv_str)

three_section_kv_str

three_section_kv_str() -> str
Source code in tests/test_card_file_modify.py
89
90
91
@pytest.fixture
def three_section_kv_str() -> str:
    return "[s1]\na=1\n[s2]\nb=2\n[s3]\nc=3"