Coverage for tests/unit/errormode/test_errormode_functionality.py: 98%

62 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2025-04-04 03:33 -0600

1from __future__ import annotations 

2 

3import warnings 

4 

5from muutils.errormode import ErrorMode 

6 

7import pytest 

8 

9 

10def test_except(): 

11 with pytest.raises(ValueError): 

12 ErrorMode.EXCEPT.process("test-except", except_cls=ValueError) 

13 

14 with pytest.raises(TypeError): 

15 ErrorMode.EXCEPT.process("test-except", except_cls=TypeError) 

16 

17 with pytest.raises(RuntimeError): 

18 ErrorMode.EXCEPT.process("test-except", except_cls=RuntimeError) 

19 

20 with pytest.raises(KeyError): 

21 ErrorMode.EXCEPT.process("test-except", except_cls=KeyError) 

22 

23 with pytest.raises(KeyError): 

24 ErrorMode.EXCEPT.process( 

25 "test-except", except_cls=KeyError, except_from=ValueError("base exception") 

26 ) 

27 

28 

29def test_warn(): 

30 with pytest.warns(UserWarning): 

31 ErrorMode.WARN.process("test-warn", warn_cls=UserWarning) 

32 

33 with pytest.warns(Warning): 

34 ErrorMode.WARN.process("test-warn", warn_cls=Warning) 

35 

36 with pytest.warns(DeprecationWarning): 

37 ErrorMode.WARN.process("test-warn", warn_cls=DeprecationWarning) 

38 

39 

40def test_ignore(): 

41 with warnings.catch_warnings(record=True) as w: 

42 ErrorMode.IGNORE.process("test-ignore") 

43 

44 ErrorMode.IGNORE.process("test-ignore", except_cls=ValueError) 

45 ErrorMode.IGNORE.process("test-ignore", except_from=TypeError("base exception")) 

46 

47 ErrorMode.IGNORE.process("test-ignore", warn_cls=UserWarning) 

48 

49 assert len(w) == 0, f"There should be no warnings: {w}" 

50 

51 

52def test_except_custom(): 

53 class MyCustomError(ValueError): 

54 pass 

55 

56 with pytest.raises(MyCustomError): 

57 ErrorMode.EXCEPT.process("test-except", except_cls=MyCustomError) 

58 

59 

60def test_warn_custom(): 

61 class MyCustomWarning(Warning): 

62 pass 

63 

64 with pytest.warns(MyCustomWarning): 

65 ErrorMode.WARN.process("test-warn", warn_cls=MyCustomWarning) 

66 

67 

68def test_except_mode_chained_exception(): 

69 try: 

70 # set up the base exception 

71 try: 

72 raise KeyError("base exception") 

73 except Exception as base_exception: 

74 # catch it, raise another exception with it as the cause 

75 ErrorMode.EXCEPT.process( 

76 "Test chained exception", 

77 except_cls=RuntimeError, 

78 except_from=base_exception, 

79 ) 

80 # catch the outer exception 

81 except RuntimeError as e: 

82 assert str(e) == "Test chained exception" 

83 # check that the cause is the base exception 

84 assert isinstance(e.__cause__, KeyError) 

85 assert repr(e.__cause__) == "KeyError('base exception')" 

86 else: 

87 assert False, "Expected RuntimeError with cause KeyError" 

88 

89 

90def test_logging_global(): 

91 import muutils.errormode as errormode 

92 

93 log: list[str] = [] 

94 

95 def log_func(msg: str): 

96 log.append(msg) 

97 

98 ErrorMode.LOG.process("test-log-print") 

99 

100 errormode.GLOBAL_LOG_FUNC = log_func 

101 

102 ErrorMode.LOG.process("test-log") 

103 ErrorMode.LOG.process("test-log-2") 

104 

105 assert log == ["test-log", "test-log-2"] 

106 

107 ErrorMode.LOG.process("test-log-3") 

108 

109 assert log == ["test-log", "test-log-2", "test-log-3"] 

110 

111 

112# def test_logging_pass(): 

113# errmode: ErrorMode = ErrorMode.LOG 

114 

115# log: list[str] = [] 

116# def log_func(msg: str): 

117# log.append(msg) 

118 

119# errmode.process( 

120# "test-log", 

121# log_func=log_func, 

122# ) 

123 

124# errmode.process( 

125# "test-log-2", 

126# log_func=log_func, 

127# ) 

128 

129# assert log == ["test-log", "test-log-2"] 

130 

131 

132# def test_logging_init(): 

133# errmode: ErrorMode = ErrorMode.LOG 

134 

135# log: list[str] = [] 

136# def log_func(msg: str): 

137# log.append(msg) 

138 

139# errmode.set_log_loc(log_func) 

140 

141# errmode.process("test-log") 

142# errmode.process("test-log-2") 

143 

144# assert log == ["test-log", "test-log-2"] 

145 

146# errmode_2: ErrorMode = ErrorMode.LOG 

147# log_2: list[str] = [] 

148# def log_func_2(msg: str): 

149# log_2.append(msg) 

150 

151# errmode_2.set_log_loc(log_func_2) 

152 

153# errmode_2.process("test-log-3") 

154# errmode_2.process("test-log-4") 

155 

156# assert log_2 == ["test-log-3", "test-log-4"] 

157# assert log == ["test-log", "test-log-2"] 

158 

159 

160# def test_logging_init_2(): 

161# log: list[str] = [] 

162# def log_func(msg: str): 

163# log.append(msg) 

164 

165# errmode: ErrorMode = ErrorMode.LOG.set_log_loc(log_func) 

166 

167# errmode.process("test-log") 

168# errmode.process("test-log-2") 

169 

170# assert log == ["test-log", "test-log-2"]