-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
159 lines (142 loc) · 6.6 KB
/
test.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
# pyfocusstackfo — Utility to reallocate photos taken for focus stacking into folders.
# Copyright (C) 2023 Ilia Baidakov <[email protected]>
# This program is free software: you can redistribute it and/or modify it under the
# terms of the GNU 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 General Public License for more details.
# You should have received a copy of the GNU General Public License along with this
# program. If not, see <https://www.gnu.org/licenses/>.
"""This module provide simple tests units."""
import contextlib
import io
import os
import shutil
import sys
import unittest
from unittest.mock import patch
from zipfile import ZipFile
import pyfocusstackfo
ROOT_DIR = os.path.dirname(__file__)
TEST_DIR = 'test'
TEMP_DIR = 'tempjpg'
answers_1 = iter([os.path.join(ROOT_DIR, TEST_DIR), TEMP_DIR])
answers_2 = iter([os.path.join(ROOT_DIR, TEST_DIR), TEMP_DIR])
answers_3 = iter([os.path.join(ROOT_DIR, TEST_DIR), TEMP_DIR])
answers_4 = iter([os.path.join(ROOT_DIR, TEST_DIR), TEMP_DIR])
answers_5 = iter([os.path.join(ROOT_DIR, TEST_DIR), TEMP_DIR])
answers_6 = iter(['', os.path.join(TEST_DIR, TEMP_DIR)])
answers_7 = iter([ROOT_DIR, TEST_DIR])
dialog_arg = ['pyfocusstackfo.py', '-l']
class Test(unittest.TestCase):
"""Unit test Test Case. All cases are with:
MINSTACKLEN = 5, MAXDELTA = 2, BIG_STRANGE_STACKLEN = 10"""
@patch('builtins.input', lambda msg: next(answers_1))
def test_1(self):
"""
This is normal case with 97 files And 9 stacks.
"""
with ZipFile(os.path.join(ROOT_DIR, TEST_DIR, 'test_97f.zip'), 'r') as myzip:
myzip.extractall(path=os.path.join(ROOT_DIR, TEST_DIR, TEMP_DIR))
buf = io.StringIO()
with contextlib.redirect_stdout(buf):
with patch.object(sys, 'argv', dialog_arg):
pyfocusstackfo.main()
self.assertIn("Got 97 timestamps in JPGs", buf.getvalue())
self.assertIn("9 folders created\n64 files moved", buf.getvalue())
shutil.rmtree(os.path.join(ROOT_DIR, TEST_DIR, TEMP_DIR))
@patch('builtins.input', lambda msg: next(answers_2))
def test_2(self):
"""
This is the test where no stacks in jpgs across 33 files.
"""
with ZipFile(os.path.join(ROOT_DIR, TEST_DIR, 'test_no_st.zip'), 'r') as myzip:
myzip.extractall(path=os.path.join(ROOT_DIR, TEST_DIR, TEMP_DIR))
buf = io.StringIO()
with contextlib.redirect_stdout(buf):
with patch.object(sys, 'argv', dialog_arg):
with self.assertRaises(SystemExit):
pyfocusstackfo.main()
self.assertIn("Got 33 timestamps in JPGs", buf.getvalue())
self.assertIn("No stacks here! Exit", buf.getvalue())
shutil.rmtree(os.path.join(ROOT_DIR, TEST_DIR, TEMP_DIR))
@patch('builtins.input', lambda msg: next(answers_3))
def test_3(self):
"""
This is the test with big stack with qty files more than BIG_STRANGE_STACKLEN.
"""
with ZipFile(
os.path.join(ROOT_DIR, TEST_DIR, 'test_strange_big.zip'), 'r'
) as myzip:
myzip.extractall(path=os.path.join(ROOT_DIR, TEST_DIR, TEMP_DIR))
buf = io.StringIO()
with contextlib.redirect_stdout(buf):
with patch.object(sys, 'argv', dialog_arg):
pyfocusstackfo.main()
self.assertIn("Strange long stack (16) elements", buf.getvalue())
self.assertIn("1 folders created\n16 files moved", buf.getvalue())
shutil.rmtree(os.path.join(ROOT_DIR, TEST_DIR, TEMP_DIR))
@patch('builtins.input', lambda msg: next(answers_4))
def test_4(self):
"""
This test with stacked files in the end of file list.
"""
with ZipFile(
os.path.join(ROOT_DIR, TEST_DIR, 'test_st_in_end.zip'), 'r'
) as myzip:
myzip.extractall(path=os.path.join(ROOT_DIR, TEST_DIR, TEMP_DIR))
buf = io.StringIO()
with contextlib.redirect_stdout(buf):
with patch.object(sys, 'argv', dialog_arg):
pyfocusstackfo.main()
self.assertIn("Got 22 timestamps in JPGs", buf.getvalue())
self.assertIn("Stack size 5 files: 1 stacks", buf.getvalue())
self.assertIn("1 folders created\n5 files moved", buf.getvalue())
shutil.rmtree(os.path.join(ROOT_DIR, TEST_DIR, TEMP_DIR))
@patch('builtins.input', lambda msg: next(answers_5))
def test_5(self):
"""
This test with stacked files in the beginning of file list.
"""
with ZipFile(
os.path.join(ROOT_DIR, TEST_DIR, 'test_st_in_begin.zip'), 'r'
) as myzip:
myzip.extractall(path=os.path.join(ROOT_DIR, TEST_DIR, TEMP_DIR))
buf = io.StringIO()
with contextlib.redirect_stdout(buf):
with patch.object(sys, 'argv', dialog_arg):
pyfocusstackfo.main()
self.assertIn(
"Got 9 timestamps in JPGs\nFROM: 2023-12-02 13:30:39", buf.getvalue()
)
self.assertIn("1 folders created\n5 files moved", buf.getvalue())
shutil.rmtree(os.path.join(ROOT_DIR, TEST_DIR, TEMP_DIR))
# This can not be tested on github
# @patch('builtins.input', lambda msg: next(answers_6))
# def test_6(self):
# """
# This tests empty input on first prompt on data of test_1
# """
# with ZipFile(os.path.join(ROOT_DIR, TEST_DIR, 'test_97f.zip'), 'r') as myzip:
# myzip.extractall(path=os.path.join(ROOT_DIR, TEST_DIR, TEMP_DIR))
# buf = io.StringIO()
# with contextlib.redirect_stdout(buf):
# with patch.object(sys, 'argv', dialog_arg):
# pyfocusstackfo.main()
# self.assertIn("Got 97 timestamps in JPGs", buf.getvalue())
# self.assertIn("9 folders created\n64 files moved", buf.getvalue())
# shutil.rmtree(os.path.join(ROOT_DIR, TEST_DIR, TEMP_DIR))
@patch('builtins.input', lambda msg: next(answers_7))
def test_7(self):
"""
This test case with no files in folder.
"""
buf = io.StringIO()
with contextlib.redirect_stdout(buf):
with patch.object(sys, 'argv', dialog_arg):
with self.assertRaises(SystemExit):
pyfocusstackfo.main()
self.assertIn("No JPG files in folder! Exit", buf.getvalue())
if __name__ == '__main__':
unittest.main()