|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
| 3 | +from collections import OrderedDict |
3 | 4 | from contextlib import nullcontext as does_not_raise |
4 | 5 |
|
5 | 6 | import pytest |
@@ -57,6 +58,60 @@ def test_apply_settings(self, monkeypatch: MonkeyPatch, client_config: ClientCon |
57 | 58 | assert isinstance(gc.default_client_config.credentials, Token) |
58 | 59 | assert gc.default_client_config.project == "test-project" |
59 | 60 |
|
| 61 | + @pytest.mark.parametrize( |
| 62 | + "attr, value", |
| 63 | + [ |
| 64 | + ("max_retries", 0), |
| 65 | + ("max_retries", 10), |
| 66 | + ("max_retries_connect", 0), |
| 67 | + ("max_retries_connect", 3), |
| 68 | + ("max_retry_backoff", 0), |
| 69 | + ("max_retry_backoff", 60), |
| 70 | + ("max_connection_pool_size", 1), |
| 71 | + ("max_connection_pool_size", 20), |
| 72 | + ("file_download_chunk_size", None), |
| 73 | + ("file_download_chunk_size", 1024), |
| 74 | + ("file_upload_chunk_size", None), |
| 75 | + ("file_upload_chunk_size", 65536), |
| 76 | + ], |
| 77 | + ) |
| 78 | + def test_validated_attrs_valid(self, monkeypatch: MonkeyPatch, attr: str, value: object) -> None: |
| 79 | + # raising=True ensures the attribute actually exists on global_config, catching typos in parameters |
| 80 | + monkeypatch.setattr(global_config, attr, value, raising=True) |
| 81 | + assert getattr(global_config, attr) == value |
| 82 | + |
| 83 | + @pytest.mark.parametrize( |
| 84 | + "attr, value, match", |
| 85 | + [ |
| 86 | + ("max_retries", -1, "non-negative integer"), |
| 87 | + ("max_retries", 1.5, "non-negative integer"), |
| 88 | + ("max_retries", None, "non-negative integer"), |
| 89 | + ("max_retries_connect", -1, "non-negative integer"), |
| 90 | + ("max_retry_backoff", -1, "non-negative integer"), |
| 91 | + ("max_connection_pool_size", 0, "positive integer"), |
| 92 | + ("max_connection_pool_size", -1, "positive integer"), |
| 93 | + ("file_download_chunk_size", 0, "positive integer or None"), |
| 94 | + ("file_download_chunk_size", -1, "positive integer or None"), |
| 95 | + ("file_upload_chunk_size", 0, "positive integer or None"), |
| 96 | + ], |
| 97 | + ) |
| 98 | + def test_validated_attrs_invalid(self, attr: str, value: object, match: str) -> None: |
| 99 | + with pytest.raises(ValueError, match=match): |
| 100 | + setattr(global_config, attr, value) |
| 101 | + |
| 102 | + def test_apply_settings_invalid_value_is_rolled_back(self) -> None: |
| 103 | + original_retries = global_config.max_retries |
| 104 | + original_retries_connect = global_config.max_retries_connect |
| 105 | + # OrderedDict guarantees max_retries (valid) is applied before max_retries_connect (invalid), |
| 106 | + # so the rollback has something to actually undo: |
| 107 | + with pytest.raises(ValueError, match="non-negative integer"): |
| 108 | + global_config.apply_settings( |
| 109 | + OrderedDict([("max_retries", original_retries + 1), ("max_retries_connect", -1)]) |
| 110 | + ) |
| 111 | + |
| 112 | + assert global_config.max_retries == original_retries |
| 113 | + assert global_config.max_retries_connect == original_retries_connect |
| 114 | + |
60 | 115 | def test_load_non_existent_attr(self) -> None: |
61 | 116 | settings = { |
62 | 117 | "max_workers": 0, # use a nonsensical value to ensure that it is not applied without assuming other tests kept the default value |
|
0 commit comments