Coverage for tests/test_compression.py: 100%

113 statements  

« prev     ^ index     » next       coverage.py v7.6.10, created at 2025-02-28 11:40 +0100

1import tempfile 

2import typing as t 

3from contextlib import chdir 

4from pathlib import Path 

5 

6from edwh_files_plugin.compression import Compression, Gzip, Nocompression, Pigz, Zip 

7 

8DATA = "x" * int(1e9) 

9 

10 

11def run_test_with_file(compressor: Compression, extension: str, decompressor: t.Optional[Compression] = None): 

12 decompressor = decompressor or compressor 

13 assert compressor.is_available() 

14 assert decompressor.is_available() 

15 

16 with tempfile.TemporaryDirectory(prefix="pytest_file") as d: 

17 dir_path = Path(d) 

18 bigfile = dir_path / "myfile.txt" 

19 bigfile.write_text(DATA) 

20 

21 lilfile = dir_path / f"myfile.{extension}" 

22 

23 assert compressor.compress(bigfile, lilfile) 

24 

25 assert lilfile.exists() 

26 assert lilfile.stat().st_size < bigfile.stat().st_size 

27 

28 unzipped = dir_path / "unzipped.txt" 

29 assert decompressor.decompress(lilfile, unzipped) 

30 

31 assert unzipped.read_text() == DATA 

32 

33 

34def run_test_with_folder(compressor: Compression, extension: str, decompressor: t.Optional[Compression] = None): 

35 decompressor = decompressor or compressor 

36 assert compressor.is_available() 

37 assert decompressor.is_available() 

38 

39 with tempfile.TemporaryDirectory(prefix="pytest_folder") as d: 

40 parent_d = Path(d) 

41 child_d = parent_d / "somefolder" 

42 child_d.mkdir() 

43 

44 bigfile = child_d / "raw.txt" 

45 bigfile.write_text(DATA) 

46 

47 file2 = child_d / "small.txt" 

48 file2.write_text("-") 

49 

50 lilfile = parent_d / f"compressed.{extension}" 

51 

52 assert compressor.compress(child_d, lilfile) 

53 

54 assert lilfile.exists() 

55 assert lilfile.stat().st_size < bigfile.stat().st_size 

56 

57 unzipped_d = parent_d / "somefolder2" 

58 unzipped_d.mkdir() 

59 assert decompressor.decompress(lilfile, unzipped_d) 

60 

61 unzipped = unzipped_d / "raw.txt" 

62 assert unzipped.read_text() == DATA 

63 

64 

65def test_zip(): 

66 zip_compression = Compression.for_extension("zip") 

67 assert isinstance(zip_compression, Zip) 

68 run_test_with_file(zip_compression, "zip") 

69 run_test_with_folder(zip_compression, "zip") 

70 

71 

72def test_gzip(): 

73 gzip_compression = Gzip() 

74 assert isinstance(gzip_compression, Gzip) 

75 run_test_with_file(gzip_compression, "gz") 

76 run_test_with_folder(gzip_compression, "tgz") 

77 run_test_with_folder(gzip_compression, "tar.gz") 

78 

79 

80def test_pigz(): 

81 pigz_compression = Compression.for_extension("gz") 

82 assert isinstance(pigz_compression, Pigz) # pigz > gz 

83 run_test_with_file(pigz_compression, "gz") 

84 run_test_with_folder(pigz_compression, "tgz") 

85 run_test_with_folder(pigz_compression, "tar.gz") 

86 

87 

88def test_gzip_pigz_cross(): 

89 gz_compression = Gzip() 

90 pigz_compression = Pigz() 

91 run_test_with_file(pigz_compression, "gz", decompressor=gz_compression) 

92 run_test_with_folder(pigz_compression, "tgz", decompressor=gz_compression) 

93 

94 run_test_with_file(gz_compression, "gz", decompressor=pigz_compression) 

95 run_test_with_folder(gz_compression, "tgz", decompressor=pigz_compression) 

96 

97 

98def test_noop(): 

99 class Noop(Compression, extension="noop"): ... 

100 

101 # false because it is not available 

102 assert not Compression.for_extension("noop") 

103 assert not Noop.is_available() 

104 

105 assert not Compression.for_extension("fake") 

106 

107 

108def test_nocompression(): 

109 compressor = Nocompression() 

110 

111 # test file: 

112 assert compressor.is_available() 

113 

114 with tempfile.TemporaryDirectory(prefix="pytest_file") as d: 

115 dir_path = Path(d) 

116 bigfile = dir_path / "myfile.txt" 

117 bigfile.write_text(DATA) 

118 

119 lilfile = dir_path / f"myfile.txt" 

120 

121 assert compressor.compress(bigfile) 

122 

123 assert lilfile.exists() 

124 assert lilfile.stat().st_size == bigfile.stat().st_size 

125 

126 unzipped = dir_path / "myfile.txt" 

127 assert compressor.decompress(lilfile) 

128 

129 assert unzipped.read_text() == DATA 

130 

131 # test folder: 

132 with tempfile.TemporaryDirectory(prefix="pytest_folder") as d: 

133 parent_d = Path(d) 

134 child_d = parent_d / "somefolder" 

135 child_d.mkdir() 

136 

137 bigfile = child_d / "raw.txt" 

138 bigfile.write_text(DATA) 

139 

140 file2 = child_d / "small.txt" 

141 file2.write_text("-") 

142 

143 lilfile = parent_d / f"somefolder.tar" 

144 

145 assert compressor.compress(child_d) 

146 

147 assert lilfile.exists() 

148 

149 unzipped_d = parent_d / "somefolder" 

150 assert compressor.decompress(lilfile) 

151 

152 unzipped = unzipped_d / "raw.txt" 

153 assert unzipped.read_text() == DATA 

154 

155 

156def test_best(): 

157 compressor = Compression.best() 

158 assert isinstance(compressor, Pigz) 

159 

160 

161def test_compress_decompress_without_filename(): 

162 c = Compression.best() 

163 

164 with tempfile.TemporaryDirectory() as d, chdir(d): 

165 p = Path(d) 

166 t = p / "file.txt" 

167 t.write_text("--------------------") 

168 

169 assert c.compress(".") 

170 assert c.compress(t) 

171 

172 assert c.decompress(p.with_suffix(".tgz")) 

173 assert c.decompress(t.with_suffix(".txt.gz"))