1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
|
# -*- coding: utf-8 -*-
# ******************************************************************************
#
# Copyright (C) 2009 Olivier Tilloy <olivier@tilloy.net>
#
# This file is part of the pyexiv2 distribution.
#
# pyexiv2 is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# pyexiv2 is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with pyexiv2; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, 5th Floor, Boston, MA 02110-1301 USA.
#
# Author: Olivier Tilloy <olivier@tilloy.net>
#
# ******************************************************************************
import unittest
from pyexiv2.exif import ExifTag, ExifValueError
from pyexiv2.utils import Rational
import datetime
class ExifTagMock(ExifTag):
def __init__(self, key, type, fvalue=''):
super(ExifTagMock, self).__init__(key, '', '', '', type, '', fvalue)
def _init_values(self):
pass
class ImageMetadataMock(object):
tags = {}
def _set_exif_tag_value(self, key, value):
self.tags[key] = value
def _delete_exif_tag(self, key):
try:
del self.tags[key]
except KeyError:
pass
class TestExifTag(unittest.TestCase):
def test_convert_to_python_ascii(self):
type = 'Ascii'
# Valid values: datetimes
tag = ExifTagMock('Exif.Image.DateTime', type)
self.assertEqual(tag._convert_to_python('2009-03-01 12:46:51'),
datetime.datetime(2009, 03, 01, 12, 46, 51))
self.assertEqual(tag._convert_to_python('2009:03:01 12:46:51'),
datetime.datetime(2009, 03, 01, 12, 46, 51))
self.assertEqual(tag._convert_to_python('2009-03-01T12:46:51Z'),
datetime.datetime(2009, 03, 01, 12, 46, 51))
# Valid values: dates
tag = ExifTagMock('Exif.GPSInfo.GPSDateStamp', type)
self.assertEqual(tag._convert_to_python('2009:08:04'),
datetime.date(2009, 8, 4))
# Valid values: strings
tag = ExifTagMock('Exif.Image.Copyright', type)
self.assertEqual(tag._convert_to_python('Some text.'), 'Some text.')
self.assertEqual(tag._convert_to_python(u'Some text with exotic chàräctérʐ.'),
u'Some text with exotic chàräctérʐ.')
# Invalid values: datetimes
tag = ExifTagMock('Exif.Image.DateTime', type)
self.assertEqual(tag._convert_to_python('2009-13-01 12:46:51'),
u'2009-13-01 12:46:51')
self.assertEqual(tag._convert_to_python('2009-12-01'), u'2009-12-01')
# Invalid values: dates
tag = ExifTagMock('Exif.GPSInfo.GPSDateStamp', type)
self.assertEqual(tag._convert_to_python('2009:13:01'), u'2009:13:01')
self.assertEqual(tag._convert_to_python('2009-12-01'), u'2009-12-01')
def test_convert_to_string_ascii(self):
type = 'Ascii'
# Valid values: datetimes
tag = ExifTagMock('Exif.Image.DateTime', type)
self.assertEqual(tag._convert_to_string(datetime.datetime(2009, 03, 01, 12, 54, 28)),
'2009:03:01 12:54:28')
self.assertEqual(tag._convert_to_string(datetime.date(2009, 03, 01)),
'2009:03:01 00:00:00')
# Valid values: dates
tag = ExifTagMock('Exif.GPSInfo.GPSDateStamp', type)
self.assertEqual(tag._convert_to_string(datetime.date(2009, 03, 01)),
'2009:03:01')
# Valid values: strings
tag = ExifTagMock('Exif.Image.Copyright', type)
self.assertEqual(tag._convert_to_string(u'Some text'), 'Some text')
self.assertEqual(tag._convert_to_string(u'Some text with exotic chàräctérʐ.'),
'Some text with exotic chàräctérʐ.')
self.assertEqual(tag._convert_to_string('Some text with exotic chàräctérʐ.'),
'Some text with exotic chàräctérʐ.')
# Invalid values
self.failUnlessRaises(ExifValueError, tag._convert_to_string, None)
def test_convert_to_python_byte(self):
type = 'Byte'
# Valid values
tag = ExifTagMock('Exif.GPSInfo.GPSVersionID', type)
self.assertEqual(tag._convert_to_python('D'), 'D')
def test_convert_to_string_byte(self):
type = 'Byte'
# Valid values
tag = ExifTagMock('Exif.GPSInfo.GPSVersionID', type)
self.assertEqual(tag._convert_to_string('Some text'), 'Some text')
self.assertEqual(tag._convert_to_string(u'Some text'), 'Some text')
# Invalid values
self.failUnlessRaises(ExifValueError, tag._convert_to_string, None)
def test_convert_to_python_short(self):
type = 'Short'
# Valid values
tag = ExifTagMock('Exif.Image.BitsPerSample', type)
self.assertEqual(tag._convert_to_python('8'), 8)
self.assertEqual(tag._convert_to_python('+5628'), 5628)
# Invalid values
self.failUnlessRaises(ExifValueError, tag._convert_to_python, 'abc')
self.failUnlessRaises(ExifValueError, tag._convert_to_python, '5,64')
self.failUnlessRaises(ExifValueError, tag._convert_to_python, '47.0001')
self.failUnlessRaises(ExifValueError, tag._convert_to_python, '1E3')
def test_convert_to_string_short(self):
type = 'Short'
# Valid values
tag = ExifTagMock('Exif.Image.BitsPerSample', type)
self.assertEqual(tag._convert_to_string(123), '123')
# Invalid values
self.failUnlessRaises(ExifValueError, tag._convert_to_string, -57)
self.failUnlessRaises(ExifValueError, tag._convert_to_string, 'invalid')
self.failUnlessRaises(ExifValueError, tag._convert_to_string, 3.14)
def test_convert_to_python_long(self):
type = 'Long'
# Valid values
tag = ExifTagMock('Exif.Image.ImageWidth', type)
self.assertEqual(tag._convert_to_python('8'), 8)
self.assertEqual(tag._convert_to_python('+5628'), 5628)
# Invalid values
self.failUnlessRaises(ExifValueError, tag._convert_to_python, 'abc')
self.failUnlessRaises(ExifValueError, tag._convert_to_python, '5,64')
self.failUnlessRaises(ExifValueError, tag._convert_to_python, '47.0001')
self.failUnlessRaises(ExifValueError, tag._convert_to_python, '1E3')
def test_convert_to_string_long(self):
type = 'Long'
# Valid values
tag = ExifTagMock('Exif.Image.ImageWidth', type)
self.assertEqual(tag._convert_to_string(123), '123')
self.assertEqual(tag._convert_to_string(678024), '678024')
# Invalid values
self.failUnlessRaises(ExifValueError, tag._convert_to_string, -57)
self.failUnlessRaises(ExifValueError, tag._convert_to_string, 'invalid')
self.failUnlessRaises(ExifValueError, tag._convert_to_string, 3.14)
def test_convert_to_python_slong(self):
type = 'SLong'
# Valid values
tag = ExifTagMock('Exif.OlympusCs.ManometerReading', type)
self.assertEqual(tag._convert_to_python('23'), 23)
self.assertEqual(tag._convert_to_python('+5628'), 5628)
self.assertEqual(tag._convert_to_python('-437'), -437)
# Invalid values
self.failUnlessRaises(ExifValueError, tag._convert_to_python, 'abc')
self.failUnlessRaises(ExifValueError, tag._convert_to_python, '5,64')
self.failUnlessRaises(ExifValueError, tag._convert_to_python, '47.0001')
self.failUnlessRaises(ExifValueError, tag._convert_to_python, '1E3')
def test_convert_to_string_slong(self):
type = 'SLong'
# Valid values
tag = ExifTagMock('Exif.OlympusCs.ManometerReading', type)
self.assertEqual(tag._convert_to_string(123), '123')
self.assertEqual(tag._convert_to_string(678024), '678024')
self.assertEqual(tag._convert_to_string(-437), '-437')
# Invalid values
self.failUnlessRaises(ExifValueError, tag._convert_to_string, 'invalid')
self.failUnlessRaises(ExifValueError, tag._convert_to_string, 3.14)
def test_convert_to_python_rational(self):
type = 'Rational'
# Valid values
tag = ExifTagMock('Exif.Image.XResolution', type)
self.assertEqual(tag._convert_to_python('5/3'), Rational(5, 3))
# Invalid values
self.failUnlessRaises(ExifValueError, tag._convert_to_python, 'invalid')
self.failUnlessRaises(ExifValueError, tag._convert_to_python, '-5/3')
self.failUnlessRaises(ExifValueError, tag._convert_to_python, '5 / 3')
self.failUnlessRaises(ExifValueError, tag._convert_to_python, '5/-3')
def test_convert_to_string_rational(self):
type = 'Rational'
# Valid values
tag = ExifTagMock('Exif.Image.XResolution', type)
self.assertEqual(tag._convert_to_string(Rational(5, 3)), '5/3')
# Invalid values
self.failUnlessRaises(ExifValueError, tag._convert_to_string, 'invalid')
self.failUnlessRaises(ExifValueError,
tag._convert_to_string, Rational(-5, 3))
def test_convert_to_python_srational(self):
type = 'SRational'
# Valid values
tag = ExifTagMock('Exif.Image.BaselineExposure', type)
self.assertEqual(tag._convert_to_python('5/3'), Rational(5, 3))
self.assertEqual(tag._convert_to_python('-5/3'), Rational(-5, 3))
# Invalid values
self.failUnlessRaises(ExifValueError, tag._convert_to_python, 'invalid')
self.failUnlessRaises(ExifValueError, tag._convert_to_python, '5 / 3')
self.failUnlessRaises(ExifValueError, tag._convert_to_python, '5/-3')
def test_convert_to_string_srational(self):
type = 'SRational'
# Valid values
tag = ExifTagMock('Exif.Image.BaselineExposure', type)
self.assertEqual(tag._convert_to_string(Rational(5, 3)), '5/3')
self.assertEqual(tag._convert_to_string(Rational(-5, 3)), '-5/3')
# Invalid values
self.failUnlessRaises(ExifValueError, tag._convert_to_string, 'invalid')
def test_convert_to_python_undefined(self):
type = 'Undefined'
# Valid values
tag = ExifTagMock('Exif.Photo.ExifVersion', type, '1.00')
self.assertEqual(tag._convert_to_python('48 49 48 48 '), u'1.00')
tag = ExifTagMock('Exif.Photo.DeviceSettingDescription', type, 'Digital still camera')
self.assertEqual(tag._convert_to_python('3 '), u'Digital still camera')
def test_convert_to_string_undefined(self):
type = 'Undefined'
# Valid values
tag = ExifTagMock('Exif.Photo.ExifVersion', type)
self.assertEqual(tag._convert_to_string('48 49 48 48 '), '48 49 48 48 ')
self.assertEqual(tag._convert_to_string(u'48 49 48 48 '), '48 49 48 48 ')
# Invalid values
self.failUnlessRaises(ExifValueError, tag._convert_to_string, 3)
def test_set_value_no_metadata(self):
tag = ExifTag('Exif.Thumbnail.Orientation', 'Orientation',
'Orientation', 'The image orientation viewed in terms ' \
'of rows and columns.', 'Short', '1', 'top, left')
old_value = tag.value
tag.value = 2
self.failIfEqual(tag.value, old_value)
def test_set_value_with_metadata(self):
tag = ExifTag('Exif.Thumbnail.Orientation', 'Orientation',
'Orientation', 'The image orientation viewed in terms ' \
'of rows and columns.', 'Short', '1', 'top, left')
tag.metadata = ImageMetadataMock()
old_value = tag.value
tag.value = 2
self.failIfEqual(tag.value, old_value)
self.assertEqual(tag.metadata.tags[tag.key], '2')
def test_del_value_no_metadata(self):
tag = ExifTag('Exif.Thumbnail.Orientation', 'Orientation',
'Orientation', 'The image orientation viewed in terms ' \
'of rows and columns.', 'Short', '1', 'top, left')
del tag.value
self.failIf(hasattr(tag, 'value'))
def test_del_value_with_metadata(self):
tag = ExifTag('Exif.Thumbnail.Orientation', 'Orientation',
'Orientation', 'The image orientation viewed in terms ' \
'of rows and columns.', 'Short', '1', 'top, left')
tag.metadata = ImageMetadataMock()
tag.metadata._set_exif_tag_value(tag.key, str(tag))
self.assertEqual(tag.metadata.tags, {tag.key: '1'})
del tag.value
self.failIf(hasattr(tag, 'value'))
self.failIf(tag.metadata.tags.has_key(tag.key))
|