2014-04-07 22:19:44 +02:00
|
|
|
import shutil
|
2022-02-15 12:02:17 +01:00
|
|
|
from pathlib import Path
|
2021-06-03 15:20:39 +02:00
|
|
|
|
2021-06-04 14:05:14 +02:00
|
|
|
import pytest
|
2021-10-17 16:24:37 +02:00
|
|
|
|
2022-01-28 06:09:19 +01:00
|
|
|
from libretime_analyzer.pipeline.organise_file import organise_file
|
2021-06-04 22:43:52 +02:00
|
|
|
|
2022-01-17 20:31:43 +01:00
|
|
|
from ..conftest import AUDIO_FILENAME
|
2021-06-04 14:05:14 +02:00
|
|
|
|
|
|
|
|
2022-02-15 12:02:17 +01:00
|
|
|
def organise_file_args_factory(filepath: Path, dest_dir: Path):
|
|
|
|
return (
|
|
|
|
str(filepath),
|
|
|
|
str(dest_dir),
|
2021-06-04 14:05:14 +02:00
|
|
|
AUDIO_FILENAME,
|
2022-02-15 12:02:17 +01:00
|
|
|
{},
|
2021-06-04 14:05:14 +02:00
|
|
|
)
|
2021-05-27 16:23:02 +02:00
|
|
|
|
2014-04-07 22:19:44 +02:00
|
|
|
|
2022-02-15 12:02:17 +01:00
|
|
|
def test_organise_file(src_dir: Path, dest_dir: Path):
|
|
|
|
organise_file(*organise_file_args_factory(src_dir / AUDIO_FILENAME, dest_dir))
|
|
|
|
assert (dest_dir / AUDIO_FILENAME).exists()
|
2021-05-27 16:23:02 +02:00
|
|
|
|
2014-04-07 22:19:44 +02:00
|
|
|
|
2022-02-15 12:02:17 +01:00
|
|
|
def test_organise_file_samefile(src_dir: Path):
|
|
|
|
organise_file(*organise_file_args_factory(src_dir / AUDIO_FILENAME, src_dir))
|
|
|
|
assert (src_dir / AUDIO_FILENAME).exists()
|
2021-05-27 16:23:02 +02:00
|
|
|
|
|
|
|
|
2022-02-15 12:02:17 +01:00
|
|
|
def test_organise_file_duplicate_file(src_dir: Path, dest_dir: Path):
|
|
|
|
for i in range(1, 4):
|
|
|
|
# Make a copy so we can reuse the file
|
|
|
|
filename = f"{i}_{AUDIO_FILENAME}"
|
|
|
|
shutil.copy(src_dir / AUDIO_FILENAME, src_dir / filename)
|
2021-05-27 16:23:02 +02:00
|
|
|
|
2022-02-15 12:02:17 +01:00
|
|
|
metadata = organise_file(
|
|
|
|
*organise_file_args_factory(src_dir / filename, dest_dir)
|
|
|
|
)
|
2021-05-29 18:11:54 +02:00
|
|
|
|
2022-02-15 12:02:17 +01:00
|
|
|
full_path = Path(metadata["full_path"])
|
|
|
|
assert full_path.exists()
|
|
|
|
if i == 1:
|
|
|
|
assert full_path.name == AUDIO_FILENAME
|
|
|
|
else:
|
|
|
|
assert len(full_path.name) == len(AUDIO_FILENAME) + 1 + 36 # _ + UUID size
|
2021-06-04 14:05:14 +02:00
|
|
|
|
|
|
|
|
2022-02-15 12:02:17 +01:00
|
|
|
def test_organise_file_bad_permissions_dest_dir(src_dir: Path):
|
2021-06-04 14:05:14 +02:00
|
|
|
with pytest.raises(OSError):
|
|
|
|
# /sys is using sysfs on Linux, which is unwritable
|
2022-01-17 20:31:43 +01:00
|
|
|
organise_file(
|
2022-02-15 12:02:17 +01:00
|
|
|
*organise_file_args_factory(
|
|
|
|
src_dir / AUDIO_FILENAME,
|
|
|
|
Path("/sys/foobar"),
|
|
|
|
)
|
2021-06-04 14:05:14 +02:00
|
|
|
)
|