ordigi/tests/test_media.py

69 lines
2.5 KiB
Python
Raw Permalink Normal View History

from datetime import datetime
2021-08-08 15:33:47 +02:00
import pytest
from pathlib import Path
import re
2021-08-08 15:33:47 +02:00
import shutil
import tempfile
2021-08-08 21:43:37 +02:00
from .conftest import copy_sample_files
2021-08-14 21:28:40 +02:00
from ordigi import constants
from ordigi.media import Media
from ordigi.images import Images
from ordigi.exiftool import ExifTool, ExifToolCaching
2021-08-08 15:33:47 +02:00
2021-08-14 21:28:40 +02:00
ORDIGI_PATH = Path(__file__).parent.parent
CACHING = True
2021-08-08 15:33:47 +02:00
class TestMetadata:
def setup_class(cls):
2021-08-08 21:43:37 +02:00
cls.src_paths, cls.file_paths = copy_sample_files()
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:
self.exif_data = ExifTool(str(file_path)).asdict()
yield Media(str(file_path), self.ignore_tags)
def test_get_metadata(self):
for media in self.get_media():
result = media.get_metadata()
assert result
assert isinstance(media.metadata, dict), media.metadata
#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
# 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
if has_exif_data == False:
assert not media.has_exif_data()
# 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