Coverage for tests/utility/test_numbers.py: 100%

73 statements  

« prev     ^ index     » next       coverage.py v7.10.5, created at 2025-08-28 09:13 +0000

1"""Test module for the functions in the `utility/dict.py` module. 

2 

3This module contains unit tests for the functions implemented in the `dict.py` module. The purpose of these tests is to 

4ensure the correct functionality of each function in different scenarios and to validate that the expected outputs are 

5returned. 

6 

7Tests should cover various edge cases, valid inputs, and any other conditions that are necessary to confirm the 

8robustness of the functions.""" 

9 

10from app.utility.numbers import * 

11 

12 

13class TestGetPowerHtml: 

14 

15 def test_single_value_zero(self): 

16 """Test when the value is zero.""" 

17 

18 result = get_power_html(0) 

19 assert result == "0" 

20 

21 def test_single_value_positive(self): 

22 """Test when the value is a positive number with fixed decimal precision.""" 

23 

24 result = get_power_html(1234.5646, 2) 

25 assert result == "1.23 &#10005; 10<sup>3</sup>" 

26 

27 def test_single_value_negative(self): 

28 """Test when the value is a negative number.""" 

29 

30 result = get_power_html(-1234.56, 1) 

31 assert result == "-1.2 &#10005; 10<sup>3</sup>" 

32 

33 def test_single_value_exponent_only(self): 

34 """Test when the value is a positive number with exponent only.""" 

35 result = get_power_html(123456789, None) 

36 assert result == "&#10005; 10<sup>8</sup>" 

37 

38 def test_single_value_all_significant_digits(self): 

39 """Test when n is -1 to show all significant digits.""" 

40 result = get_power_html(1234.56789, -1) 

41 assert result == "1.23457 &#10005; 10<sup>3</sup>" 

42 

43 def test_single_value_with_small_exponent(self): 

44 """Test when the value has a small exponent (e.g., a number less than 1).""" 

45 result = get_power_html(0.000123, 3) 

46 assert result == "1.230 &#10005; 10<sup>-4</sup>" 

47 

48 def test_list_input(self): 

49 """Test when the input is a list of values.""" 

50 values = [1234.56, 0, 987654.321] 

51 result = get_power_html(values, 2) 

52 expected = ["1.23 &#10005; 10<sup>3</sup>", "0", "9.88 &#10005; 10<sup>5</sup>"] 

53 assert result == expected 

54 

55 def test_empty_list(self): 

56 """Test when the input is an empty list.""" 

57 result = get_power_html([], 2) 

58 assert result == [] 

59 

60 def test_base0(self): 

61 

62 result = get_power_html(1.4, 1) 

63 assert result == "1.4" 

64 

65 result = get_power_html(-1.4, 1) 

66 assert result == "-1.4" 

67 

68 

69class TestToScientific: 

70 """Test cases for the to_scientific function""" 

71 

72 def test_single_float(self) -> None: 

73 """Test single float to scientific notation""" 

74 result = to_scientific(1.4e-4) 

75 assert result == "1.4E-04" 

76 

77 def test_single_integer(self) -> None: 

78 """Test single integer to scientific notation""" 

79 result = to_scientific(5000) 

80 assert result == "5E+03" 

81 

82 def test_none_input(self) -> None: 

83 """Test None input""" 

84 result = to_scientific(None) 

85 assert result == "" 

86 

87 def test_single_negative_float(self) -> None: 

88 """Test single negative float to scientific notation""" 

89 result = to_scientific(-1.4e-4) 

90 assert result == "-1.4E-04" 

91 

92 def test_single_float_no_trailing_zeros(self) -> None: 

93 """Test float with no trailing zeros""" 

94 result = to_scientific(1.0e5) 

95 assert result == "1E+05" 

96 

97 def test_list_of_floats(self) -> None: 

98 """Test list of floats to scientific notation""" 

99 result = to_scientific([1e-4, 1e-5]) 

100 assert result == "1E-04, 1E-05" 

101 

102 def test_list_of_mixed_numbers(self) -> None: 

103 """Test list of integers and floats to scientific notation""" 

104 result = to_scientific([100, 1.4e-3, 5]) 

105 assert result == "1E+02, 1.4E-03, 5E+00" 

106 

107 def test_empty_list(self) -> None: 

108 """Test empty list input""" 

109 result = to_scientific([]) 

110 assert result == "" 

111 

112 def test_large_number(self) -> None: 

113 """Test large number to scientific notation""" 

114 result = to_scientific(1e100) 

115 assert result == "1E+100" 

116 

117 def test_edge_case_zero(self) -> None: 

118 """Test zero as an edge case""" 

119 result = to_scientific(0) 

120 assert result == "0" 

121 

122 def test_single_integer_with_zero_trailing(self) -> None: 

123 """Test integer with trailing zeros""" 

124 result = to_scientific(5000000) 

125 assert result == "5E+06" 

126 

127 

128class TestGetConcentrationsHtml: 

129 

130 def test_basic_case(self) -> None: 

131 """Test with a basic list of initial carrier concentrations.""" 

132 N0s = [1.6e16, 2e17, 3e18] 

133 result = get_concentrations_html(N0s) 

134 expected = [ 

135 "N<sub>0</sub> = 1.6 &#10005; 10<sup>16</sup> cm<sup>-3</sup>", 

136 "N<sub>0</sub> = 2 &#10005; 10<sup>17</sup> cm<sup>-3</sup>", 

137 "N<sub>0</sub> = 3 &#10005; 10<sup>18</sup> cm<sup>-3</sup>", 

138 ] 

139 assert result == expected