Coverage for tests / test_derivepassphrase_types / test_000_basic.py: 100.000%
47 statements
« prev ^ index » next coverage.py v7.13.1, created at 2026-07-06 21:34 +0200
« prev ^ index » next coverage.py v7.13.1, created at 2026-07-06 21:34 +0200
1# SPDX-FileCopyrightText: 2026 Marco Ricci <software@the13thletter.info>
2#
3# SPDX-License-Identifier: Zlib
5"""Tests for `derivepassphrase._types`: basic tests."""
7from __future__ import annotations
9import copy
10import types
12import hypothesis
13import pytest
14from hypothesis import strategies
16from derivepassphrase import _types
17from tests import data
18from tests.machinery import hypothesis as hypothesis_machinery
21class Strategies:
22 VALID_VAULT_TEST_CONFIGS = tuple(
23 conf for conf in data.TEST_CONFIGS if conf.is_valid()
24 )
25 SMUDGABLE_VAULT_TEST_CONFIGS = tuple(
26 conf for conf in data.TEST_CONFIGS if conf.is_smudgable()
27 )
30class Parametrize(types.SimpleNamespace):
31 VALID_VAULT_TEST_CONFIGS = pytest.mark.parametrize(
32 "test_config",
33 Strategies.VALID_VAULT_TEST_CONFIGS,
34 ids=data.VaultTestConfig._test_id,
35 )
36 VAULT_TEST_CONFIGS = pytest.mark.parametrize(
37 "test_config",
38 data.TEST_CONFIGS,
39 ids=data.VaultTestConfig._test_id,
40 )
43class TestVaultConfig:
44 """Test `vault` configuration detection."""
46 @staticmethod
47 def _test(test_config: data.VaultTestConfig) -> None:
48 config, comment, _ = test_config 1de
49 obj = copy.deepcopy(config) 1de
50 did_cleanup = _types.clean_up_falsy_vault_config_values(obj) 1de
51 assert _types.is_vault_config(obj) == (not comment), ( 1de
52 "failed to complain about: " + comment
53 if comment
54 else "failed on valid example"
55 )
56 assert did_cleanup is None or bool(did_cleanup) == (obj != config), ( 1de
57 "mismatched report on cleanup work"
58 )
60 @Parametrize.VALID_VAULT_TEST_CONFIGS
61 def test_is_config(self, test_config: data.VaultTestConfig) -> None:
62 """Is this vault configuration recognized as valid/invalid?
64 Check all test configurations that do not need custom validation
65 settings.
67 This primarily tests the [`_types.is_vault_config`][] and
68 [`_types.clean_up_falsy_vault_config_values`][] functions.
70 """
71 self._test(test_config) 1d
73 @hypothesis.given(
74 test_config=hypothesis_machinery.smudged_vault_test_config(
75 config=strategies.sampled_from([
76 conf for conf in data.TEST_CONFIGS if conf.is_valid()
77 ])
78 )
79 )
80 def test_is_config_even_if_smudged(
81 self,
82 test_config: data.VaultTestConfig,
83 ) -> None:
84 """Is this vault configuration recognized as valid/invalid?
86 Generate test data via hypothesis by smudging all valid test
87 configurations.
89 This primarily tests the [`_types.is_vault_config`][] and
90 [`_types.clean_up_falsy_vault_config_values`][] functions.
92 """
93 self._test(test_config) 1e
96class TestVaultConfigValidation:
97 """Test the validation of `vault` configurations."""
99 def _test(
100 self,
101 test_config: data.VaultTestConfig,
102 ) -> None:
103 config, comment, validation_settings = test_config 1bc
104 (allow_unknown_settings,) = validation_settings or (True,) 1bc
105 obj = copy.deepcopy(config) 1bc
106 did_cleanup = _types.clean_up_falsy_vault_config_values(obj) 1bc
107 if comment: 1bc
108 with pytest.raises((TypeError, ValueError)): 1bc
109 _types.validate_vault_config( 1bc
110 obj,
111 allow_unknown_settings=allow_unknown_settings,
112 )
113 else:
114 try: 1bc
115 _types.validate_vault_config( 1bc
116 obj,
117 allow_unknown_settings=allow_unknown_settings,
118 )
119 except (TypeError, ValueError): # pragma: no cover
120 pytest.fail("failed to validate valid example")
121 assert did_cleanup is None or bool(did_cleanup) == (obj != config), ( 1bc
122 "mismatched report on cleanup work"
123 )
125 @Parametrize.VAULT_TEST_CONFIGS
126 def test_validate_config(
127 self,
128 test_config: data.VaultTestConfig,
129 ) -> None:
130 """Validate this vault configuration.
132 Check all test configurations, including those with non-standard
133 validation settings.
135 This primarily tests the [`_types.validate_vault_config`][] and
136 [`_types.clean_up_falsy_vault_config_values`][] functions.
138 """
139 self._test(test_config) 1b
141 @hypothesis.given(
142 test_config=hypothesis_machinery.smudged_vault_test_config(
143 config=strategies.sampled_from([
144 conf for conf in data.TEST_CONFIGS if conf.is_smudgable()
145 ])
146 )
147 )
148 def test_validate_config_even_if_smudged(
149 self,
150 test_config: data.VaultTestConfig,
151 ) -> None:
152 """Validate this vault configuration.
154 Generate test data via hypothesis by smudging all smudgable test
155 configurations.
157 This primarily tests the [`_types.validate_vault_config`][] and
158 [`_types.clean_up_falsy_vault_config_values`][] functions.
160 """
161 self._test(test_config) 1c