def test_setvalue_context_manager(): # test setting a tag value as context manager import os.path import tempfile import osxphotos.exiftool from osxphotos.fileutil import FileUtil tempdir = tempfile.TemporaryDirectory(prefix="osxphotos_") tempfile = os.path.join(tempdir.name, os.path.basename(TEST_FILE_ONE_KEYWORD)) FileUtil.copy(TEST_FILE_ONE_KEYWORD, tempfile) with osxphotos.exiftool.ExifTool(tempfile) as exif: exif.setvalue("IPTC:Keywords", "test1") exif.setvalue("IPTC:Keywords", "test2") exif.setvalue("XMP:Title", "title") exif.setvalue("XMP:Subject", "subject") assert exif.error is None exif2 = osxphotos.exiftool.ExifTool(tempfile) exif2._read_exif() assert sorted(exif2.data["IPTC:Keywords"]) == ["test1", "test2"] assert exif2.data["XMP:Title"] == "title" assert exif2.data["XMP:Subject"] == "subject"
def copy_photos_library(photos_library, destination=None): """ copy the test library, returns path to copied library """ photoslib = photoscript.PhotosLibrary() photoslib.quit() picture_folder = (pathlib.Path(str(destination)) or pathlib.Path("~/Pictures").expanduser()) if not picture_folder.is_dir(): pytest.exit(f"Invalid picture folder: '{picture_folder}'") src = pathlib.Path(os.getcwd()) / f"tests/test_libraries/{photos_library}" dest = picture_folder / photos_library FileUtil.copy(src, picture_folder) return dest
def test_setvalue_context_manager_error(): # test setting a tag value as context manager when error generated import os.path import tempfile import osxphotos.exiftool from osxphotos.fileutil import FileUtil tempdir = tempfile.TemporaryDirectory(prefix="osxphotos_") tempfile = os.path.join(tempdir.name, os.path.basename(TEST_FILE_ONE_KEYWORD)) FileUtil.copy(TEST_FILE_ONE_KEYWORD, tempfile) with osxphotos.exiftool.ExifTool(tempfile) as exif: exif.setvalue("Foo:Bar", "test1") assert exif.error
def test_setvalue_error(): # test setting illegal tag value generates error import os.path import tempfile import osxphotos.exiftool from osxphotos.fileutil import FileUtil tempdir = tempfile.TemporaryDirectory(prefix="osxphotos_") tempfile = os.path.join(tempdir.name, os.path.basename(TEST_FILE_ONE_KEYWORD)) FileUtil.copy(TEST_FILE_ONE_KEYWORD, tempfile) exif = osxphotos.exiftool.ExifTool(tempfile) exif.setvalue("IPTC:Foo", "test") assert exif.error
def test_addvalues_1(): # test setting a tag value import os.path import tempfile import osxphotos.exiftool from osxphotos.fileutil import FileUtil tempdir = tempfile.TemporaryDirectory(prefix="osxphotos_") tempfile = os.path.join(tempdir.name, os.path.basename(TEST_FILE_ONE_KEYWORD)) FileUtil.copy(TEST_FILE_ONE_KEYWORD, tempfile) exif = osxphotos.exiftool.ExifTool(tempfile) exif.addvalues("IPTC:Keywords", "test") exif._read_exif() assert sorted(exif.data["IPTC:Keywords"]) == sorted(["wedding", "test"])
def test_setvalue_1(): # test setting a tag value import os.path import tempfile import osxphotos.exiftool from osxphotos.fileutil import FileUtil tempdir = tempfile.TemporaryDirectory(prefix="osxphotos_") tempfile = os.path.join(tempdir.name, os.path.basename(TEST_FILE_ONE_KEYWORD)) FileUtil.copy(TEST_FILE_ONE_KEYWORD, tempfile) exif = osxphotos.exiftool.ExifTool(tempfile) exif.setvalue("IPTC:Keywords", "test") assert not exif.error exif._read_exif() assert exif.data["IPTC:Keywords"] == "test"
def test_clear_value(): # test clearing a tag value import os.path import tempfile import osxphotos.exiftool from osxphotos.fileutil import FileUtil tempdir = tempfile.TemporaryDirectory(prefix="osxphotos_") tempfile = os.path.join(tempdir.name, os.path.basename(TEST_FILE_ONE_KEYWORD)) FileUtil.copy(TEST_FILE_ONE_KEYWORD, tempfile) exif = osxphotos.exiftool.ExifTool(tempfile) assert "IPTC:Keywords" in exif.data exif.setvalue("IPTC:Keywords", None) exif._read_exif() assert "IPTC:Keywords" not in exif.data
def test_copy_file_invalid(): # copy file with invalid src import tempfile from osxphotos.fileutil import FileUtil temp_dir = tempfile.TemporaryDirectory(prefix="osxphotos_") src = "tests/test-images/wedding_DOES_NOT_EXIST.jpg" with pytest.raises(Exception) as e: assert FileUtil.copy(src, temp_dir.name) assert e.type == FileNotFoundError
def test_copy_file_valid(): # copy file with valid src, dest import os.path import tempfile from osxphotos.fileutil import FileUtil temp_dir = tempfile.TemporaryDirectory(prefix="osxphotos_") src = "tests/test-images/wedding.jpg" result = FileUtil.copy(src, temp_dir.name) assert result == 0 assert os.path.isfile(os.path.join(temp_dir.name, "wedding.jpg"))
def test_addvalues_2(): # test setting a tag value where multiple values already exist import os.path import tempfile import osxphotos.exiftool from osxphotos.fileutil import FileUtil tempdir = tempfile.TemporaryDirectory(prefix="osxphotos_") tempfile = os.path.join(tempdir.name, os.path.basename(TEST_FILE_MULTI_KEYWORD)) FileUtil.copy(TEST_FILE_MULTI_KEYWORD, tempfile) exif = osxphotos.exiftool.ExifTool(tempfile) assert sorted(exif.data["IPTC:Keywords"]) == sorted(TEST_MULTI_KEYWORDS) exif.addvalues("IPTC:Keywords", "test") exif._read_exif() assert "IPTC:Keywords" in exif.data test_multi = TEST_MULTI_KEYWORDS.copy() test_multi.append("test") assert sorted(exif.data["IPTC:Keywords"]) == sorted(test_multi)
def test_unlink_file(): import os.path import tempfile from osxphotos.fileutil import FileUtil temp_dir = tempfile.TemporaryDirectory(prefix="osxphotos_") src = "tests/test-images/wedding.jpg" dest = os.path.join(temp_dir.name, "wedding.jpg") result = FileUtil.copy(src, temp_dir.name) assert os.path.isfile(dest) FileUtil.unlink(dest) assert not os.path.isfile(dest)