ordigi/tests/test_media.py

115 lines
4.4 KiB
Python
Raw Permalink Normal View History

from datetime import datetime
2021-08-08 15:33:47 +02:00
from pathlib import Path
import re
2021-08-08 15:33:47 +02:00
2022-01-29 15:19:37 +01:00
import pytest
2021-08-13 21:11:24 +02:00
from ordigi.media import Media
from ordigi.exiftool import ExifTool, ExifToolCaching
from ordigi.utils import get_date_from_string
2021-08-08 15:33:47 +02:00
2021-08-13 21:11:24 +02:00
ORDIGI_PATH = Path(__file__).parent.parent
CACHING = True
2021-08-08 15:33:47 +02:00
class TestMedia:
2021-08-08 15:33:47 +02:00
2021-08-27 12:45:25 +02:00
@pytest.fixture(autouse=True)
def setup_class(cls, sample_files_paths):
2021-09-18 22:06:34 +02:00
cls.src_path, cls.file_paths = sample_files_paths
2022-01-29 15:19:37 +01:00
cls.ignore_tags = (
'EXIF:CreateDate',
'File:FileModifyDate',
'File:FileAccessDate',
'EXIF:Make',
'Composite:LightValue'
)
2021-08-08 15:33:47 +02:00
def get_media(self):
2021-08-08 15:33:47 +02:00
for file_path in self.file_paths:
2022-01-29 15:19:37 +01:00
yield file_path, Media(
file_path,
self.src_path,
album_from_folder=True,
cache=False,
ignore_tags=self.ignore_tags,
)
def test_get_metadata(self, tmp_path):
2021-09-12 07:39:37 +02:00
for file_path, media in self.get_media():
# test get metadata from cache or exif
for root in self.src_path, tmp_path:
media.get_metadata(root)
assert isinstance(media.metadata, dict), media.metadata
2022-01-29 15:19:37 +01:00
# check if all tags key are present
for tags_key, tags in media.tags_keys.items():
assert tags_key in media.metadata
for tag in tags:
for tag_regex in self.ignore_tags:
assert not re.match(tag_regex, tag)
# Check for valid type
for key, value in media.metadata.items():
if value or value == '':
if 'date' in key:
assert isinstance(value, datetime)
elif key in ('latitude', 'longitude'):
assert isinstance(value, float)
else:
assert isinstance(value, str)
else:
assert value is None
2021-09-12 07:39:37 +02:00
if key == 'album':
2022-01-29 15:19:37 +01:00
for album in media.get_key_values('album'):
if album is not None and album != '':
assert value == album
break
else:
assert value == file_path.parent.name
2021-09-12 07:39:37 +02:00
# Check if has_exif_data() is True if 'date_original' key is
# present, else check if it's false
has_exif_data = False
for tag in media.tags_keys['date_original']:
if tag in media.exif_metadata:
if media.get_date_format(media.exif_metadata[tag]):
has_exif_data = True
assert media.has_exif_data()
break
2022-01-29 15:19:37 +01:00
if not has_exif_data:
assert not media.has_exif_data()
def test_get_date_media(self):
for file_path in self.file_paths:
exif_data = ExifToolCaching(str(file_path)).asdict()
2022-01-29 15:19:37 +01:00
media = Media(
file_path, self.src_path, use_date_filename=True, use_file_dates=True
)
2021-11-11 16:24:37 +01:00
media.get_metadata(self.src_path)
date_media = media.get_date_media()
date_filename = None
for tag in media.tags_keys['original_name']:
if tag in exif_data:
2022-07-27 03:11:11 +02:00
date_filename, _, _ = get_date_from_string(exif_data[tag])
break
if not date_filename:
2022-07-27 03:11:11 +02:00
date_filename, _, _ = get_date_from_string(file_path.name)
if media.metadata['date_original']:
assert date_media == media.metadata['date_original']
elif date_filename:
assert date_media == date_filename
elif media.metadata['date_created']:
assert date_media == media.metadata['date_created']
elif media.metadata['date_modified']:
assert date_media == media.metadata['date_modified']
elif media.metadata['file_modify_date']:
assert date_media == media.metadata['file_modify_date']
# Will be changed to get_metadata
# check if metatadata type are correct
2021-08-08 15:33:47 +02:00
# if(isinstance(self.metadata, dict) and update_cache is False):
# return self.metadata
# Album for folder implemented other place