From d41e384e90ec2791983b390f464f1e3c2abaf5e9 Mon Sep 17 00:00:00 2001 From: Jean-Claude Graf Date: Wed, 31 Mar 2021 22:09:28 +0200 Subject: [PATCH] NEW: Basic test setup Start working on a test setup using fixtures for preparing a test image. Currently, the test fails due to a weird circumstances. Accessing `value.human_value` in the for loop in `test_MetadataHandler_fetch_key` causes a segmentation fault. Also, the test image is not deleted after the test. --- misc/requirements/requirements_tests.txt | 1 + tests/conftest.py | 19 +++++++ tests/unit/imutils/test_metadata.py | 70 ++++++++++++++++++++++++ 3 files changed, 90 insertions(+) diff --git a/misc/requirements/requirements_tests.txt b/misc/requirements/requirements_tests.txt index 12d9ba718..09a5ed4e0 100644 --- a/misc/requirements/requirements_tests.txt +++ b/misc/requirements/requirements_tests.txt @@ -4,3 +4,4 @@ pytest-mock==3.5.1 pytest-qt==3.3.0 pytest-xvfb==2.0.0 pytest-bdd==4.0.2 +Pillow==8.1.2 diff --git a/tests/conftest.py b/tests/conftest.py index cad7dc6ad..4473f7f3e 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -190,3 +190,22 @@ def add_exif_information_impl(path: str, content): metadata.piexif.insert(metadata.piexif.dump(exif_dict), path) return add_exif_information_impl + + +@pytest.fixture() +def add_metadata_information(): + """Fixture to retrieve a helper function that adds metadata content to an image.""" + + def add_metadata_information_impl(path: str, content): + assert ( + metadata.pyexiv2 is not None + ), "pyexiv2 required to add metadata information" + _metadata = metadata.pyexiv2.ImageMetadata(path) + _metadata.read() + + for key, value in content.items(): + _metadata.__setitem__(key, value) + + _metadata.write() + + return add_metadata_information_impl diff --git a/tests/unit/imutils/test_metadata.py b/tests/unit/imutils/test_metadata.py index 421c94199..b55a11f75 100644 --- a/tests/unit/imutils/test_metadata.py +++ b/tests/unit/imutils/test_metadata.py @@ -6,9 +6,24 @@ """Tests for vimiv.imutils.metadata.""" +from fractions import Fraction +from PyQt5.QtGui import QPixmap +from PIL import Image import pytest +from vimiv import imutils, utils from vimiv.imutils import metadata +from vimiv.imutils.metadata import MetadataHandler + +try: + import piexif +except ImportError: + piexif = None + +try: + import pyexiv2 +except ImportError: + pyexiv2 = None @pytest.fixture( @@ -66,3 +81,58 @@ def test_handler_base_raises(methodname, args): def test_handler_exception_customization(handler, expected_msg): with pytest.raises(metadata.UnsupportedMetadataOperation, match=expected_msg): handler.raise_exception("test operation") + + +@pytest.fixture() +def metadata_content(): + return { + "Exif.Image.Copyright": pyexiv2.exif.ExifTag( + "Exif.Image.Copyright", "vimiv-AUTHORS-2021" + ), + "Exif.Image.DateTime": pyexiv2.exif.ExifTag( + "Exif.Image.DateTime", "2017-12-16 16:21:57" + ), + "Exif.Image.Make": pyexiv2.exif.ExifTag("Exif.Image.Make", "vimiv"), + "Exif.Photo.ApertureValue": pyexiv2.exif.ExifTag( + "Exif.Photo.ApertureValue", Fraction(4) + ), + "Exif.Photo.ExposureTime": pyexiv2.exif.ExifTag( + "Exif.Photo.ExposureTime", Fraction(1, 25) + ), + "Exif.Photo.FocalLength": pyexiv2.exif.ExifTag( + "Exif.Photo.FocalLength", Fraction(600) + ), + "Exif.Photo.ISOSpeedRatings": pyexiv2.exif.ExifTag( + "Exif.Photo.ISOSpeedRatings", [320] + ), + "Exif.GPSInfo.GPSAltitude": pyexiv2.exif.ExifTag( + "Exif.GPSInfo.GPSAltitude", Fraction(2964) + ) + # TODO: ADD IPTC + } + + +@pytest.fixture() +def dummy_image(): + filename = "./image.jpg" + Image.new(mode="RGB", size=(300, 300), color="red").save(filename) + # QPixmap(*(300, 300)).save(filename) + return filename + + +@pytest.fixture +def get_MetadataHandler(add_metadata_information, dummy_image, metadata_content): + assert pyexiv2 is not None, "pyexiv2 required to add metadata information" + add_metadata_information(dummy_image, metadata_content) + return MetadataHandler(dummy_image) + + +def test_MetadataHandler_fetch_key(get_MetadataHandler, metadata_content): + handler = get_MetadataHandler + for key, value in metadata_content.items(): + data = handler.fetch_key(key) + assert data[0] == key + try: + assert data[2] == value.human_value + except AttributeError: + assert data[2] == value.raw_value