Coverage for tests\test_edit.py: 100%
111 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 the edit torrent feature.
21"""
23import sys
25import pyben
26import pytest
28from tests import dir1, dir2, metafile2
29from torrentfile.cli import main
30from torrentfile.edit import edit_torrent
33def test_fix():
34 """
35 Testing dir fixtures.
36 """
37 assert dir2 and metafile2 and dir1
40@pytest.mark.parametrize(
41 "announce", [["urla"], ["urlb", "urlc"], ["urla", "urlb", "urlc"]])
42def test_edit_torrent(metafile2, announce):
43 """
44 Test edit torrent with announce param.
45 """
46 edits = {"announce": announce}
47 data = edit_torrent(metafile2, edits)
48 meta = pyben.load(metafile2)
49 assert data == meta
50 assert data["announce-list"] == [announce]
53@pytest.mark.parametrize("announce", ["urla", "urlb urlc", "urla urlb urlc"])
54def test_edit_torrent_str(metafile2, announce):
55 """
56 Test edit torrent with announce param as string.
57 """
58 edits = {"announce": announce}
59 data = edit_torrent(metafile2, edits)
60 meta = pyben.load(metafile2)
61 assert data == meta
62 assert data["announce-list"] == [announce.split()]
65@pytest.mark.parametrize("url_list", ["urla", "urlb urlc", "urla urlb urlc"])
66def test_edit_urllist_str(metafile2, url_list):
67 """
68 Test edit torrent with webseed param.
69 """
70 edits = {"url-list": url_list}
71 data = edit_torrent(metafile2, edits)
72 meta = pyben.load(metafile2)
73 assert data == meta
74 assert data["url-list"] == url_list.split()
77@pytest.mark.parametrize("httpseeds", ["urla", "urlb urlc", "urla urlb urlc"])
78def test_edit_httpseeds_str(metafile2, httpseeds):
79 """
80 Test edit torrent with webseed param.
81 """
82 edits = {"httpseeds": httpseeds}
83 data = edit_torrent(metafile2, edits)
84 meta = pyben.load(metafile2)
85 assert data == meta
86 assert data["httpseeds"] == httpseeds.split()
89@pytest.mark.parametrize(
90 "url_list", [["urla"], ["urlb", "urlc"], ["urla", "urlb", "urlc"]])
91def test_edit_urllist(metafile2, url_list):
92 """
93 Test edit torrent with webseed param as string.
94 """
95 edits = {"url-list": url_list}
96 data = edit_torrent(metafile2, edits)
97 meta = pyben.load(metafile2)
98 assert data == meta
99 assert data["url-list"] == url_list
102@pytest.mark.parametrize(
103 "httpseed", [["urla"], ["urlb", "urlc"], ["urla", "urlb", "urlc"]])
104def test_edit_httpseeds(metafile2, httpseed):
105 """
106 Test edit torrent with webseed param as string.
107 """
108 edits = {"httpseeds": httpseed}
109 data = edit_torrent(metafile2, edits)
110 meta = pyben.load(metafile2)
111 assert data == meta
112 assert data["httpseeds"] == httpseed
115@pytest.mark.parametrize("comment", ["COMMENT", "COMIT", "MITCO"])
116def test_edit_comment(metafile2, comment):
117 """
118 Test edit torrent with comment param.
119 """
120 edits = {"comment": comment}
121 data = edit_torrent(metafile2, edits)
122 meta = pyben.load(metafile2)
123 assert data == meta
124 assert data["info"]["comment"] == comment
127@pytest.mark.parametrize("source", ["SomeSource", "NoSouce", "MidSource"])
128def test_edit_source(metafile2, source):
129 """
130 Test edit torrent with source param.
131 """
132 edits = {"source": source}
133 data = edit_torrent(metafile2, edits)
134 meta = pyben.load(metafile2)
135 assert data == meta
136 assert data["info"]["source"] == source
139def test_edit_private_true(metafile2):
140 """
141 Test edit torrent with private param.
142 """
143 edits = {"private": "1"}
144 data = edit_torrent(metafile2, edits)
145 meta = pyben.load(metafile2)
146 assert data == meta
147 assert data["info"]["private"] == 1
150def test_edit_private_false(metafile2):
151 """
152 Test edit torrent with private param False.
153 """
154 edits = {"private": ""}
155 data = edit_torrent(metafile2, edits)
156 meta = pyben.load(metafile2)
157 assert data == meta
158 assert "private" not in data["info"]
161def test_edit_none(metafile2):
162 """
163 Test edit torrent with None for all params.
164 """
165 edits = {
166 "announce": None,
167 "url-list": None,
168 "comment": None,
169 "source": None,
170 "private": None,
171 }
172 data = pyben.load(metafile2)
173 edited = edit_torrent(metafile2, edits)
174 meta = pyben.load(metafile2)
175 assert data == meta == edited
178def test_edit_removal(metafile2):
179 """
180 Test edit torrent with empty for all params.
181 """
182 edits = {
183 "announce": "",
184 "url-list": "",
185 "httpseeds": "",
186 "comment": "",
187 "source": "",
188 "private": "",
189 }
190 data = edit_torrent(metafile2, edits)
191 meta = pyben.load(metafile2)
192 assert data == meta
195@pytest.mark.parametrize("comment", ["commenta", "commentb", "commentc"])
196@pytest.mark.parametrize("source", ["sourcea", "sourceb", "sourcec"])
197@pytest.mark.parametrize("announce", [["url1", "url2", "url3"], ["url1"]])
198@pytest.mark.parametrize("webseed", [["ftp1"], ["ftpa", "ftpb"]])
199@pytest.mark.parametrize("httpseed", [["ftp1"], ["ftpa", "ftpb"]])
200def test_edit_cli(metafile2, comment, source, announce, webseed, httpseed):
201 """
202 Test edit torrent with all params on cli.
203 """
204 sys.argv = [
205 "torrentfile",
206 "edit",
207 metafile2,
208 "--comment",
209 comment,
210 "--source",
211 source,
212 "--web-seed",
213 webseed,
214 "--http-seed",
215 httpseed,
216 "--tracker",
217 announce,
218 "--private",
219 ]
220 main()
221 meta = pyben.load(metafile2)
222 info = meta["info"]
223 assert comment == info.get("comment")
224 assert source == info.get("source")
225 assert info.get("private") == 1
226 assert meta["announce-list"] == [[announce]]
227 assert meta["url-list"] == [webseed]
230def test_metafile_edit_with_unicode(metafile2):
231 """
232 Test if editing full unicode works as it should.
233 """
234 edits = {
235 "comment": "丂七万丈三与丏丑丒专且丕世丗両丢丣两严丩个丫丬中丮丯.torrent",
236 "source": "丂七万丏丑严丩个丫丬中丮丯",
237 }
238 data = edit_torrent(metafile2, edits)
239 meta = pyben.load(metafile2)
240 com1 = data["info"]["comment"]
241 com2 = meta["info"]["comment"]
242 msg = edits["comment"]
243 assert com1 == com2 == msg