Coverage for tests\test_torrent.py: 100%
123 statements
« prev ^ index » next coverage.py v7.3.0, created at 2023-08-27 21:50 -0700
« prev ^ index » next coverage.py v7.3.0, created at 2023-08-27 21:50 -0700
1#! /usr/bin/python3
2# -*- coding: utf-8 -*-
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 torrent module.
21"""
22import os
24import pytest
26from tests import dir1, dir2, rmpath, tempfile, torrents
27from torrentfile.mixins import ProgMixin, waiting
28from torrentfile.torrent import MetaFile
29from torrentfile.utils import MissingPathError
32def test_fixtures():
33 """
34 Test pytest fixtures.
35 """
36 assert dir1 and dir2
39@pytest.mark.parametrize("version", torrents())
40def test_torrentfile_missing_path(version):
41 """
42 Test missing path error exception.
43 """
44 try:
45 version()
46 except MissingPathError:
47 assert True
50def test_metafile_assemble(dir1):
51 """
52 Test assembling base metafile exception.
53 """
54 metafile = MetaFile(path=dir1)
55 try:
56 metafile.assemble()
57 except NotImplementedError:
58 assert True
61@pytest.mark.parametrize("version", torrents())
62def test_torrentfile_one_empty(dir2, version):
63 """
64 Test creating a torrent meta file with given directory plus extra.
65 """
66 a = next(os.walk(dir2))
67 if len(a[-1]) > 0:
68 with open(os.path.join(a[0], a[-1][0]), "w", encoding="utf-8") as _:
69 pass
70 args = {
71 "path": dir2,
72 "comment": "somecomment",
73 "announce": "announce",
74 }
75 torrent = version(**args)
76 assert torrent.meta["announce"] == "announce"
79@pytest.mark.parametrize("version", torrents())
80def test_torrentfile_extra(dir2, version):
81 """
82 Test creating a torrent meta file with given directory plus extra.
83 """
85 def walk(item):
86 """
87 Edit files in directory structure.
88 """
89 if item.is_file():
90 with open(item, "ab") as binfile:
91 binfile.write(bytes(1000))
92 elif item.is_dir():
93 for sub in item.iterdir():
94 walk(sub)
96 walk(dir2)
97 args = {
98 "path": dir2,
99 "comment": "somecomment",
100 "announce": "announce",
101 }
102 torrent = version(**args)
103 assert torrent.meta["announce"] == "announce"
106@pytest.mark.parametrize("num", list(range(17, 25)))
107@pytest.mark.parametrize("piece_length", [2**i for i in range(14, 18)])
108@pytest.mark.parametrize("version", torrents())
109def test_torrentfile_single(version, num, piece_length, capsys):
110 """
111 Test creating a torrent file from a single file contents.
112 """
113 tfile = tempfile(exp=num)
114 with capsys.disabled():
115 version.set_callback(print)
116 outfile = str(tfile) + ".torrent"
117 args = {
118 "path": tfile,
119 "comment": "somecomment",
120 "announce": "announce",
121 "piece_length": piece_length,
122 "outfile": outfile,
123 }
124 trent = version(**args)
125 trent.write()
126 assert os.path.exists(outfile)
127 rmpath(tfile, str(tfile) + ".torrent")
130@pytest.mark.parametrize("size", list(range(17, 25)))
131@pytest.mark.parametrize("piece_length", [2**i for i in range(14, 18)])
132@pytest.mark.parametrize("version", torrents())
133def test_torrentfile_single_extra(version, size, piece_length):
134 """
135 Test creating a torrent file from a single file contents plus extra.
136 """
137 tfile = tempfile(exp=size)
138 with open(tfile, "ab") as binfile:
139 binfile.write(bytes(str(tfile).encode("utf-8")))
140 outfile = str(tfile) + ".torrent"
141 args = {
142 "path": tfile,
143 "comment": "somecomment",
144 "announce": "announce",
145 "piece_length": piece_length,
146 "outfile": outfile,
147 }
148 torrent = version(**args)
149 torrent.write()
150 assert os.path.exists(outfile)
151 rmpath(tfile, outfile)
154@pytest.mark.parametrize("sze", list(range(17, 25)))
155@pytest.mark.parametrize("piecelength", [2**i for i in range(14, 18)])
156@pytest.mark.parametrize("ver", torrents())
157def test_torrentfile_single_under(ver, sze, piecelength):
158 """
159 Test creating a torrent file from less than a single file contents.
160 """
161 tfile = tempfile(exp=sze)
162 with open(tfile, "rb") as binfile:
163 data = binfile.read()
164 with open(tfile, "wb") as binfile:
165 binfile.write(data[:-(2**9)])
166 outfile = str(tfile) + ".torrent"
167 kwargs = {
168 "path": tfile,
169 "comment": "somecomment",
170 "announce": "announce",
171 "piece_length": piecelength,
172 "outfile": outfile,
173 }
174 torrent = ver(**kwargs)
175 outfile, _ = torrent.write()
176 assert os.path.exists(outfile)
177 rmpath(tfile, outfile)
180def test_create_cwd_fail():
181 """Test cwd argument with create command failure."""
183 class SuFile:
184 """A mock admin file."""
186 @staticmethod
187 def __fspath__():
188 raise PermissionError
190 def __str__(self):
191 return "SuFile"
193 tfile = tempfile()
194 torrent = MetaFile(path=tfile)
195 sufile = SuFile()
196 try:
197 assert torrent.write(outfile=sufile)
198 except PermissionError:
199 assert True
200 rmpath(tfile)
203def test_waiting_mixin():
204 """
205 Test waiting function.
206 """
207 msg = "Testing message"
208 lst = []
209 timeout = 3
210 waiting(msg, lst, timeout=timeout)
211 assert len(lst) == 0
214@pytest.mark.parametrize("version", torrents())
215@pytest.mark.parametrize("progress", [0, 1, 2])
216def test_mbtorrent(version, progress):
217 """
218 Test torrent creation for file size larger than 10MB.
219 """
220 tfile = tempfile(exp=26)
221 outfile = str(tfile) + ".torrent"
222 args = {
223 "path": tfile,
224 "progress": progress,
225 "piece_length": "14",
226 "outfile": outfile,
227 "align": True,
228 }
229 torrent = version(**args)
230 outfile, _ = torrent.write()
231 assert os.path.exists(outfile)
232 rmpath(tfile, outfile)
235@pytest.mark.parametrize("params", [(10, 12), (10, 15), (20, 25), (28, 32)])
236def test_progress_bar(params):
237 """Testing the prog mixin with various sizes."""
238 increment, total = params
239 progress = ProgMixin()
240 progbar = progress.get_progress_tracker(1 << total, "some/fake/path")
241 while progbar.state < total:
242 progbar.update(1 << increment)
243 assert progbar.state >= total