Skip to content

test_def_file_modify

Functions:

all_datatypes_dict

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

all_datatypes_modified

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

all_datatypes_str

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

includedef_add_str

includedef_add_str() -> str
Source code in tests/test_def_file_modify.py
41
42
43
@pytest.fixture
def includedef_add_str() -> str:
    return _read_file("includedef/add.def")

includedef_everything_str

includedef_everything_str() -> str
Source code in tests/test_def_file_modify.py
46
47
48
@pytest.fixture
def includedef_everything_str() -> str:
    return _read_file("includedef/everything.def")

includedef_ref

includedef_ref() -> dict
Source code in tests/test_def_file_modify.py
36
37
38
@pytest.fixture
def includedef_ref() -> dict:
    return _decode_file("includedef/reference.def")

includedef_ref_str

includedef_ref_str() -> str
Source code in tests/test_def_file_modify.py
51
52
53
@pytest.fixture
def includedef_ref_str() -> str:
    return _read_file("includedef/reference.def")

includedef_remove_str

includedef_remove_str() -> str
Source code in tests/test_def_file_modify.py
56
57
58
@pytest.fixture
def includedef_remove_str() -> str:
    return _read_file("includedef/remove.def")

test_append_kv

test_append_kv(three_kv_str, three_kv_dict)

Test appending of key-value pairs at the end

Source code in tests/test_def_file_modify.py
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
def test_append_kv(three_kv_str, three_kv_dict):
    """Test appending of key-value pairs at the end"""
    append_one = three_kv_dict.copy()
    append_one["d"] = 42
    assert modifys(three_kv_str, append_one) == "a=1\nb=2\nc=3\nd = 42"
    append_two = three_kv_dict.copy()
    append_two["d"] = 42
    append_two["e"] = 43
    assert modifys(three_kv_str, append_two) == "a=1\nb=2\nc=3\nd = 42\ne = 43"
    append_with_header = three_kv_dict.copy()
    append_with_header["d"] = 42
    assert (
        modifys(three_kv_str, append_with_header, "New values")
        == "a=1\nb=2\nc=3\n# New values\nd = 42"
    )

test_modify_single_kv

test_modify_single_kv(three_kv_str, three_kv_dict)

Test modifications of key-value pairs at different positions

Source code in tests/test_def_file_modify.py
 96
 97
 98
 99
100
101
102
103
104
105
106
def test_modify_single_kv(three_kv_str, three_kv_dict):
    """Test modifications of key-value pairs at different positions"""
    modify_first = three_kv_dict.copy()
    modify_first["a"] = 42
    assert modifys(three_kv_str, modify_first) == "a=42\nb=2\nc=3"
    modify_mid = three_kv_dict.copy()
    modify_mid["b"] = 42
    assert modifys(three_kv_str, modify_mid) == "a=1\nb=42\nc=3"
    modify_last = three_kv_dict.copy()
    modify_last["c"] = 42
    assert modifys(three_kv_str, modify_last) == "a=1\nb=2\nc=42"

test_modify_values_str

test_modify_values_str(
    all_datatypes_dict,
    all_datatypes_str,
    all_datatypes_modified,
)
Source code in tests/test_def_file_modify.py
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
def test_modify_values_str(
    all_datatypes_dict, all_datatypes_str, all_datatypes_modified
):
    modified_dict = all_datatypes_dict
    # Remove existing keys
    modified_dict.pop("logical_true2")
    modified_dict.pop("string_single_quotes")
    modified_dict.pop("array_mixed")
    # Modify existing values
    modified_dict["logical_true1"] = False
    modified_dict["logical_true5"] = False
    modified_dict["logical_false3"] = True
    modified_dict["logical_false4"] = True
    modified_dict["integer"] = 3.14
    modified_dict["integer_zero"] = -100
    modified_dict["string"] = "halo?"
    modified_dict["string_double_quotes"] = '"halo?"'
    modified_dict["array_floats"] = [True, False, True]
    modified_dict["auto_default_float"] = ("_AUTOBLOCKER_", 100)
    # Add new keys
    modified_dict["new_string"] = "halo?halo?"
    modified_dict["new_integer"] = 333
    modified_dict["new_auto"] = ("_AUTO_", None)
    modified_dict["new_auto_default"] = ("_AUTO_", 3.14)
    # Modify INCLUDEDEF
    modified_dict["INCLUDEDEF"] = ["module1", "module3.def"]
    assert (
        modifys(
            all_datatypes_str,
            modified_dict,
            insert_header="These subsequent keys 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.rstrip("\n")
    )

test_modifys_includedef_add

test_modifys_includedef_add(
    includedef_ref, includedef_ref_str, includedef_add_str
)
Source code in tests/test_def_file_modify.py
168
169
170
171
def test_modifys_includedef_add(includedef_ref, includedef_ref_str, includedef_add_str):
    includedef_ref["INCLUDEDEF"].insert(1, "module1A")
    includedef_ref["INCLUDEDEF"].insert(4, '"module3A.def"')
    assert modifys(includedef_ref_str, includedef_ref) == includedef_add_str

test_modifys_includedef_empty

test_modifys_includedef_empty(
    includedef_ref,
    includedef_ref_str,
    includedef_remove_str,
)
Source code in tests/test_def_file_modify.py
182
183
184
185
186
def test_modifys_includedef_empty(
    includedef_ref, includedef_ref_str, includedef_remove_str
):
    includedef_ref["INCLUDEDEF"] = []
    assert modifys(includedef_ref_str, includedef_ref) == ""

test_modifys_includedef_everything

test_modifys_includedef_everything(
    includedef_ref,
    includedef_ref_str,
    includedef_everything_str,
)
Source code in tests/test_def_file_modify.py
189
190
191
192
193
194
195
196
def test_modifys_includedef_everything(
    includedef_ref, includedef_ref_str, includedef_everything_str
):
    includedef_ref["INCLUDEDEF"].pop(1)
    includedef_ref["INCLUDEDEF"].pop(1)
    includedef_ref["INCLUDEDEF"].insert(0, "module1A")
    includedef_ref["INCLUDEDEF"].append("'module4A.def'")
    assert modifys(includedef_ref_str, includedef_ref) == includedef_everything_str

test_modifys_includedef_ref

test_modifys_includedef_ref(
    includedef_ref, includedef_ref_str
)

Test if making no changes doesn't change the input text!

Source code in tests/test_def_file_modify.py
163
164
165
def test_modifys_includedef_ref(includedef_ref, includedef_ref_str):
    """Test if making no changes doesn't change the input text!"""
    assert modifys(includedef_ref_str, includedef_ref) == includedef_ref_str

test_modifys_includedef_remove

test_modifys_includedef_remove(
    includedef_ref,
    includedef_ref_str,
    includedef_remove_str,
)
Source code in tests/test_def_file_modify.py
174
175
176
177
178
179
def test_modifys_includedef_remove(
    includedef_ref, includedef_ref_str, includedef_remove_str
):
    includedef_ref["INCLUDEDEF"].pop(2)
    includedef_ref["INCLUDEDEF"].pop(0)
    assert modifys(includedef_ref_str, includedef_ref) == includedef_remove_str

test_null_modify

test_null_modify(all_datatypes_dict, all_datatypes_str)

Test modification with no modifications!

Source code in tests/test_def_file_modify.py
71
72
73
74
75
76
77
78
79
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
    )

test_remove_single_kv

test_remove_single_kv(three_kv_str, three_kv_dict)

Test removal of key-value pairs at different positions

Source code in tests/test_def_file_modify.py
82
83
84
85
86
87
88
89
90
91
92
93
def test_remove_single_kv(three_kv_str, three_kv_dict):
    """Test removal of key-value pairs at different positions"""
    remove_first = three_kv_dict.copy()
    del remove_first["a"]
    assert modifys(three_kv_str, remove_first) == "b=2\nc=3"
    remove_mid = three_kv_dict.copy()
    del remove_mid["b"]
    assert modifys(three_kv_str, remove_mid) == "a=1\nc=3"
    remove_last = three_kv_dict.copy()
    del remove_last["c"]
    # TODO: when removing the last item, the preceeding newline is not removed
    assert modifys(three_kv_str, remove_last) == "a=1\nb=2\n"

three_kv_dict

three_kv_dict() -> dict
Source code in tests/test_def_file_modify.py
66
67
68
@pytest.fixture
def three_kv_dict() -> dict:
    return loads("a=1\nb=2\nc=3")

three_kv_str

three_kv_str() -> str
Source code in tests/test_def_file_modify.py
61
62
63
@pytest.fixture
def three_kv_str() -> str:
    return "a=1\nb=2\nc=3"