| 351 | |
| 352 | |
| 353 | class TestConversionBox: |
| 354 | def test_duplicate_detection_warn_error_and_ignore(self): |
| 355 | with pytest.raises(BoxError): |
| 356 | Box({"a b": 1, "a_b": 2}, box_duplicates="error") |
| 357 | with pytest.warns(BoxWarning): |
| 358 | Box({"a b": 1, "a_b": 2}, box_duplicates="warn") |
| 359 | with warnings.catch_warnings(): |
| 360 | warnings.simplefilter("error") |
| 361 | quiet = Box({"a b": 1, "a_b": 2}) |
| 362 | assert len(quiet) == 2 |
| 363 | with pytest.raises(BoxError): |
| 364 | Box(box_duplicates="error", conversion_box=False) |
| 365 | |
| 366 | def test_keywords_digits_and_unicode_keys_get_safe_aliases(self): |
| 367 | bx = Box({"class": 1, "def": 2, "1abc": 3, "ключ": 4, "naïve key": 5, "x-y": 6}) |
| 368 | assert bx.xclass == 1 |
| 369 | assert bx.xdef == 2 |
| 370 | assert bx.x1abc == 3 |
| 371 | assert bx.ключ == 4 |
| 372 | assert bx.naïve_key == 5 |
| 373 | assert bx.x_y == 6 |
| 374 | assert bx["class"] == 1 |
| 375 | dirs = dir(bx) |
| 376 | assert "xclass" in dirs and "ключ" in dirs and "class" not in dirs |
| 377 | |
| 378 | def test_deleting_a_key_releases_its_alias(self): |
| 379 | bx = Box({"a b": 1}, box_duplicates="error") |
| 380 | assert bx.a_b == 1 |
| 381 | del bx["a b"] |
| 382 | with pytest.raises(BoxKeyError): |
| 383 | bx.a_b |
| 384 | bx["a_b"] = 2 |
| 385 | assert bx.a_b == 2 |
| 386 | bx.clear() |
| 387 | bx["a b"] = 3 |
| 388 | assert bx.a_b == 3 |
| 389 | |
| 390 | def test_protected_names_stay_methods_but_work_as_keys(self): |
| 391 | bx = Box() |
| 392 | with pytest.raises(BoxKeyError): |
| 393 | bx.keys = 1 |
| 394 | with pytest.raises(BoxKeyError): |
| 395 | bx.to_dict = 1 |
| 396 | bx["keys"] = 1 |
| 397 | bx["update"] = 2 |
| 398 | assert bx["keys"] == 1 |
| 399 | assert callable(bx.keys) |
| 400 | assert callable(bx.update) |
| 401 | assert sorted(bx.keys()) == ["keys", "update"] |
| 402 | with pytest.raises(BoxKeyError): |
| 403 | del bx.keys |
| 404 | with pytest.raises(BoxError): |
| 405 | del bx._box_config |
| 406 | |
| 407 | def test_setting_method_names(self): |
| 408 | # Box's own converters are protected keys |
| 409 | for name in ("to_msgpack", "from_msgpack", "to_toon", "from_toon", "to_json"): |
| 410 | with pytest.raises(BoxKeyError, match="protected"): |
nothing calls this directly
no outgoing calls
no test coverage detected