Skip to content

Commit

Permalink
add pytest test cases with real files
Browse files Browse the repository at this point in the history
  • Loading branch information
DasMoorhuhn committed Nov 9, 2024
1 parent d60f18d commit 87c6163
Show file tree
Hide file tree
Showing 13 changed files with 122 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[run]
source = exifread
omit = tests/*, __init__.py
9 changes: 9 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,15 @@ This example shows how to use the library to correct the orientation of an image
return im
PyTest
******
for running PyTest, run `sh tests/start_tests.sh` in the projects root directory.
This will run a few test cases with real files.
The images are cropped to 10x10 pixels for the sage of the git repo.
The RAW file is modified, so only the data with the exif tags are there.

This can be used as reference during development, to ensure, that the lib still works after changes.

License
*******

Expand Down
3 changes: 3 additions & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[pytest]
pythonpath = tests
addopts = --ignore-glob=**/tests/**,
3 changes: 3 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
"pylint==2.14.4",
]

# TODO: add testing before build/install

setup(
name="ExifRead",
version=exifread.__version__,
Expand All @@ -34,6 +36,7 @@
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Topic :: Utilities",
],
extras_require={
Expand Down
Empty file added tests/__init__.py
Empty file.
Empty file added tests/helpers/__init__.py
Empty file.
25 changes: 25 additions & 0 deletions tests/helpers/folder_helper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import os
import shutil
from pathlib import Path

TEST_FOLDER = '.test_folder'
TEST_IMAGES = os.path.join('tests', 'test_files')


def create_file(file):
Path(os.path.join(TEST_FOLDER, file)).touch()


def delete_folder():
shutil.rmtree(TEST_FOLDER, ignore_errors=True)


def copy_test_images(file:str):
delete_folder()
os.mkdir(TEST_FOLDER)
if file:
shutil.copy(src=os.path.join(TEST_IMAGES, file), dst=os.path.join(TEST_FOLDER, file))
else:
shutil.copy(src=os.path.join(TEST_IMAGES, os.path.sep), dst=TEST_FOLDER)


10 changes: 10 additions & 0 deletions tests/start_tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
python3.12 -m pytest \
--no-header \
-rfp \
--cov \
--cov-report html:tests/coverage \
--cov-report xml:tests/coverage/coverage.xml \
--junitxml=tests/coverage/report.xml \
tests/

exit $?
Binary file added tests/test_files/sony_alpha_a58.JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/test_files/sony_alpha_a7iii_raw_image.ARW
Binary file not shown.
Binary file added tests/test_files/test_video.mp4
Binary file not shown.
45 changes: 45 additions & 0 deletions tests/test_images.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import unittest
import sys
import os


sys.path.append('..')

from exifread import process_file
from helpers.folder_helper import copy_test_images
from helpers.folder_helper import TEST_FOLDER
from helpers.folder_helper import delete_folder


class TestMobiles(unittest.TestCase):
def tearDown(self):
delete_folder()

def test_samsung(self):
copy_test_images(file='samsung_a54_001.jpg')
with open(file=os.path.join(TEST_FOLDER, 'samsung_a54_001.jpg'), mode='rb') as file: exif_tags = process_file(file)
make = next((exif_tags[tag] for tag in exif_tags.keys() if 'Make' in tag), None)
assert str(make) == 'samsung'

def test_apple(self):
copy_test_images(file='iphone_x_001.jpeg')
with open(file=os.path.join(TEST_FOLDER, 'iphone_x_001.jpeg'), mode='rb') as file: exif_tags = process_file(file)
make = next((exif_tags[tag] for tag in exif_tags.keys() if 'Make' in tag), None)
assert str(make) == 'Apple'


class TestCameras(unittest.TestCase):
def tearDown(self):
delete_folder()

def test_sony_jpg(self):
copy_test_images(file='sony_alpha_a58.JPG')
with open(file=os.path.join(TEST_FOLDER, 'sony_alpha_a58.JPG'), mode='rb') as file: exif_tags = process_file(file)
make = next((exif_tags[tag] for tag in exif_tags.keys() if 'Make' in tag), None)
assert str(make) == 'SONY'

def test_sony_raw(self):
copy_test_images(file='sony_alpha_a7iii_raw_image.ARW')
with open(file=os.path.join(TEST_FOLDER, 'sony_alpha_a7iii_raw_image.ARW'), mode='rb') as file: exif_tags = process_file(file)
make = next((exif_tags[tag] for tag in exif_tags.keys() if 'Make' in tag), None)
assert str(make) == 'SONY'
24 changes: 24 additions & 0 deletions tests/test_video.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import unittest
import sys
import os


sys.path.append('..')

from exifread import process_file
from helpers.folder_helper import copy_test_images
from helpers.folder_helper import TEST_FOLDER
from helpers.folder_helper import delete_folder


class TestVideos(unittest.TestCase):
def tearDown(self):
delete_folder()

# TODO: add a mp4 with exif tags
def test_video_mp4(self):
copy_test_images(file='test_video.mp4')
with open(file=os.path.join(TEST_FOLDER, 'test_video.mp4'), mode='rb') as file: exif_tags = process_file(file)
make = next((exif_tags[tag] for tag in exif_tags.keys() if 'Make' in tag), None)
assert make is None

0 comments on commit 87c6163

Please sign in to comment.