-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtests.py
More file actions
152 lines (131 loc) · 4.69 KB
/
tests.py
File metadata and controls
152 lines (131 loc) · 4.69 KB
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
#!/usr/bin/env python3
#
# Copyright (C) 2024-2025 Denis <denis@nzbget.com>
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with the program. If not, see <https://www.gnu.org/licenses/>.
#
import sys
from os.path import dirname
import os
import subprocess
import unittest
import shutil
import json
POSTPROCESS_SUCCESS = 93
POSTPROCESS_NONE = 95
POSTPROCESS_ERROR = 94
root = dirname(__file__)
test_data_dir = root + "/test_data/"
tmp_dir = root + "/tmp/"
dst_dir = root + "/tmp/dst/"
dwn_dir = root + "/tmp/dwn/"
test_file = "movie.mp4"
nzb_name = "TestNZB"
category = "Movies"
host = "127.0.0.1"
username = "TestUser"
password = "TestPassword"
port = "6789"
def get_python():
if os.name == "nt":
return "python"
return "python3"
def run_script():
sys.stdout.flush()
proc = subprocess.Popen(
[get_python(), root + "/main.py"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
env=os.environ.copy(),
)
out, err = proc.communicate()
proc.pid
ret_code = proc.returncode
return (out.decode(), int(ret_code), err.decode())
def set_defaults_env():
# NZBGet global options
os.environ["NZBOP_CONTROLPORT"] = port
os.environ["NZBOP_CONTROLIP"] = host
os.environ["NZBOP_CONTROLUSERNAME"] = username
os.environ["NZBOP_CONTROLPASSWORD"] = password
os.environ["NZBOP_NZBLOG"] = tmp_dir + "/log.txt"
os.environ["NZBPP_TOTALSTATUS"] = "SUCCESS"
os.environ["NZBPO_PASSACTION"] = "PASSACTION"
os.environ["NZBPP_CATEGORY"] = ""
# script options
os.environ["NZBPO_OVERWRITE"] = "yes"
os.environ["NZBPO_CLEANUP"] = "yes"
os.environ["NZBPO_PREVIEW"] = "no"
os.environ["NZBPO_VERBOSE"] = "no"
os.environ["NZBPP_TOTALSTATUS"] = "SUCCESS"
os.environ["NZBPP_DIRECTORY"] = dwn_dir
os.environ["NZBPP_NZBNAME"] = nzb_name
os.environ["NZBOP_TEMPDIR"] = tmp_dir
os.environ["NZBPO_DESTDIR"] = dst_dir
os.environ["NZBPO_EXTENSIONS"] = ".mp4"
os.environ["NZBPO_USECATEGORYDIR"] = "no"
os.environ["NZBPO_USENZBPARENTDIR"] = "no"
os.environ["NZBPO_MINSIZE"] = "0"
class Tests(unittest.TestCase):
def test_do_nothing_if_file_too_small(self):
os.mkdir(tmp_dir)
os.mkdir(dwn_dir)
shutil.copyfile(test_data_dir + test_file, dwn_dir + test_file)
set_defaults_env()
os.environ["NZBPO_MINSIZE"] = "1000"
[_, code, _] = run_script()
shutil.rmtree(tmp_dir)
self.assertEqual(code, POSTPROCESS_NONE)
def test_move_file(self):
os.mkdir(tmp_dir)
os.mkdir(dwn_dir)
shutil.copyfile(test_data_dir + test_file, dwn_dir + test_file)
set_defaults_env()
[_, code, _] = run_script()
self.assertTrue(os.path.exists(dst_dir + test_file))
shutil.rmtree(tmp_dir)
self.assertEqual(code, POSTPROCESS_SUCCESS)
def test_move_file_with_category(self):
os.mkdir(tmp_dir)
os.mkdir(dwn_dir)
shutil.copyfile(test_data_dir + test_file, dwn_dir + test_file)
set_defaults_env()
os.environ["NZBPP_CATEGORY"] = category
os.environ["NZBPO_USECATEGORYDIR"] = "yes"
[_, code, _] = run_script()
self.assertTrue(os.path.exists(dst_dir + category + "/" + test_file))
shutil.rmtree(tmp_dir)
self.assertEqual(code, POSTPROCESS_SUCCESS)
def test_move_file_with_category_and_nzb_parent(self):
os.mkdir(tmp_dir)
os.mkdir(dwn_dir)
shutil.copyfile(test_data_dir + test_file, dwn_dir + test_file)
set_defaults_env()
os.environ["NZBPP_CATEGORY"] = category
os.environ["NZBPO_USENZBPARENTDIR"] = "yes"
os.environ["NZBPO_USECATEGORYDIR"] = "yes"
[_, code, _] = run_script()
self.assertTrue(
os.path.exists(dst_dir + "/" + category + "/" + nzb_name + "/" + test_file)
)
shutil.rmtree(tmp_dir)
self.assertEqual(code, POSTPROCESS_SUCCESS)
def test_manifest(self):
with open(root + "/manifest.json", encoding="utf-8") as file:
try:
json.loads(file.read())
except ValueError as e:
self.fail("manifest.json is not valid.")
if __name__ == "__main__":
unittest.main()