aboutsummaryrefslogtreecommitdiffstats
path: root/unittest/iptc.py
blob: 38826d8a304f18765b73a07c1a0a16cfb2f25550 (plain) (blame)
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
# -*- 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 import IptcTag, IptcValueError, FixedOffset
import datetime


class ImageMetadataMock(object):

    tags = {}

    def _set_iptc_tag_values(self, key, values):
        self.tags[key] = values

    def _delete_iptc_tag(self, key):
        try:
            del self.tags[key]
        except KeyError:
            pass


class TestIptcTag(unittest.TestCase):

    def test_convert_to_python_short(self):
        xtype = 'Short'
        # Valid values
        self.assertEqual(IptcTag._convert_to_python('23', xtype), 23)
        self.assertEqual(IptcTag._convert_to_python('+5628', xtype), 5628)
        self.assertEqual(IptcTag._convert_to_python('-4', xtype), -4)
        # Invalid values
        self.failUnlessRaises(IptcValueError, IptcTag._convert_to_python, 'abc', xtype)
        self.failUnlessRaises(IptcValueError, IptcTag._convert_to_python, '5,64', xtype)
        self.failUnlessRaises(IptcValueError, IptcTag._convert_to_python, '47.0001', xtype)
        self.failUnlessRaises(IptcValueError, IptcTag._convert_to_python, '1E3', xtype)

    def test_convert_to_string_short(self):
        xtype = 'Short'
        # Valid values
        self.assertEqual(IptcTag._convert_to_string(123, xtype), '123')
        self.assertEqual(IptcTag._convert_to_string(-57, xtype), '-57')
        # Invalid values
        self.failUnlessRaises(IptcValueError, IptcTag._convert_to_string, 'invalid', xtype)
        self.failUnlessRaises(IptcValueError, IptcTag._convert_to_string, 3.14, xtype)

    def test_convert_to_python_string(self):
        xtype = 'String'
        # Valid values
        self.assertEqual(IptcTag._convert_to_python('Some text.', xtype), u'Some text.')
        self.assertEqual(IptcTag._convert_to_python('Some text with exotic chàräctérʐ.', xtype),
                         u'Some text with exotic chàräctérʐ.')
        # Invalid values
        self.failUnlessRaises(IptcValueError, IptcTag._convert_to_python, None, xtype)

    def test_convert_to_string_string(self):
        xtype = 'String'
        # Valid values
        self.assertEqual(IptcTag._convert_to_string(u'Some text', xtype), 'Some text')
        self.assertEqual(IptcTag._convert_to_string(u'Some text with exotic chàräctérʐ.', xtype),
                         'Some text with exotic chàräctérʐ.')
        self.assertEqual(IptcTag._convert_to_string('Some text with exotic chàräctérʐ.', xtype),
                         'Some text with exotic chàräctérʐ.')
        # Invalid values
        self.failUnlessRaises(IptcValueError, IptcTag._convert_to_string, None, xtype)

    def test_convert_to_python_date(self):
        xtype = 'Date'
        # Valid values
        self.assertEqual(IptcTag._convert_to_python('1999-10-13', xtype),
                         datetime.date(1999, 10, 13))
        # Invalid values
        self.failUnlessRaises(IptcValueError, IptcTag._convert_to_python, 'invalid', xtype)
        self.failUnlessRaises(IptcValueError, IptcTag._convert_to_python, '11/10/1983', xtype)
        self.failUnlessRaises(IptcValueError, IptcTag._convert_to_python, '-1000', xtype)
        self.failUnlessRaises(IptcValueError, IptcTag._convert_to_python, '2009-02', xtype)
        self.failUnlessRaises(IptcValueError, IptcTag._convert_to_python, '2009-10-32', xtype)
        self.failUnlessRaises(IptcValueError, IptcTag._convert_to_python, '2009-02-24T22:12:54', xtype)

    def test_convert_to_string_date(self):
        xtype = 'Date'
        # Valid values
        self.assertEqual(IptcTag._convert_to_string(datetime.date(2009, 2, 4), xtype),
                         '20090204')
        self.assertEqual(IptcTag._convert_to_string(datetime.datetime(1999, 10, 13), xtype),
                         '19991013')
        self.assertEqual(IptcTag._convert_to_string(datetime.datetime(2009, 2, 4), xtype),
                         '20090204')
        self.assertEqual(IptcTag._convert_to_string(datetime.datetime(2009, 2, 4, 10, 52, 37), xtype),
                         '20090204')
        # Invalid values
        self.failUnlessRaises(IptcValueError, IptcTag._convert_to_string, 'invalid', xtype)
        self.failUnlessRaises(IptcValueError, IptcTag._convert_to_string, None, xtype)

    def test_convert_to_python_time(self):
        xtype = 'Time'
        # Valid values
        self.assertEqual(IptcTag._convert_to_python('05:03:54+00:00', xtype),
                         datetime.time(5, 3, 54, tzinfo=FixedOffset()))
        self.assertEqual(IptcTag._convert_to_python('05:03:54+06:00', xtype),
                         datetime.time(5, 3, 54, tzinfo=FixedOffset('+', 6, 0)))
        self.assertEqual(IptcTag._convert_to_python('05:03:54-10:30', xtype),
                         datetime.time(5, 3, 54, tzinfo=FixedOffset('-', 10, 30)))
        # Invalid values
        self.failUnlessRaises(IptcValueError, IptcTag._convert_to_python, 'invalid', xtype)
        self.failUnlessRaises(IptcValueError, IptcTag._convert_to_python, '23:12:42', xtype)
        self.failUnlessRaises(IptcValueError, IptcTag._convert_to_python, '25:12:42+00:00', xtype)
        self.failUnlessRaises(IptcValueError, IptcTag._convert_to_python, '21:77:42+00:00', xtype)
        self.failUnlessRaises(IptcValueError, IptcTag._convert_to_python, '21:12:98+00:00', xtype)
        self.failUnlessRaises(IptcValueError, IptcTag._convert_to_python, '081242+0000', xtype)

    def test_convert_to_string_time(self):
        xtype = 'Time'
        # Valid values
        self.assertEqual(IptcTag._convert_to_string(datetime.time(10, 52, 4), xtype),
                         '105204+0000')
        self.assertEqual(IptcTag._convert_to_string(datetime.time(10, 52, 4, 574), xtype),
                         '105204+0000')
        self.assertEqual(IptcTag._convert_to_string(datetime.time(10, 52, 4, tzinfo=FixedOffset()), xtype),
                         '105204+0000')
        self.assertEqual(IptcTag._convert_to_string(datetime.time(10, 52, 4, tzinfo=FixedOffset('+', 5, 30)), xtype),
                         '105204+0530')
        self.assertEqual(IptcTag._convert_to_string(datetime.time(10, 52, 4, tzinfo=FixedOffset('-', 4, 0)), xtype),
                         '105204-0400')
        self.assertEqual(IptcTag._convert_to_string(datetime.datetime(2007, 2, 7, 10, 52, 4), xtype),
                         '105204+0000')
        self.assertEqual(IptcTag._convert_to_string(datetime.datetime(2007, 2, 7, 10, 52, 4, 478), xtype),
                         '105204+0000')
        self.assertEqual(IptcTag._convert_to_string(datetime.datetime(2007, 2, 7, 10, 52, 4, tzinfo=FixedOffset()), xtype),
                         '105204+0000')
        self.assertEqual(IptcTag._convert_to_string(datetime.datetime(2007, 2, 7, 10, 52, 4, tzinfo=FixedOffset('+', 5, 30)), xtype),
                         '105204+0530')
        self.assertEqual(IptcTag._convert_to_string(datetime.datetime(2007, 2, 7, 10, 52, 4, tzinfo=FixedOffset('-', 4, 0)), xtype),
                         '105204-0400')
        # Invalid values
        self.failUnlessRaises(IptcValueError, IptcTag._convert_to_python, 'invalid', xtype)

    def test_convert_to_python_undefined(self):
        xtype = 'Undefined'
        # Valid values
        self.assertEqual(IptcTag._convert_to_python('Some binary data.', xtype),
                         'Some binary data.')
        self.assertEqual(IptcTag._convert_to_python('�lj1�eEϟ�u����ᒻ;C(�SpI]���QI�}', xtype),
                         '�lj1�eEϟ�u����ᒻ;C(�SpI]���QI�}')

    def test_convert_to_string_undefined(self):
        xtype = 'Undefined'
        # Valid values
        self.assertEqual(IptcTag._convert_to_string('Some binary data.', xtype),
                         'Some binary data.')
        self.assertEqual(IptcTag._convert_to_string('�lj1�eEϟ�u����ᒻ;C(�SpI]���QI�}', xtype),
                         '�lj1�eEϟ�u����ᒻ;C(�SpI]���QI�}')
        # Invalid values
        self.failUnlessRaises(IptcValueError, IptcTag._convert_to_string, None, xtype)

    def test_set_values_no_metadata(self):
        tag = IptcTag('Iptc.Application2.City', 'City', 'City', 'Identifies ' \
                      'city of object data origin according to guidelines ' \
                      'established by the provider.', 'String', ['Seattle'])
        old_values = tag.values
        tag.values = ['Barcelona']
        self.failIfEqual(tag.values, old_values)

    def test_set_values_with_metadata(self):
        tag = IptcTag('Iptc.Application2.City', 'City', 'City', 'Identifies ' \
                      'city of object data origin according to guidelines ' \
                      'established by the provider.', 'String', ['Seattle'])
        tag.metadata = ImageMetadataMock()
        old_values = tag.values
        tag.values = ['Barcelona']
        self.failIfEqual(tag.values, old_values)
        self.assertEqual(tag.metadata.tags[tag.key], ['Barcelona'])

    def test_del_values_no_metadata(self):
        tag = IptcTag('Iptc.Application2.City', 'City', 'City', 'Identifies ' \
                      'city of object data origin according to guidelines ' \
                      'established by the provider.', 'String', ['Seattle'])
        del tag.values
        self.failIf(hasattr(tag, 'values'))

    def test_del_values_with_metadata(self):
        tag = IptcTag('Iptc.Application2.City', 'City', 'City', 'Identifies ' \
                      'city of object data origin according to guidelines ' \
                      'established by the provider.', 'String', ['Seattle'])
        tag.metadata = ImageMetadataMock()
        tag.metadata._set_iptc_tag_values(tag.key, tag.to_string())
        self.assertEqual(tag.metadata.tags, {tag.key: ['Seattle']})
        del tag.values
        self.failIf(hasattr(tag, 'values'))
        self.failIf(tag.metadata.tags.has_key(tag.key))