Coverage for tests\test_interactive.py: 100%

70 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""" 

20Testing functions for the command line interface. 

21""" 

22 

23import os 

24 

25import pyben 

26import pytest 

27 

28from tests import file1, file2, filemeta2, torrents 

29from torrentfile.interactive import select_action 

30from torrentfile.utils import normalize_piece_length 

31 

32MOCK = "torrentfile.interactive.get_input" 

33 

34 

35def test_fixtures(): 

36 """ 

37 Test the fixtures used in module. 

38 """ 

39 assert filemeta2 and file1 and file2 

40 

41 

42def test_interactive_create(monkeypatch, file1): 

43 """ 

44 Test creating torrent interactively. 

45 """ 

46 mapping = [ 

47 "create", 

48 "", 

49 "", 

50 "", 

51 "", 

52 "", 

53 "", 

54 "", 

55 file1, 

56 str(file1) + ".torrent", 

57 "", 

58 ] 

59 it = iter(mapping) 

60 monkeypatch.setattr(MOCK, lambda *_: next(it)) 

61 select_action() 

62 assert os.path.exists(str(file1) + ".torrent") 

63 

64 

65@pytest.mark.parametrize("version", ["1", "2", "3"]) 

66@pytest.mark.parametrize("piece_length", ["23", "18", "131072"]) 

67@pytest.mark.parametrize("announce", ["url1", "urla urlb urlc"]) 

68@pytest.mark.parametrize("url_list", ["ftp url2", "ftp1 ftp2 ftp3"]) 

69@pytest.mark.parametrize("comment", ["Some Comment", "No Comment"]) 

70@pytest.mark.parametrize("source", ["Do", "Ra", "Me"]) 

71def test_inter_create_full( 

72 file1, 

73 piece_length, 

74 announce, 

75 comment, 

76 source, 

77 url_list, 

78 version, 

79 monkeypatch, 

80): 

81 """ 

82 Test creating torrent interactively with many parameters. 

83 """ 

84 mapping = [ 

85 "create", 

86 piece_length, 

87 announce, 

88 url_list, 

89 url_list, 

90 comment, 

91 source, 

92 "Y", 

93 file1, 

94 str(file1) + ".torrent", 

95 version, 

96 ] 

97 it = iter(mapping) 

98 monkeypatch.setattr(MOCK, lambda *_: next(it)) 

99 select_action() 

100 meta = pyben.load(str(file1) + ".torrent") 

101 assert meta["info"]["source"] == source 

102 assert meta["info"]["piece length"] == normalize_piece_length(piece_length) 

103 assert meta["info"]["comment"] == comment 

104 assert meta["url-list"] == url_list.split() 

105 

106 

107@pytest.mark.parametrize("announce", ["url1"]) 

108@pytest.mark.parametrize("url_list", ["ftp url2", "ftp1 ftp2 ftp3"]) 

109@pytest.mark.parametrize("comment", ["Some Comment", "No Comment"]) 

110@pytest.mark.parametrize("source", ["Fa", "So", "La"]) 

111def test_inter_edit_full(filemeta2, announce, comment, source, url_list, 

112 monkeypatch): 

113 """ 

114 Test editing torrent file interactively. 

115 """ 

116 seq = [ 

117 "edit", 

118 filemeta2, 

119 "4", 

120 announce, 

121 "1", 

122 comment, 

123 "2", 

124 source, 

125 "5", 

126 url_list, 

127 "", 

128 "6", 

129 "Y", 

130 "DONE", 

131 ] 

132 it = iter(seq) 

133 monkeypatch.setattr(MOCK, lambda *_: next(it)) 

134 select_action() 

135 meta1 = pyben.load(filemeta2) 

136 assert meta1["info"]["source"] == source 

137 assert meta1["info"]["comment"] == comment 

138 assert meta1["url-list"] == url_list.split() 

139 assert meta1["info"]["private"] == 1 

140 

141 

142@pytest.mark.parametrize("announce", ["urla urlb urlc", "urld url2"]) 

143@pytest.mark.parametrize("urllist", ["ftp url2", "ftp1 ftp2 ftp3"]) 

144@pytest.mark.parametrize("cmnt", ["Some Comment"]) 

145@pytest.mark.parametrize("srce", ["Do", "Ra"]) 

146def test_inter_edit_cli(filemeta2, announce, cmnt, srce, urllist, monkeypatch): 

147 """ 

148 Test editing torrent interactively from CLI. 

149 """ 

150 seq = [ 

151 "edit", 

152 filemeta2, 

153 "4", 

154 announce, 

155 "1", 

156 cmnt, 

157 "2", 

158 srce, 

159 "5", 

160 urllist, 

161 urllist, 

162 "6", 

163 "Y", 

164 "DONE", 

165 ] 

166 it = iter(seq) 

167 monkeypatch.setattr(MOCK, lambda *_: next(it)) 

168 select_action() 

169 meta2 = pyben.load(filemeta2) 

170 assert meta2["info"]["source"] == srce 

171 assert meta2["info"]["comment"] == cmnt 

172 assert meta2["url-list"] == urllist.split() 

173 assert meta2["info"]["private"] == 1 

174 

175 

176@pytest.mark.parametrize("torrentclass", torrents()) 

177def test_inter_recheck(torrentclass, monkeypatch, file1): 

178 """ 

179 Test interactive recheck function. 

180 """ 

181 outpath = str(file1) + ".torrent" 

182 torrent = torrentclass(path=file1, outfile=outpath) 

183 filemeta, _ = torrent.write(outfile=outpath) 

184 seq = ["recheck", filemeta, str(file1)] 

185 it = iter(seq) 

186 monkeypatch.setattr(MOCK, lambda *_: next(it)) 

187 result = select_action() 

188 assert result == 100