-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
202 lines (160 loc) · 6.21 KB
/
tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
from unittest.mock import MagicMock, Mock, patch, call
import unittest
from script import check_torrents
from script import torrent_applicable_for_deletion
from qbittorrentapi import TrackerStatus
class TestQbitDeleter(unittest.TestCase):
@patch("script.shutil.disk_usage")
@patch("script.torrent_applicable_for_deletion")
def test_disk_limit_reached_deletes_appropriate_torrents(
self, mock_torrent_applicable_for_deletion, mock_shutil_disk_usage
):
mock_torrent_applicable_for_deletion.return_value = True
mock_shutil_disk_usage.return_value.free = 500
torrent1 = MagicMock(name="Torrent 1", total_size=1000, hash="hash1")
torrent2 = MagicMock(name="Torrent 2", total_size=1000, hash="hash2")
torrent3 = MagicMock(name="Torrent 3", total_size=400, hash="hash3")
qbt_client_mock = MagicMock()
qbt_client_mock.torrents_info = MagicMock(
return_value=[torrent1, torrent2, torrent3]
)
args = Mock()
args.freespace = 2000
args.freespacepath = "/"
args.dryrun = False
args.deletefiles = True
args.deleteunregistered = False
check_torrents(qbt_client_mock, args)
self.assertEqual(qbt_client_mock.torrents_delete.call_count, 2)
qbt_client_mock.torrents_delete.assert_has_calls(
[
call(delete_files=True, torrent_hashes="hash1"),
call(delete_files=True, torrent_hashes="hash2"),
]
)
def test_excluded_tag_is_not_applicable_for_deletion(
self,
):
torrent = MagicMock(
name="Torrent", ratio=0.5, hash="hash", seeding_time=9999, tags=["excluded"]
)
qbt_client_mock = MagicMock()
args = Mock()
args.excludetag = "excluded"
result = torrent_applicable_for_deletion(torrent, qbt_client_mock, args)
self.assertEqual(result, False)
def test_enough_ratio_tracker_working_is_applicable_for_deletion(self):
torrent = MagicMock(
name="Torrent",
total_size=1000,
ratio=1,
hash="hash",
trackers=[
MagicMock(
status=TrackerStatus.WORKING, url="http://tracker.com", msg=""
),
MagicMock(
status=TrackerStatus.NOT_WORKING,
url="http://tracker2.com",
msg="stream truncated",
),
],
)
qbt_client_mock = MagicMock()
args = Mock()
args.ratio = 1
args.restrict_to_working = True
result = torrent_applicable_for_deletion(torrent, qbt_client_mock, args)
self.assertEqual(result, True)
def test_enough_ratio_tracker_not_working_enabled_is_not_applicable_for_deletion(
self,
):
torrent = MagicMock(
name="Torrent",
total_size=1000,
ratio=1,
hash="hash",
trackers=[
MagicMock(
status=TrackerStatus.NOT_WORKING,
url="http://tracker.com",
msg="stream truncated",
)
],
)
qbt_client_mock = MagicMock()
args = Mock()
args.ratio = 1
args.restrict_to_working = False
result = torrent_applicable_for_deletion(torrent, qbt_client_mock, args)
self.assertEqual(result, True)
def test_enough_ratio_tracker_not_working_is_not_applicable_for_deletion(self):
torrent = MagicMock(
name="Torrent",
total_size=1000,
ratio=1,
hash="hash",
trackers=[
MagicMock(
status=TrackerStatus.NOT_WORKING,
url="http://tracker.com",
msg="stream truncated",
)
],
)
qbt_client_mock = MagicMock()
args = Mock()
args.ratio = 1
args.restrict_to_working = True
result = torrent_applicable_for_deletion(torrent, qbt_client_mock, args)
self.assertEqual(result, False)
def test_enough_ratio_tracker_reports_unregistered_is_applicable_for_deletion(self):
torrent = MagicMock(
name="Torrent",
total_size=1000,
ratio=1,
hash="hash",
trackers=[
MagicMock(
status=TrackerStatus.NOT_WORKING,
url="http://tracker.com",
msg="Unregistered torrent",
)
],
)
qbt_client_mock = MagicMock()
args = Mock()
args.ratio = 1
args.restrict_to_working = True
result = torrent_applicable_for_deletion(torrent, qbt_client_mock, args)
self.assertEqual(result, True)
def test_enough_seed_time_is_applicable_for_deletion(self):
torrent = MagicMock(name="Torrent", ratio=0.5, hash="hash", seeding_time=9999)
qbt_client_mock = MagicMock()
args = Mock()
args.ratio = 1
args.seedduration = 1000
result = torrent_applicable_for_deletion(torrent, qbt_client_mock, args)
self.assertEqual(result, True)
def test_single_file_enough_seed_time_is_applicable_for_deletion(self):
torrent = MagicMock(name="Torrent", ratio=0.5, hash="hash", seeding_time=9999)
qbt_client_mock = MagicMock()
args = Mock()
args.ratio = 1
args.seedduration = 0
args.singleseedduration = 1000
qbt_client_mock.torrents_files = Mock(return_value=[1])
result = torrent_applicable_for_deletion(torrent, qbt_client_mock, args)
self.assertEqual(result, True)
def test_multi_file_enough_seed_time_is_applicable_for_deletion(self):
torrent = MagicMock(name="Torrent", ratio=0.5, hash="hash", seeding_time=9999)
qbt_client_mock = MagicMock()
args = Mock()
args.ratio = 1
args.seedduration = 0
args.collectionseedduration = 1000
qbt_client_mock.torrents_files = Mock(return_value=[1, 2])
result = torrent_applicable_for_deletion(torrent, qbt_client_mock, args)
self.assertEqual(result, True)
if __name__ == "__main__":
unittest.main()