Skip to content

Commit

Permalink
NEW: Basic test setup
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jcjgraf committed Mar 31, 2021
1 parent 8327beb commit 733b23d
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 0 deletions.
1 change: 1 addition & 0 deletions misc/requirements/requirements_tests.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
19 changes: 19 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
70 changes: 70 additions & 0 deletions tests/unit/imutils/test_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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

0 comments on commit 733b23d

Please sign in to comment.