| 192 | |
| 193 | |
| 194 | class TestConfigBoxPaths: |
| 195 | def test_update_non_string_and_exact_keys(self): |
| 196 | cb = ConfigBox({"a": 1}) |
| 197 | cb.update({1: "one", "a": 2}) |
| 198 | assert cb.to_dict() == {"a": 2, 1: "one"} |
| 199 | |
| 200 | def test_missing_dotted_key(self): |
| 201 | cb = ConfigBox({"a": {"b": 1}}, box_dots=True) |
| 202 | assert "nope.x" not in cb |
| 203 | assert cb["A.B"] == 1 |
| 204 | |
| 205 | def test_index_rebuilds_after_direct_dict_write(self): |
| 206 | cb = ConfigBox({"a": 1}) |
| 207 | assert "A" in cb # builds the lower case index |
| 208 | dict.__setitem__(cb, "Raw", 2) |
| 209 | cb["b"] = 3 # the index is stale: dropped and rebuilt on the next lookup |
| 210 | assert cb.raw == 2 and cb.B == 3 |
| 211 | |
| 212 | def test_index_rebuilds_after_direct_write_then_exact_write(self): |
| 213 | cb = ConfigBox({"a": 1}) |
| 214 | assert "A" in cb |
| 215 | dict.__setitem__(cb, "Raw", 2) |
| 216 | cb["a"] = 5 # exact key, no lookup first, so the stale index is noticed on the write |
| 217 | assert cb.raw == 2 and cb.A == 5 |
| 218 | |
| 219 | def test_reflected_add_on_config_box(self): |
| 220 | assert ({"FOO": 2} + ConfigBox({"Foo": 1, "bar": 3})).to_dict() == {"bar": 3, "Foo": 1} |
| 221 | |
| 222 | def test_index_tracks_non_string_keys(self): |
| 223 | cb = ConfigBox({"a": 1}) |
| 224 | assert "A" in cb |
| 225 | cb[5] = "five" |
| 226 | assert cb[5] == "five" and cb.A == 1 |
| 227 | |
| 228 | def test_index_invalidated_by_nested_dotted_write(self): |
| 229 | cb = ConfigBox(default_box=True, box_dots=True) |
| 230 | assert "x" not in cb |
| 231 | cb["a.b"] = 1 |
| 232 | assert cb["A.B"] == 1 |
| 233 | |
| 234 | def test_case_variants_without_conversion_box(self): |
| 235 | cb = ConfigBox({"Foo": 1}, conversion_box=False) |
| 236 | cb["FOO"] = 2 |
| 237 | cb.merge_update({"foo": 3}) |
| 238 | assert cb.to_dict() == {"foo": 3} |
| 239 | |
| 240 | def test_take_over_keeps_other_alias(self): |
| 241 | cb = ConfigBox({"a_b": 1, "a b": 2}) |
| 242 | cb["A_B"] = 3 |
| 243 | assert cb.to_dict() == {"a b": 2, "A_B": 3} |
| 244 | |
| 245 | def test_box_config_key_is_stored_as_data(self): |
| 246 | cb = ConfigBox() |
| 247 | cb["_box_config"] = 5 |
| 248 | assert dict.__getitem__(cb, "_box_config") == 5 |
| 249 | |
| 250 | def test_update_too_many_arguments(self): |
| 251 | with pytest.raises(BoxTypeError, match="at most 1 argument"): |
nothing calls this directly
no outgoing calls
no test coverage detected