2021-10-31 15:09:40 +01:00
|
|
|
import shutil
|
2021-10-30 12:48:39 +02:00
|
|
|
from click.testing import CliRunner
|
2021-10-31 15:09:40 +01:00
|
|
|
from pathlib import Path
|
2021-10-30 12:48:39 +02:00
|
|
|
import pytest
|
2022-04-17 22:05:13 +02:00
|
|
|
import inquirer
|
2021-10-31 15:09:40 +01:00
|
|
|
|
|
|
|
from ordigi import cli
|
2022-08-27 18:58:27 +02:00
|
|
|
from ordigi.request import Input
|
2021-10-30 12:48:39 +02:00
|
|
|
|
|
|
|
CONTENT = "content"
|
|
|
|
|
2021-10-31 15:09:40 +01:00
|
|
|
ORDIGI_PATH = Path(__file__).parent.parent
|
|
|
|
|
|
|
|
|
|
|
|
def get_arg_options_list(arg_options):
|
|
|
|
arg_options_list = []
|
|
|
|
for opt, arg in arg_options:
|
|
|
|
arg_options_list.append(opt)
|
|
|
|
arg_options_list.append(arg)
|
|
|
|
|
|
|
|
return arg_options_list
|
|
|
|
|
2021-10-30 12:48:39 +02:00
|
|
|
|
|
|
|
class TestOrdigi:
|
|
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
def setup_class(cls, sample_files_paths):
|
|
|
|
cls.runner = CliRunner()
|
|
|
|
cls.src_path, cls.file_paths = sample_files_paths
|
2021-11-12 09:04:53 +01:00
|
|
|
cls.logger_options = (('--verbose', 'DEBUG'),)
|
2021-10-31 15:09:40 +01:00
|
|
|
cls.filter_options = (
|
2021-11-03 21:29:06 +01:00
|
|
|
('--ignore-tags', 'CreateDate'),
|
2021-11-06 16:35:35 +01:00
|
|
|
('--ext', 'jpg'),
|
2021-10-31 15:09:40 +01:00
|
|
|
('--glob', '*'),
|
|
|
|
)
|
|
|
|
cls.sort_options = (
|
|
|
|
'--album-from-folder',
|
2022-04-23 18:39:13 +02:00
|
|
|
'--fill-date-original',
|
2021-10-31 15:09:40 +01:00
|
|
|
'--path-format',
|
|
|
|
'--remove-duplicates',
|
|
|
|
'--use-date-filename',
|
|
|
|
'--use-file-dates',
|
|
|
|
)
|
|
|
|
|
2021-11-13 10:03:53 +01:00
|
|
|
def assert_cli(self, command, attributes, state=0):
|
2021-11-07 12:28:30 +01:00
|
|
|
result = self.runner.invoke(command, [*attributes])
|
2021-11-13 10:03:53 +01:00
|
|
|
assert result.exit_code == state, (command, attributes)
|
2021-10-31 15:09:40 +01:00
|
|
|
|
|
|
|
def assert_options(self, command, bool_options, arg_options, paths):
|
|
|
|
for bool_option in bool_options:
|
|
|
|
self.assert_cli(command, [bool_option, *paths])
|
|
|
|
|
|
|
|
for opt, arg in arg_options:
|
|
|
|
self.assert_cli(command, [opt, arg, *paths])
|
|
|
|
|
|
|
|
def assert_all_options(self, command, bool_options, arg_options, paths):
|
|
|
|
arg_options_list = get_arg_options_list(arg_options)
|
|
|
|
self.assert_cli(command, [
|
|
|
|
*bool_options, *arg_options_list, *paths,
|
|
|
|
])
|
|
|
|
|
2021-11-13 10:03:53 +01:00
|
|
|
def test_commands(self):
|
|
|
|
# Check if fail if path not exist
|
|
|
|
commands = [
|
|
|
|
cli._check,
|
|
|
|
cli._clean,
|
|
|
|
cli._compare,
|
2022-04-17 22:05:13 +02:00
|
|
|
cli._edit,
|
2021-11-13 10:03:53 +01:00
|
|
|
cli._import,
|
|
|
|
cli._init,
|
|
|
|
cli._sort,
|
|
|
|
cli._update,
|
|
|
|
]
|
|
|
|
|
|
|
|
for command in commands:
|
2022-01-29 07:48:33 +01:00
|
|
|
if command.name == 'edit':
|
|
|
|
self.assert_cli(command, ['-k', 'date_original', 'not_exist'], state=1)
|
|
|
|
else:
|
|
|
|
self.assert_cli(command, ['not_exist'], state=1)
|
2021-11-13 10:03:53 +01:00
|
|
|
|
2021-12-05 18:27:04 +01:00
|
|
|
self.assert_cli(cli._clone, ['not_exist'], state=2)
|
|
|
|
|
2022-04-17 22:05:13 +02:00
|
|
|
def test_edit(self, monkeypatch):
|
|
|
|
|
|
|
|
bool_options = ()
|
|
|
|
|
|
|
|
arg_options = (
|
|
|
|
*self.logger_options,
|
|
|
|
*self.filter_options,
|
|
|
|
)
|
|
|
|
|
2022-08-27 18:58:27 +02:00
|
|
|
def mockreturn(self, message):
|
|
|
|
return '03-12-2021 08:12:35'
|
2022-04-17 22:05:13 +02:00
|
|
|
|
2022-08-27 18:58:27 +02:00
|
|
|
monkeypatch.setattr(Input, 'text', mockreturn)
|
2022-04-17 22:05:13 +02:00
|
|
|
|
|
|
|
args = (
|
|
|
|
'--key',
|
|
|
|
'date_original',
|
2022-07-27 03:16:25 +02:00
|
|
|
'--overwrite',
|
2022-04-17 22:05:13 +02:00
|
|
|
str(self.src_path.joinpath('test_exif/photo.png')),
|
|
|
|
str(self.src_path),
|
|
|
|
)
|
|
|
|
|
|
|
|
self.assert_cli(cli._edit, args)
|
|
|
|
|
|
|
|
# self.assert_options(cli._edit, bool_options, arg_options, args)
|
|
|
|
# self.assert_all_options(cli._edit, bool_options, arg_options, args)
|
|
|
|
|
2021-10-31 15:09:40 +01:00
|
|
|
def test_sort(self):
|
|
|
|
bool_options = (
|
|
|
|
# '--interactive',
|
|
|
|
'--dry-run',
|
|
|
|
'--album-from-folder',
|
|
|
|
'--remove-duplicates',
|
|
|
|
'--use-date-filename',
|
|
|
|
'--use-file-dates',
|
|
|
|
'--clean',
|
|
|
|
)
|
|
|
|
|
|
|
|
arg_options = (
|
2021-11-12 09:04:53 +01:00
|
|
|
*self.logger_options,
|
2021-10-31 15:09:40 +01:00
|
|
|
*self.filter_options,
|
|
|
|
('--path-format', '{%Y}/{folder}/{name}.{ext}'),
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
paths = (str(self.src_path),)
|
|
|
|
|
|
|
|
self.assert_cli(cli._sort, paths)
|
|
|
|
|
|
|
|
self.assert_options(cli._sort, bool_options, arg_options, paths)
|
|
|
|
self.assert_all_options(cli._sort, bool_options, arg_options, paths)
|
|
|
|
|
2021-12-05 18:27:04 +01:00
|
|
|
def test_clone(self, tmp_path):
|
|
|
|
|
|
|
|
arg_options = (
|
|
|
|
*self.logger_options,
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
paths = (str(self.src_path), str(tmp_path))
|
|
|
|
|
|
|
|
self.assert_cli(cli._init, [str(self.src_path)])
|
|
|
|
self.assert_cli(cli._clone, ['--dry-run', '--verbose', 'DEBUG', *paths])
|
|
|
|
self.assert_cli(cli._clone, paths)
|
|
|
|
|
|
|
|
|
2021-10-31 15:09:40 +01:00
|
|
|
def assert_init(self):
|
2021-11-12 09:04:53 +01:00
|
|
|
for opt, arg in self.logger_options:
|
|
|
|
self.assert_cli(cli._init, [opt, arg, str(self.src_path)])
|
2021-10-31 15:09:40 +01:00
|
|
|
|
|
|
|
def assert_update(self):
|
|
|
|
file_path = Path(ORDIGI_PATH, 'samples/test_exif/photo.cr2')
|
|
|
|
dest_path = self.src_path / 'photo_moved.cr2'
|
|
|
|
shutil.copyfile(file_path, dest_path)
|
2021-11-12 09:04:53 +01:00
|
|
|
for opt, arg in self.logger_options:
|
|
|
|
self.assert_cli(cli._update, [opt, arg, str(self.src_path)])
|
2022-07-27 03:28:24 +02:00
|
|
|
self.assert_cli(cli._update, ['--checksum', str(self.src_path)])
|
2021-10-31 15:09:40 +01:00
|
|
|
|
|
|
|
def assert_check(self):
|
2021-11-12 09:04:53 +01:00
|
|
|
for opt, arg in self.logger_options:
|
|
|
|
self.assert_cli(cli._check, [opt, arg, str(self.src_path)])
|
2021-10-31 15:09:40 +01:00
|
|
|
|
|
|
|
def assert_clean(self):
|
|
|
|
bool_options = (
|
|
|
|
# '--interactive',
|
|
|
|
'--dry-run',
|
|
|
|
'--delete-excluded',
|
|
|
|
'--folders',
|
|
|
|
'--path-string',
|
|
|
|
'--remove-duplicates',
|
|
|
|
)
|
|
|
|
|
|
|
|
arg_options = (
|
2021-11-12 09:04:53 +01:00
|
|
|
*self.logger_options,
|
2021-10-31 15:09:40 +01:00
|
|
|
*self.filter_options,
|
|
|
|
('--dedup-regex', r'\d{4}-\d{2}'),
|
|
|
|
)
|
|
|
|
|
|
|
|
paths = ('test_exif', str(self.src_path))
|
|
|
|
self.assert_cli(cli._clean, paths)
|
|
|
|
|
|
|
|
paths = (str(self.src_path),)
|
|
|
|
self.assert_cli(cli._clean, paths)
|
|
|
|
|
|
|
|
self.assert_options(cli._clean, bool_options, arg_options, paths)
|
|
|
|
self.assert_all_options(cli._clean, bool_options, arg_options, paths)
|
|
|
|
|
|
|
|
def test_init_update_check_clean(self):
|
|
|
|
self.assert_init()
|
|
|
|
self.assert_update()
|
|
|
|
self.assert_clean()
|
|
|
|
|
|
|
|
def test_import(self, tmp_path):
|
|
|
|
bool_options = (
|
|
|
|
# '--interactive',
|
|
|
|
'--dry-run',
|
|
|
|
'--album-from-folder',
|
|
|
|
'--remove-duplicates',
|
|
|
|
'--use-date-filename',
|
|
|
|
'--use-file-dates',
|
|
|
|
'--copy',
|
|
|
|
)
|
|
|
|
|
|
|
|
arg_options = (
|
2021-11-12 09:04:53 +01:00
|
|
|
*self.logger_options,
|
2022-04-17 10:18:32 +02:00
|
|
|
('--exclude', '.DS_Store'),
|
2021-10-31 15:09:40 +01:00
|
|
|
*self.filter_options,
|
|
|
|
('--path-format', '{%Y}/{folder}/{stem}.{ext}'),
|
|
|
|
)
|
|
|
|
|
|
|
|
paths = (str(self.src_path), str(tmp_path))
|
|
|
|
|
|
|
|
result = self.runner.invoke(cli._import, ['--copy', *paths])
|
|
|
|
assert result.exit_code == 0
|
|
|
|
|
|
|
|
self.assert_options(cli._import, bool_options, arg_options, paths)
|
|
|
|
self.assert_all_options(cli._import, bool_options, arg_options, paths)
|
|
|
|
|
|
|
|
def test_compare(self):
|
|
|
|
bool_options = (
|
|
|
|
# '--interactive',
|
|
|
|
'--dry-run',
|
|
|
|
'--find-duplicates',
|
|
|
|
'--remove-duplicates',
|
|
|
|
)
|
|
|
|
|
|
|
|
arg_options = (
|
2021-11-12 09:04:53 +01:00
|
|
|
*self.logger_options,
|
2021-10-31 15:09:40 +01:00
|
|
|
*self.filter_options,
|
|
|
|
# ('--similar-to', ''),
|
|
|
|
('--similarity', '65'),
|
|
|
|
)
|
|
|
|
|
|
|
|
paths = (str(self.src_path),)
|
2021-10-30 12:48:39 +02:00
|
|
|
|
2022-01-29 07:48:33 +01:00
|
|
|
# Workaround
|
|
|
|
self.assert_cli(cli._update, paths)
|
|
|
|
|
2021-10-31 15:09:40 +01:00
|
|
|
self.assert_cli(cli._compare, paths)
|
|
|
|
self.assert_options(cli._compare, bool_options, arg_options, paths)
|
2021-10-30 12:48:39 +02:00
|
|
|
|
2022-07-27 03:28:24 +02:00
|
|
|
def test_check(self):
|
|
|
|
self.assert_check()
|
|
|
|
|
2021-10-30 12:48:39 +02:00
|
|
|
|
|
|
|
def test_needsfiles(tmpdir):
|
|
|
|
assert tmpdir
|
|
|
|
|
|
|
|
|
|
|
|
def test_create_file(tmp_path):
|
|
|
|
directory = tmp_path / "sub"
|
|
|
|
directory.mkdir()
|
|
|
|
path = directory / "hello.txt"
|
|
|
|
path.write_text(CONTENT)
|
|
|
|
assert path.read_text() == CONTENT
|
|
|
|
assert len(list(tmp_path.iterdir())) == 1
|