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

1# SPDX-FileCopyrightText: 2026 Marco Ricci <software@the13thletter.info> 

2# 

3# SPDX-License-Identifier: Zlib 

4 

5"""Tests for `derivepassphrase._types`: basic tests.""" 

6 

7from __future__ import annotations 

8 

9import copy 

10import types 

11 

12import hypothesis 

13import pytest 

14from hypothesis import strategies 

15 

16from derivepassphrase import _types 

17from tests import data 

18from tests.machinery import hypothesis as hypothesis_machinery 

19 

20 

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 ) 

28 

29 

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 ) 

41 

42 

43class TestVaultConfig: 

44 """Test `vault` configuration detection.""" 

45 

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 ) 

59 

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? 

63 

64 Check all test configurations that do not need custom validation 

65 settings. 

66 

67 This primarily tests the [`_types.is_vault_config`][] and 

68 [`_types.clean_up_falsy_vault_config_values`][] functions. 

69 

70 """ 

71 self._test(test_config) 1d

72 

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? 

85 

86 Generate test data via hypothesis by smudging all valid test 

87 configurations. 

88 

89 This primarily tests the [`_types.is_vault_config`][] and 

90 [`_types.clean_up_falsy_vault_config_values`][] functions. 

91 

92 """ 

93 self._test(test_config) 1e

94 

95 

96class TestVaultConfigValidation: 

97 """Test the validation of `vault` configurations.""" 

98 

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 ) 

124 

125 @Parametrize.VAULT_TEST_CONFIGS 

126 def test_validate_config( 

127 self, 

128 test_config: data.VaultTestConfig, 

129 ) -> None: 

130 """Validate this vault configuration. 

131 

132 Check all test configurations, including those with non-standard 

133 validation settings. 

134 

135 This primarily tests the [`_types.validate_vault_config`][] and 

136 [`_types.clean_up_falsy_vault_config_values`][] functions. 

137 

138 """ 

139 self._test(test_config) 1b

140 

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. 

153 

154 Generate test data via hypothesis by smudging all smudgable test 

155 configurations. 

156 

157 This primarily tests the [`_types.validate_vault_config`][] and 

158 [`_types.clean_up_falsy_vault_config_values`][] functions. 

159 

160 """ 

161 self._test(test_config) 1c