--convert-to-jpeg initial version working
This commit is contained in:
BIN
tests/export_db_version1.db
Normal file
BIN
tests/export_db_version1.db
Normal file
Binary file not shown.
@@ -12,7 +12,7 @@ def test_export_db():
|
||||
""" test ExportDB """
|
||||
import os
|
||||
import tempfile
|
||||
from osxphotos._export_db import ExportDB
|
||||
from osxphotos.export_db import ExportDB
|
||||
|
||||
tempdir = tempfile.TemporaryDirectory(prefix="osxphotos_")
|
||||
dbname = os.path.join(tempdir.name, ".osxphotos_export.db")
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
import pytest
|
||||
|
||||
TEST_HEIC = "tests/test-images/IMG_3092.heic"
|
||||
|
||||
def test_copy_file_valid():
|
||||
# copy file with valid src, dest
|
||||
@@ -67,3 +68,15 @@ def test_unlink_file():
|
||||
assert os.path.isfile(dest)
|
||||
FileUtil.unlink(dest)
|
||||
assert not os.path.isfile(dest)
|
||||
|
||||
def test_convert_to_jpeg():
|
||||
import pathlib
|
||||
import tempfile
|
||||
from osxphotos.fileutil import FileUtil
|
||||
|
||||
temp_dir = tempfile.TemporaryDirectory(prefix="osxphotos_")
|
||||
with temp_dir:
|
||||
imgfile = pathlib.Path(TEST_HEIC)
|
||||
outfile = pathlib.Path(temp_dir.name) / f"{imgfile.stem}.jpeg"
|
||||
assert FileUtil.convert_to_jpeg(imgfile, outfile)
|
||||
assert outfile.is_file()
|
||||
106
tests/test_image_converter.py
Normal file
106
tests/test_image_converter.py
Normal file
@@ -0,0 +1,106 @@
|
||||
""" test ImageConverter """
|
||||
|
||||
import pytest
|
||||
|
||||
TEST_HEIC = "tests/test-images/IMG_3092.heic"
|
||||
TEST_RAW = "tests/test-images/IMG_0476_2.CR2"
|
||||
TEST_JPEG = "tests/test-images/IMG_3984.jpeg"
|
||||
TEST_IMAGES = [TEST_HEIC, TEST_RAW, TEST_JPEG]
|
||||
TEST_NOT_AN_IMAGE = "tests/README.md"
|
||||
TEST_IMAGE_DOES_NOT_EXIST = "tests/test-images/NOT-A-FILE.heic"
|
||||
|
||||
|
||||
def test_image_converter_singleton():
|
||||
""" test that ImageConverter is a singleton """
|
||||
from osxphotos.imageconverter import ImageConverter
|
||||
|
||||
convert1 = ImageConverter()
|
||||
convert2 = ImageConverter()
|
||||
|
||||
assert convert1 == convert2
|
||||
|
||||
|
||||
def test_image_converter():
|
||||
""" test conversion of different image types """
|
||||
import pathlib
|
||||
import tempfile
|
||||
from osxphotos.imageconverter import ImageConverter
|
||||
|
||||
converter = ImageConverter()
|
||||
tempdir = tempfile.TemporaryDirectory(prefix="osxphotos_")
|
||||
with tempdir:
|
||||
for imgfile in TEST_IMAGES:
|
||||
imgfile = pathlib.Path(imgfile)
|
||||
outfile = pathlib.Path(tempdir.name) / f"{imgfile.stem}.jpeg"
|
||||
outfile2 = pathlib.Path(tempdir.name) / f"{imgfile.stem}_2.jpeg"
|
||||
|
||||
# call write_jpeg with both pathlib.Path and str arguments
|
||||
assert converter.write_jpeg(imgfile, outfile)
|
||||
assert converter.write_jpeg(str(imgfile), str(outfile2))
|
||||
assert outfile.is_file()
|
||||
assert outfile2.is_file()
|
||||
|
||||
|
||||
def test_image_converter_compression_quality():
|
||||
""" test conversion of different image types with custom compression quality """
|
||||
import pathlib
|
||||
import tempfile
|
||||
from osxphotos.imageconverter import ImageConverter
|
||||
|
||||
converter = ImageConverter()
|
||||
tempdir = tempfile.TemporaryDirectory(prefix="osxphotos_")
|
||||
with tempdir:
|
||||
for imgfile in TEST_IMAGES:
|
||||
imgfile = pathlib.Path(imgfile)
|
||||
outfile = pathlib.Path(tempdir.name) / f"{imgfile.stem}.jpeg"
|
||||
|
||||
# call write_jpeg with both pathlib.Path and str arguments
|
||||
assert converter.write_jpeg(imgfile, outfile, compression_quality=0.5)
|
||||
assert outfile.is_file()
|
||||
|
||||
|
||||
def test_image_converter_bad_compression_quality():
|
||||
""" test illegal compression quality """
|
||||
import pathlib
|
||||
import tempfile
|
||||
from osxphotos.imageconverter import ImageConverter
|
||||
|
||||
converter = ImageConverter()
|
||||
tempdir = tempfile.TemporaryDirectory(prefix="osxphotos_")
|
||||
with tempdir:
|
||||
imgfile = pathlib.Path(TEST_HEIC)
|
||||
outfile = pathlib.Path(tempdir.name) / f"{imgfile.stem}.jpeg"
|
||||
with pytest.raises(ValueError):
|
||||
converter.write_jpeg(imgfile, outfile, compression_quality=2.0)
|
||||
with pytest.raises(ValueError):
|
||||
converter.write_jpeg(imgfile, outfile, compression_quality=-1.0)
|
||||
|
||||
|
||||
def test_image_converter_bad_file():
|
||||
""" Try to convert a file that's not an image """
|
||||
import pathlib
|
||||
import tempfile
|
||||
from osxphotos.imageconverter import ImageConverter
|
||||
|
||||
converter = ImageConverter()
|
||||
tempdir = tempfile.TemporaryDirectory(prefix="osxphotos_")
|
||||
with tempdir:
|
||||
imgfile = pathlib.Path(TEST_NOT_AN_IMAGE)
|
||||
outfile = pathlib.Path(tempdir.name) / f"{imgfile.stem}.jpeg"
|
||||
assert not converter.write_jpeg(imgfile, outfile)
|
||||
|
||||
|
||||
def test_image_converter_missing_file():
|
||||
""" Try to convert a file that's not an image """
|
||||
import pathlib
|
||||
import tempfile
|
||||
from osxphotos.imageconverter import ImageConverter
|
||||
|
||||
converter = ImageConverter()
|
||||
tempdir = tempfile.TemporaryDirectory(prefix="osxphotos_")
|
||||
with tempdir:
|
||||
imgfile = pathlib.Path(TEST_IMAGE_DOES_NOT_EXIST)
|
||||
outfile = pathlib.Path(tempdir.name) / f"{imgfile.stem}.jpeg"
|
||||
with pytest.raises(FileNotFoundError):
|
||||
converter.write_jpeg(imgfile, outfile)
|
||||
|
||||
Reference in New Issue
Block a user