Skip to content

Commit

Permalink
FIX: segfault in test and make test work
Browse files Browse the repository at this point in the history
All code is from @karlch. Thanks for the snippet
  • Loading branch information
jcjgraf committed Apr 11, 2021
1 parent d41e384 commit aad33d0
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 12 deletions.
4 changes: 2 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,8 @@ def add_metadata_information_impl(path: str, content):
_metadata = metadata.pyexiv2.ImageMetadata(path)
_metadata.read()

for key, value in content.items():
_metadata.__setitem__(key, value)
for tag in content.values():
_metadata[tag.key] = tag.value

_metadata.write()

Expand Down
33 changes: 23 additions & 10 deletions tests/unit/imutils/test_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,26 +113,39 @@ def metadata_content():


@pytest.fixture()
def dummy_image():
def dummy_image(qapp, tmp_path):
filename = "./image.jpg"
Image.new(mode="RGB", size=(300, 300), color="red").save(filename)
# QPixmap(*(300, 300)).save(filename)
filename = str(tmp_path / "image.jpg")
QPixmap(300, 300).save(filename)
return filename


@pytest.fixture
def get_MetadataHandler(add_metadata_information, dummy_image, metadata_content):
def metadata_handler(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
@pytest.fixture
def metadata_handler_piexif(dummy_image, metadata_handler):
metadata_handler._ext_handler = metadata._ExternalKeyHandlerPiexif(dummy_image)
return metadata_handler


def test_metadatahandler_fetch_key(metadata_handler, metadata_content):
for key, value in metadata_content.items():
data = handler.fetch_key(key)
assert data[0] == key
fetched_key, _, fetched_value = metadata_handler.fetch_key(key)
assert fetched_key == key
try:
assert data[2] == value.human_value
assert fetched_value == value.human_value
except AttributeError:
assert data[2] == value.raw_value
assert fetched_value == value.raw_value


def test_metadatahandler_fetch_key_piexif(metadata_handler_piexif, metadata_content):
for key, value in metadata_content.items():
fetched_key, _, fetched_value = metadata_handler_piexif.fetch_key(key)
short_key = key.rpartition(".")[-1]
assert fetched_key == short_key
assert fetched_value == value.raw_value

0 comments on commit aad33d0

Please sign in to comment.