Skip to content

Commit 60d793f

Browse files
committed
added a ExifTool bytes read/write test
1 parent 957757f commit 60d793f

File tree

4 files changed

+113
-2
lines changed

4 files changed

+113
-2
lines changed

tests/common_util.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
from pathlib import Path
1010
import tempfile
11+
import os
1112

1213
from typing import Optional, Tuple, Any
1314

@@ -21,7 +22,7 @@
2122
SCRIPT_PATH = Path(__file__).resolve().parent
2223

2324
# location to images directory
24-
TEST_IMAGE_DIR = SCRIPT_PATH / "images"
25+
TEST_IMAGE_DIR = SCRIPT_PATH / "files"
2526

2627

2728
# Find example image (note: JPG is listed explicitly, as it may have different tags than other file types)
@@ -67,3 +68,25 @@ def et_get_temp_dir(suffix: Optional[str] = None, base_path: Optional[Path] = No
6768

6869
# ---------------------------------------------------------------------------------------------------------
6970

71+
72+
def create_random_bin_file(filepath: Path, size: int):
73+
"""
74+
generate random binary file with the specified size in bytes
75+
76+
:param filename: the filename
77+
:param size: the size in bytes
78+
79+
:return: filepath
80+
"""
81+
# https://www.bswen.com/2018/04/python-How-to-generate-random-large-file-using-python.html
82+
83+
if filepath.exists():
84+
raise FileExistsError
85+
86+
with open(filepath, 'wb') as f:
87+
f.write(os.urandom(size))
88+
89+
return filepath
90+
91+
# ---------------------------------------------------------------------------------------------------------
92+

tests/files/README.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
This folder contains images used in tests.
1+
This folder contains images/files used in tests.
22

33
More may be added at a later time to test different file formats.
44

tests/files/my_makernotes.config

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# find sample file here -- https://exiftool.org/config.html
2+
3+
# this file was created from a Phil Harvey example here: https://exiftool.org/forum/index.php?topic=5563.msg26986#msg26986
4+
5+
%Image::ExifTool::UserDefined = (
6+
'Image::ExifTool::Exif::Main' => {
7+
0x927c => {
8+
Name => 'MyMakerNotes',
9+
Writable => 'undef',
10+
Binary => 1,
11+
},
12+
},
13+
);
14+
1; # end

tests/test_exiftool_bytes.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
Test :: ExifTool base class - execute() using raw_bytes parameter
4+
"""
5+
6+
# standard
7+
import unittest
8+
import shutil
9+
10+
# test helpers
11+
from tests.common_util import et_get_temp_dir, TEST_IMAGE_DIR, TEST_IMAGE_JPG, create_random_bin_file
12+
13+
# custom
14+
import exiftool
15+
16+
class TestExifToolBytes(unittest.TestCase):
17+
18+
# ---------------------------------------------------------------------------------------------------------
19+
def setUp(self):
20+
self.et = exiftool.ExifTool(common_args=["-G", "-n", "-overwrite_original"], config_file=TEST_IMAGE_DIR / "my_makernotes.config")
21+
22+
(self.temp_obj, self.temp_dir) = et_get_temp_dir(suffix="etbytes")
23+
24+
def tearDown(self):
25+
if self.et.running:
26+
self.et.terminate()
27+
28+
# ---------------------------------------------------------------------------------------------------------
29+
30+
def test_read_write_binary(self):
31+
self.assertFalse(self.et.running)
32+
self.et.run()
33+
34+
### set up files ###
35+
36+
# write a binary file to test with ... we can make it random or the same, shouldn't matter
37+
BIN_FILE_SIZE = 1024
38+
39+
p_bin_file = self.temp_dir / "test_data.bin"
40+
create_random_bin_file(p_bin_file, BIN_FILE_SIZE)
41+
42+
p_jpg_file = self.temp_dir / "rose.jpg"
43+
shutil.copyfile(TEST_IMAGE_JPG, p_jpg_file)
44+
45+
### write the binary data ###
46+
self.et.execute(f"-MyMakerNotes<={p_bin_file}", str(p_jpg_file))
47+
self.assertEqual(self.et.last_status, 0)
48+
49+
### read the binary data and compare ###
50+
with open(p_bin_file, 'rb') as f:
51+
bin_file_data = f.read()
52+
53+
# make sure it's the right amount of bytes wrote in originally
54+
self.assertEqual(len(bin_file_data), BIN_FILE_SIZE)
55+
56+
# get the custom tag
57+
written_bin_data = self.et.execute("-b", "-MyMakerNotes", str(p_jpg_file), raw_bytes=True)
58+
59+
# check that they are equal
60+
self.assertEqual(bin_file_data, written_bin_data)
61+
62+
63+
64+
65+
66+
67+
68+
69+
""" also write tests to allow executehelper to use the execute(raw_bytes) and test whether it works with check error """
70+
71+
""" test multi tags in mixed encoding with bytes """
72+
73+
""" drawback to change is that it would be slower as the param list is copied again internally """
74+

0 commit comments

Comments
 (0)