ordigi/tests/test_collection.py

197 lines
7.6 KiB
Python
Raw Normal View History

2021-08-08 15:33:47 +02:00
# TODO to be removed later
from datetime import datetime
import os
import pytest
2021-08-31 16:18:41 +02:00
import sqlite3
2021-08-08 15:33:47 +02:00
from pathlib import Path
import re
from sys import platform
from time import sleep
2021-08-31 16:18:41 +02:00
from .conftest import randomize_files, randomize_db
2021-08-13 21:11:24 +02:00
from ordigi import constants
2021-08-31 16:18:41 +02:00
from ordigi.database import Sqlite
from ordigi.exiftool import ExifToolCaching, exiftool_is_running, terminate_exiftool
2021-08-27 12:45:25 +02:00
from ordigi.collection import Collection
from ordigi.geolocation import GeoLocation
2021-08-13 21:11:24 +02:00
from ordigi.media import Media
2021-09-12 07:39:37 +02:00
from ordigi.utils import get_date_from_string, get_date_regex
2021-08-08 15:33:47 +02:00
2021-08-27 12:45:25 +02:00
class TestCollection:
@pytest.fixture(autouse=True)
def setup_class(cls, sample_files_paths):
cls.src_paths, cls.file_paths = sample_files_paths
2021-08-08 15:33:47 +02:00
cls.path_format = constants.default_path + '/' + constants.default_name
def teardown_class(self):
terminate_exiftool()
assert not exiftool_is_running()
2021-08-08 21:43:37 +02:00
def test_get_part(self, tmp_path):
2021-08-08 15:33:47 +02:00
"""
Test all parts
"""
# Item to search for:
2021-08-31 16:18:41 +02:00
collection = Collection(tmp_path, self.path_format)
2021-08-27 12:45:25 +02:00
items = collection.get_items()
2021-08-08 15:33:47 +02:00
masks = [
'{album}',
'{basename}',
'{camera_make}',
'{camera_model}',
'{city}',
'{"custom"}',
'{country}',
'{ext}',
'{folder}',
'{folders[1:3]}',
'{location}',
'{name}',
'{original_name}',
'{state}',
'{title}',
'{%Y-%m-%d}',
'{%Y-%m-%d_%H-%M-%S}',
'{%Y-%m-%b}'
]
subdirs = Path('a', 'b', 'c', 'd')
2021-08-08 21:43:37 +02:00
for file_path in self.file_paths:
2021-09-12 07:39:37 +02:00
media = Media(os.path.dirname(file_path), '', os.path.basename(file_path))
exif_tags = {}
for key in ('album', 'camera_make', 'camera_model', 'latitude',
'longitude', 'original_name', 'title'):
exif_tags[key] = media.tags_keys[key]
2021-08-08 15:33:47 +02:00
exif_data = ExifToolCaching(str(file_path)).asdict()
loc = GeoLocation()
2021-08-31 16:18:41 +02:00
metadata = media.get_metadata(loc)
2021-08-08 15:33:47 +02:00
for item, regex in items.items():
for mask in masks:
matched = re.search(regex, mask)
if matched:
2021-08-27 12:45:25 +02:00
part = collection.get_part(item, mask[1:-1],
2021-08-31 16:18:41 +02:00
metadata, subdirs)
2021-08-08 15:33:47 +02:00
# check if part is correct
2021-08-08 21:43:37 +02:00
assert isinstance(part, str), file_path
2021-08-08 15:33:47 +02:00
if item == 'basename':
2021-08-08 21:43:37 +02:00
assert part == file_path.stem, file_path
2021-08-08 15:33:47 +02:00
elif item == 'date':
assert datetime.strptime(part, mask[1:-1])
elif item == 'folder':
2021-08-08 21:43:37 +02:00
assert part == subdirs.name, file_path
2021-08-08 15:33:47 +02:00
elif item == 'folders':
if platform == "win32":
2021-08-08 21:43:37 +02:00
assert '\\' in part, file_path
2021-08-08 15:33:47 +02:00
else:
2021-08-08 21:43:37 +02:00
assert '/' in part, file_path
2021-08-08 15:33:47 +02:00
elif item == 'ext':
2021-08-08 21:43:37 +02:00
assert part == file_path.suffix[1:], file_path
elif item == 'name':
2021-08-08 15:33:47 +02:00
expected_part = file_path.stem
2021-09-12 07:39:37 +02:00
for i, rx in get_date_regex(expected_part):
2021-08-08 21:43:37 +02:00
part = re.sub(rx, '', expected_part)
assert part == expected_part, file_path
2021-08-08 15:33:47 +02:00
elif item == 'custom':
2021-08-08 21:43:37 +02:00
assert part == mask[2:-2], file_path
2021-08-08 15:33:47 +02:00
elif item in ('city', 'country', 'location', 'state'):
2021-08-08 21:43:37 +02:00
pass
2021-08-08 15:33:47 +02:00
elif item in exif_tags.keys():
f = False
for key in exif_tags[item]:
if key in exif_data:
f = True
2021-08-08 21:43:37 +02:00
assert part == exif_data[key], file_path
2021-08-08 15:33:47 +02:00
break
if f == False:
2021-08-08 21:43:37 +02:00
assert part == '', file_path
else:
assert part == '', file_path
2021-08-08 15:33:47 +02:00
2021-08-27 12:45:25 +02:00
def test_get_date_taken(self, tmp_path):
2021-08-31 16:18:41 +02:00
collection = Collection(tmp_path, self.path_format)
2021-08-08 21:43:37 +02:00
for file_path in self.file_paths:
2021-08-08 15:33:47 +02:00
exif_data = ExifToolCaching(str(file_path)).asdict()
2021-09-12 07:39:37 +02:00
media = Media(os.path.dirname(file_path), '', os.path.basename(file_path))
2021-08-08 15:33:47 +02:00
metadata = media.get_metadata()
2021-09-12 07:39:37 +02:00
date_taken = media.get_date_taken()
2021-08-08 15:33:47 +02:00
date_filename = None
for tag in media.tags_keys['original_name']:
if tag in exif_data:
2021-09-12 07:39:37 +02:00
date_filename = get_date_from_string(exif_data[tag])
break
if not date_filename:
2021-09-12 07:39:37 +02:00
date_filename = get_date_from_string(file_path.name)
2021-08-08 15:33:47 +02:00
if media.metadata['date_original']:
assert date_taken == media.metadata['date_original']
2021-08-08 15:33:47 +02:00
elif date_filename:
assert date_taken == date_filename
elif media.metadata['date_created']:
assert date_taken == media.metadata['date_created']
elif media.metadata['date_modified']:
assert date_taken == media.metadata['date_modified']
2021-08-08 15:33:47 +02:00
def test_sort_files(self, tmp_path):
2021-09-12 07:39:37 +02:00
collection = Collection(tmp_path, self.path_format, album_from_folder=True)
loc = GeoLocation()
2021-08-31 16:18:41 +02:00
summary, has_errors = collection.sort_files([self.src_paths], loc)
2021-08-08 15:33:47 +02:00
# Summary is created and there is no errors
assert summary, summary
assert not has_errors, has_errors
2021-09-12 07:39:37 +02:00
for file_path in tmp_path.glob('*/**/*.*'):
if '.db' not in str(file_path):
media = Media(os.path.dirname(file_path), '', os.path.basename(file_path), album_from_folder=True)
media.get_exif_metadata()
for value in media._get_key_values('album'):
assert value != '' or None
# test with populated dest dir
2021-08-27 12:45:25 +02:00
randomize_files(tmp_path)
2021-08-31 16:18:41 +02:00
summary, has_errors = collection.sort_files([self.src_paths], loc)
2021-08-27 12:45:25 +02:00
assert summary, summary
assert not has_errors, has_errors
2021-08-08 15:33:47 +02:00
# TODO check if path follow path_format
2021-08-31 16:18:41 +02:00
def test_sort_files_invalid_db(self, tmp_path):
collection = Collection(tmp_path, self.path_format)
loc = GeoLocation()
randomize_db(tmp_path)
with pytest.raises(sqlite3.DatabaseError) as e:
summary, has_errors = collection.sort_files([self.src_paths], loc)
2021-08-27 12:45:25 +02:00
2021-08-08 15:33:47 +02:00
def test_sort_file(self, tmp_path):
for mode in 'copy', 'move':
2021-08-31 16:18:41 +02:00
collection = Collection(tmp_path, self.path_format, mode=mode)
2021-08-08 15:33:47 +02:00
# copy mode
2021-08-08 21:43:37 +02:00
src_path = Path(self.src_paths, 'photo.png')
name = 'photo_' + mode + '.png'
dest_path = Path(tmp_path, name)
2021-08-27 12:45:25 +02:00
src_checksum = collection.checksum(src_path)
result_copy = collection.sort_file(src_path, dest_path)
2021-08-08 15:33:47 +02:00
assert result_copy
# Ensure files remain the same
2021-08-27 12:45:25 +02:00
assert collection.checkcomp(dest_path, src_checksum)
2021-08-08 15:33:47 +02:00
if mode == 'copy':
assert src_path.exists()
else:
assert not src_path.exists()
# TODO check for conflicts
# TODO check date
#- Sort similar images into a directory
2021-08-27 12:45:25 +02:00
# collection.sort_similar
2021-08-08 15:33:47 +02:00