Coverage for tests\test_utils.py: 100%

89 statements  

« prev     ^ index     » next       coverage.py v7.3.0, created at 2023-08-27 21:50 -0700

1#! /usr/bin/python3 

2# -*- coding: utf-8 -*- 

3 

4############################################################################## 

5# Copyright (C) 2021-current alexpdev 

6# 

7# Licensed under the Apache License, Version 2.0 (the "License"); 

8# you may not use this file except in compliance with the License. 

9# You may obtain a copy of the License at 

10# 

11# http://www.apache.org/licenses/LICENSE-2.0 

12# 

13# Unless required by applicable law or agreed to in writing, software 

14# distributed under the License is distributed on an "AS IS" BASIS, 

15# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 

16# See the License for the specific language governing permissions and 

17# limitations under the License. 

18############################################################################## 

19""" 

20Unittest functions for testing torrentfile utils module. 

21""" 

22import math 

23 

24import pytest 

25 

26from tests import dir1, dir2, rmpath 

27from torrentfile import utils 

28from torrentfile.__main__ import main 

29 

30 

31def test_main_exists(): 

32 """ 

33 Test if main exists 

34 """ 

35 assert main 

36 

37 

38@pytest.mark.parametrize("size", [156634528, 2**30, 67987, 16384, 8563945]) 

39def test_get_piece_length(size): 

40 """ 

41 Test function for best piece length for given size. 

42 """ 

43 value = utils.get_piece_length(size) 

44 assert value % 1024 == 0 

45 

46 

47@pytest.mark.parametrize("size", [156634528, 2**30, 67987, 16384, 8563945]) 

48def test_get_piece_length_max(size): 

49 """ 

50 Test function for best piece length for given size maximum. 

51 """ 

52 value = utils.get_piece_length(size) 

53 assert value < 2**27 

54 

55 

56@pytest.mark.parametrize("size", [156634528, 2**30, 67987, 16384, 8563945]) 

57def test_get_piece_length_min(size): 

58 """ 

59 Test function for best piece length for given size minimum. 

60 """ 

61 value = utils.get_piece_length(size) 

62 assert value >= 2**14 

63 

64 

65def test_get_path_length_mod(dir1): 

66 """ 

67 Test function for the best piece length for provided path. 

68 """ 

69 assert utils.path_piece_length(dir1) % (2**14) == 0 

70 

71 

72def test_get_path_length_min(dir1): 

73 """ 

74 Test function for getting piece length for folders min. 

75 """ 

76 assert utils.path_piece_length(dir1) >= (2**14) 

77 

78 

79def test_get_path_length_max(dir1): 

80 """ 

81 Test function for getting piece length for folders max. 

82 """ 

83 assert utils.path_piece_length(dir1) <= (2**27) 

84 

85 

86def test_path_stat(dir1): 

87 """ 

88 Test function for acquiring piece length information on folder. 

89 """ 

90 _, _, piece_length = utils.path_stat(dir1) 

91 assert piece_length % (2**14) == 0 

92 

93 

94def test_path_stat_size(dir1): 

95 """ 

96 Test function for acquiring total size information on folder. 

97 """ 

98 _, totalsize, _ = utils.path_stat(dir1) 

99 assert totalsize == (2**18) * 8 

100 

101 

102def test_path_stat_filelist_size(dir1): 

103 """ 

104 Test function for acquiring file list information on folder. 

105 """ 

106 filelist, _, _ = utils.path_stat(dir1) 

107 assert len(filelist) == 8 

108 

109 

110def test_get_filelist(dir1): 

111 """ 

112 Test function for get a list of files in a directory. 

113 """ 

114 filelist = utils.get_file_list(dir1) 

115 assert len(filelist) == 8 

116 

117 

118def test_get_path_size(dir1): 

119 """ 

120 Test function for getting total size of directory. 

121 """ 

122 pathsize = utils.path_size(dir1) 

123 assert pathsize == (2**18) * 8 

124 

125 

126def test_filelist_total(dir1): 

127 """ 

128 Test function for acquiring a filelist for directory. 

129 """ 

130 total, _ = utils.filelist_total(dir1) 

131 assert total == (2**18) * 8 

132 

133 

134def test_piecelength_error_fixtures(): 

135 """ 

136 Test exception for uninterpretable piece length value. 

137 """ 

138 try: 

139 raise utils.PieceLengthValueError("message") 

140 except utils.PieceLengthValueError: 

141 assert True 

142 assert dir1 

143 

144 

145def test_missing_path_error(): 

146 """ 

147 Test exception for missing path parameter. 

148 """ 

149 try: 

150 raise utils.MissingPathError("message") 

151 except utils.MissingPathError: 

152 assert True 

153 assert dir2 

154 

155 

156@pytest.mark.parametrize("value", [5, 32, 18, 225, 16384, 256000]) 

157def test_next_power_2(value): 

158 """ 

159 Test next power of 2 function in utils module. 

160 """ 

161 result = utils.next_power_2(value) 

162 log = math.log2(result) 

163 assert log == int(log) 

164 assert result % 2 == 0 

165 assert result >= value 

166 

167 

168@pytest.mark.parametrize( 

169 "amount, result", 

170 [ 

171 (1, f"{float(1)} Byte"), 

172 (100, "100.0 Bytes"), 

173 (1100, "1.1 KiB"), 

174 (1_100_000, "1.0 MiB"), 

175 (1_100_000_000, "1.0 GiB"), 

176 (4_400_120_000, "4.1 GiB"), 

177 (4_000_120_000, "3.7 GiB"), 

178 ], 

179) 

180def test_humanize_bytes(amount, result): 

181 """ 

182 Test humanize bytes function. 

183 """ 

184 assert utils.humanize_bytes(amount) == result 

185 

186 

187@pytest.mark.parametrize("amount, result", [(i, 2**i) for i in range(14, 25)]) 

188def test_normalize_piece_length_int(amount, result): 

189 """Test normalize piece length function. 

190 

191 Parameters 

192 ---------- 

193 amount : `str` or `int` 

194 piece length or representation 

195 result : int 

196 expected output. 

197 """ 

198 assert utils.normalize_piece_length(amount) == result 

199 

200 

201@pytest.mark.parametrize("amount, result", 

202 [(str(i), 2**i) for i in range(14, 21)]) 

203def test_normalize_piece_length_str(amount, result): 

204 """Test normalize piece length function. 

205 

206 Parameters 

207 ---------- 

208 amount : `str` or `int` 

209 piece length or representation 

210 result : int 

211 expected output. 

212 """ 

213 assert utils.normalize_piece_length(amount) == result 

214 

215 

216@pytest.mark.parametrize("amount", 

217 ["hello", 11, 0, 100000, 28, "zero", "fifteen"]) 

218def test_norm_plength_errors(amount): 

219 """Test function to normalize piece length errors. 

220 

221 Parameters 

222 ---------- 

223 amount : any 

224 arguments intended to raise an exception. 

225 """ 

226 try: 

227 assert utils.normalize_piece_length(amount) 

228 except utils.PieceLengthValueError: 

229 assert True 

230 

231 

232def test_filelisttotal_missing(dir2): 

233 """Test function filelist total with missing path. 

234 

235 Parameters 

236 ---------- 

237 dir2 : pytest.fixture 

238 fixture containing a temporary directory 

239 """ 

240 rmpath(dir2) 

241 try: 

242 utils.filelist_total(dir2) 

243 except utils.MissingPathError: 

244 assert True 

245 

246 

247def test_argument_error(): 

248 """ 

249 Test Argument Error. 

250 

251 Raises 

252 ------ 

253 utils.ArgumentError 

254 Arg error 

255 """ 

256 try: 

257 raise utils.ArgumentError("This message raised by argument error") 

258 except utils.ArgumentError: 

259 assert True