Turn the `vault` test config functions into methods
Marco Ricci

Marco Ricci commited on 2025-08-08 23:09:22
Zeige 4 geänderte Dateien mit 22 Einfügungen und 45 Löschungen.


Turn the `is_valid_test_config`, `is_smudgable_vault_test_config` and
`_test_config_ids` functions into methods of the `VaultTestConfig`
class: `is_valid`, `is_smudgable` and `_test_id`.  This fits the overall
intent of only providing types and some simple predicates or
transformations, and it reads much nicer if a short method name is used
instead of an explicit function name.  (And if a one-argument function
is required, it can be obtained, as a bound method, from the class.)
... ...
@@ -285,35 +285,22 @@ class VaultTestConfig(NamedTuple):
285 285
     validation_settings: ValidationSettings | None
286 286
     """"""
287 287
 
288
-
289
-def is_valid_test_config(conf: VaultTestConfig, /) -> bool:
290
-    """Return true if the test config is valid.
291
-
292
-    Args:
293
-        conf: The test config to check.
294
-
295
-    """
296
-    return not conf.comment and conf.validation_settings in {
288
+    def is_valid(self, /) -> bool:
289
+        """Return true if this test config is valid."""
290
+        return not self.comment and self.validation_settings in {
297 291
             None,
298 292
             (True,),
299 293
         }
300 294
 
295
+    def is_smudgable(self) -> bool:
296
+        """Return true if this vault test config can be effectively smudged.
301 297
 
302
-def is_smudgable_vault_test_config(conf: VaultTestConfig) -> bool:
303
-    """Check whether this vault test config can be effectively smudged.
304
-
305
-    A "smudged" test config is one where falsy values (in the JavaScript
306
-    sense) can be replaced by other falsy values without changing the
307
-    meaning of the config.
308
-
309
-    Args:
310
-        conf: A test config to check.
311
-
312
-    Returns:
313
-        True if the test config can be smudged, False otherwise.
298
+        A "smudged" test config is one where falsy values (in the
299
+        JavaScript sense) can be replaced by other falsy values without
300
+        changing the meaning of the config.
314 301
 
315 302
         """
316
-    config = conf.config
303
+        config = self.config
317 304
         return bool(
318 305
             isinstance(config, dict)
319 306
             and ("global" not in config or isinstance(config["global"], dict))
... ...
@@ -322,11 +309,9 @@ def is_smudgable_vault_test_config(conf: VaultTestConfig) -> bool:
322 309
             and (config["services"] or config.get("global"))
323 310
         )
324 311
 
325
-
326
-def _test_config_ids(val: VaultTestConfig) -> Any:  # pragma: no cover
327
-    """pytest id function for VaultTestConfig objects."""
328
-    assert isinstance(val, VaultTestConfig)
329
-    return val[1] or (val[0], val[1], val[2])
312
+    def _test_id(self) -> Any:  # pragma: no cover
313
+        """Return a test ID, e.g., for `pytest`."""
314
+        return self[1] or (self[0], self[1], self[2])
330 315
 
331 316
 
332 317
 # Data objects
... ...
@@ -107,7 +107,7 @@ def vault_full_service_config(draw: strategies.DrawFn) -> dict[str, int]:
107 107
 def smudged_vault_test_config(
108 108
     draw: strategies.DrawFn,
109 109
     config: Any = strategies.sampled_from(tests.data.TEST_CONFIGS).filter(  # noqa: B008
110
-        tests.data.is_smudgable_vault_test_config
110
+        tests.data.VaultTestConfig.is_smudgable
111 111
     ),
112 112
 ) -> Any:
113 113
     """Hypothesis strategy to replace falsy values with other falsy values.
... ...
@@ -132,7 +132,7 @@ def smudged_vault_test_config(
132 132
     falsy_no_str = (None, False, 0, 0.0, float("nan"))
133 133
     falsy_no_zero = (None, False, "", float("nan"))
134 134
     conf = draw(config)
135
-    hypothesis.assume(tests.data.is_smudgable_vault_test_config(conf))
135
+    hypothesis.assume(conf.is_smudgable())
136 136
     obj = copy.deepcopy(conf.config)
137 137
     services: list[dict[str, Any]] = list(obj["services"].values())
138 138
     if "global" in obj:
... ...
@@ -1221,11 +1221,7 @@ class Parametrize(types.SimpleNamespace):
1221 1221
     )
1222 1222
     VALID_TEST_CONFIGS = pytest.mark.parametrize(
1223 1223
         "config",
1224
-        [
1225
-            conf.config
1226
-            for conf in TEST_CONFIGS
1227
-            if tests.data.is_valid_test_config(conf)
1228
-        ],
1224
+        [conf.config for conf in TEST_CONFIGS if conf.is_valid()],
1229 1225
     )
1230 1226
     KEY_OVERRIDING_IN_CONFIG = pytest.mark.parametrize(
1231 1227
         ["config", "command_line"],
... ...
@@ -3083,9 +3079,7 @@ class TestCLI:
3083 3079
     @hypothesis.given(
3084 3080
         conf=tests.machinery.hypothesis.smudged_vault_test_config(
3085 3081
             strategies.sampled_from([
3086
-                conf
3087
-                for conf in tests.data.TEST_CONFIGS
3088
-                if tests.data.is_valid_test_config(conf)
3082
+                conf for conf in tests.data.TEST_CONFIGS if conf.is_valid()
3089 3083
             ])
3090 3084
         )
3091 3085
     )
... ...
@@ -77,10 +77,12 @@ class Parametrize(types.SimpleNamespace):
77 77
             for conf in tests.data.TEST_CONFIGS
78 78
             if conf.validation_settings in {None, (True,)}
79 79
         ],
80
-        ids=tests.data._test_config_ids,
80
+        ids=tests.data.VaultTestConfig._test_id,
81 81
     )
82 82
     VAULT_TEST_CONFIGS = pytest.mark.parametrize(
83
-        "test_config", tests.data.TEST_CONFIGS, ids=tests.data._test_config_ids
83
+        "test_config",
84
+        tests.data.TEST_CONFIGS,
85
+        ids=tests.data.VaultTestConfig._test_id,
84 86
     )
85 87
 
86 88
 
... ...
@@ -127,9 +129,7 @@ def test_200_is_vault_config(test_config: tests.data.VaultTestConfig) -> None:
127 129
 @hypothesis.given(
128 130
     test_config=tests.machinery.hypothesis.smudged_vault_test_config(
129 131
         config=strategies.sampled_from([
130
-            conf
131
-            for conf in tests.data.TEST_CONFIGS
132
-            if tests.data.is_valid_test_config(conf)
132
+            conf for conf in tests.data.TEST_CONFIGS if conf.is_valid()
133 133
         ])
134 134
     )
135 135
 )
... ...
@@ -194,9 +194,7 @@ def test_400_validate_vault_config(
194 194
 @hypothesis.given(
195 195
     test_config=tests.machinery.hypothesis.smudged_vault_test_config(
196 196
         config=strategies.sampled_from([
197
-            conf
198
-            for conf in tests.data.TEST_CONFIGS
199
-            if tests.data.is_smudgable_vault_test_config(conf)
197
+            conf for conf in tests.data.TEST_CONFIGS if conf.is_smudgable()
200 198
         ])
201 199
     )
202 200
 )
203 201