341.1.8
by Olivier Tilloy
Unit test reading comments off existing files. |
1 |
# -*- coding: utf-8 -*-
|
2 |
||
3 |
# ******************************************************************************
|
|
4 |
#
|
|
5 |
# Copyright (C) 2010 Olivier Tilloy <olivier@tilloy.net>
|
|
6 |
#
|
|
7 |
# This file is part of the pyexiv2 distribution.
|
|
8 |
#
|
|
9 |
# pyexiv2 is free software; you can redistribute it and/or
|
|
10 |
# modify it under the terms of the GNU General Public License
|
|
11 |
# as published by the Free Software Foundation; either version 2
|
|
12 |
# of the License, or (at your option) any later version.
|
|
13 |
#
|
|
14 |
# pyexiv2 is distributed in the hope that it will be useful,
|
|
15 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
16 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
17 |
# GNU General Public License for more details.
|
|
18 |
#
|
|
19 |
# You should have received a copy of the GNU General Public License
|
|
20 |
# along with pyexiv2; if not, write to the Free Software
|
|
21 |
# Foundation, Inc., 51 Franklin Street, 5th Floor, Boston, MA 02110-1301 USA.
|
|
22 |
#
|
|
23 |
# Author: Olivier Tilloy <olivier@tilloy.net>
|
|
24 |
#
|
|
25 |
# ******************************************************************************
|
|
26 |
||
27 |
from pyexiv2.metadata import ImageMetadata |
|
28 |
||
29 |
import unittest |
|
30 |
import testutils |
|
341.1.14
by Olivier Tilloy
Unit test adding comments to an empty image an reading them back. |
31 |
import os |
32 |
import tempfile |
|
343
by Olivier Tilloy
Moved EMPTY_JPG_DATA to the testutils module. |
33 |
from testutils import EMPTY_JPG_DATA |
341.1.14
by Olivier Tilloy
Unit test adding comments to an empty image an reading them back. |
34 |
|
35 |
||
36 |
class TestUserCommentReadWrite(unittest.TestCase): |
|
341.1.8
by Olivier Tilloy
Unit test reading comments off existing files. |
37 |
|
341.1.12
by Olivier Tilloy
Factorize the test data checksums in a dictionary. |
38 |
checksums = { |
39 |
'usercomment-ascii.jpg': 'ad29ac65fb6f63c8361aaed6cb02f8c7', |
|
40 |
'usercomment-unicode-ii.jpg': '13b7cc09129a8677f2cf18634f5abd3c', |
|
41 |
'usercomment-unicode-mm.jpg': '7addfed7823c556ba489cd4ab2037200', |
|
42 |
}
|
|
43 |
||
44 |
def _read_image(self, filename): |
|
341.1.8
by Olivier Tilloy
Unit test reading comments off existing files. |
45 |
filepath = testutils.get_absolute_file_path(os.path.join('data', filename)) |
341.1.12
by Olivier Tilloy
Factorize the test data checksums in a dictionary. |
46 |
self.assert_(testutils.CheckFileSum(filepath, self.checksums[filename])) |
341.1.8
by Olivier Tilloy
Unit test reading comments off existing files. |
47 |
m = ImageMetadata(filepath) |
48 |
m.read() |
|
49 |
return m |
|
50 |
||
341.1.15
by Olivier Tilloy
Starting from 0.20, exiv2 converts unicode comments to UTF-8. |
51 |
def _expected_raw_value(self, endianness, value): |
52 |
from pyexiv2 import __exiv2_version__ |
|
53 |
if __exiv2_version__ >= '0.20': |
|
54 |
return value |
|
55 |
else: |
|
56 |
encodings = {'ii': 'utf-16le', 'mm': 'utf-16be'} |
|
57 |
return value.decode('utf-8').encode(encodings[endianness]) |
|
58 |
||
341.1.9
by Olivier Tilloy
Renamed some unit tests. |
59 |
def test_read_ascii(self): |
341.1.12
by Olivier Tilloy
Factorize the test data checksums in a dictionary. |
60 |
m = self._read_image('usercomment-ascii.jpg') |
341.1.8
by Olivier Tilloy
Unit test reading comments off existing files. |
61 |
tag = m['Exif.Photo.UserComment'] |
363.1.4
by Olivier Tilloy
Add some sanity checks on the types of the UserComment tags. |
62 |
self.assertEqual(tag.type, 'Comment') |
341.1.8
by Olivier Tilloy
Unit test reading comments off existing files. |
63 |
self.assertEqual(tag.raw_value, 'charset="Ascii" deja vu') |
64 |
self.assertEqual(tag.value, u'deja vu') |
|
65 |
||
341.1.9
by Olivier Tilloy
Renamed some unit tests. |
66 |
def test_read_unicode_little_endian(self): |
341.1.12
by Olivier Tilloy
Factorize the test data checksums in a dictionary. |
67 |
m = self._read_image('usercomment-unicode-ii.jpg') |
341.1.8
by Olivier Tilloy
Unit test reading comments off existing files. |
68 |
tag = m['Exif.Photo.UserComment'] |
363.1.4
by Olivier Tilloy
Add some sanity checks on the types of the UserComment tags. |
69 |
self.assertEqual(tag.type, 'Comment') |
341.1.15
by Olivier Tilloy
Starting from 0.20, exiv2 converts unicode comments to UTF-8. |
70 |
self.assertEqual(tag.raw_value, 'charset="Unicode" %s' % self._expected_raw_value('ii', 'déjà vu')) |
341.1.8
by Olivier Tilloy
Unit test reading comments off existing files. |
71 |
self.assertEqual(tag.value, u'déjà vu') |
72 |
||
341.1.9
by Olivier Tilloy
Renamed some unit tests. |
73 |
def test_read_unicode_big_endian(self): |
341.1.12
by Olivier Tilloy
Factorize the test data checksums in a dictionary. |
74 |
m = self._read_image('usercomment-unicode-mm.jpg') |
341.1.8
by Olivier Tilloy
Unit test reading comments off existing files. |
75 |
tag = m['Exif.Photo.UserComment'] |
363.1.4
by Olivier Tilloy
Add some sanity checks on the types of the UserComment tags. |
76 |
self.assertEqual(tag.type, 'Comment') |
341.1.15
by Olivier Tilloy
Starting from 0.20, exiv2 converts unicode comments to UTF-8. |
77 |
self.assertEqual(tag.raw_value, 'charset="Unicode" %s' % self._expected_raw_value('mm', 'déjà vu')) |
341.1.8
by Olivier Tilloy
Unit test reading comments off existing files. |
78 |
self.assertEqual(tag.value, u'déjà vu') |
79 |
||
341.1.11
by Olivier Tilloy
Unit test writing comments. |
80 |
def test_write_ascii(self): |
341.1.12
by Olivier Tilloy
Factorize the test data checksums in a dictionary. |
81 |
m = self._read_image('usercomment-ascii.jpg') |
341.1.11
by Olivier Tilloy
Unit test writing comments. |
82 |
tag = m['Exif.Photo.UserComment'] |
363.1.4
by Olivier Tilloy
Add some sanity checks on the types of the UserComment tags. |
83 |
self.assertEqual(tag.type, 'Comment') |
341.1.11
by Olivier Tilloy
Unit test writing comments. |
84 |
tag.value = 'foo bar' |
85 |
self.assertEqual(tag.raw_value, 'charset="Ascii" foo bar') |
|
86 |
self.assertEqual(tag.value, u'foo bar') |
|
87 |
||
88 |
def test_write_unicode_over_ascii(self): |
|
341.1.12
by Olivier Tilloy
Factorize the test data checksums in a dictionary. |
89 |
m = self._read_image('usercomment-ascii.jpg') |
341.1.11
by Olivier Tilloy
Unit test writing comments. |
90 |
tag = m['Exif.Photo.UserComment'] |
363.1.4
by Olivier Tilloy
Add some sanity checks on the types of the UserComment tags. |
91 |
self.assertEqual(tag.type, 'Comment') |
341.1.11
by Olivier Tilloy
Unit test writing comments. |
92 |
tag.value = u'déjà vu' |
93 |
self.assertEqual(tag.raw_value, 'déjà vu') |
|
94 |
self.assertEqual(tag.value, u'déjà vu') |
|
95 |
||
96 |
def test_write_unicode_little_endian(self): |
|
341.1.12
by Olivier Tilloy
Factorize the test data checksums in a dictionary. |
97 |
m = self._read_image('usercomment-unicode-ii.jpg') |
341.1.11
by Olivier Tilloy
Unit test writing comments. |
98 |
tag = m['Exif.Photo.UserComment'] |
363.1.4
by Olivier Tilloy
Add some sanity checks on the types of the UserComment tags. |
99 |
self.assertEqual(tag.type, 'Comment') |
341.1.11
by Olivier Tilloy
Unit test writing comments. |
100 |
tag.value = u'DÉJÀ VU' |
341.1.15
by Olivier Tilloy
Starting from 0.20, exiv2 converts unicode comments to UTF-8. |
101 |
self.assertEqual(tag.raw_value, 'charset="Unicode" %s' % self._expected_raw_value('ii', 'DÉJÀ VU')) |
341.1.11
by Olivier Tilloy
Unit test writing comments. |
102 |
self.assertEqual(tag.value, u'DÉJÀ VU') |
103 |
||
104 |
def test_write_unicode_big_endian(self): |
|
341.1.12
by Olivier Tilloy
Factorize the test data checksums in a dictionary. |
105 |
m = self._read_image('usercomment-unicode-mm.jpg') |
341.1.11
by Olivier Tilloy
Unit test writing comments. |
106 |
tag = m['Exif.Photo.UserComment'] |
363.1.4
by Olivier Tilloy
Add some sanity checks on the types of the UserComment tags. |
107 |
self.assertEqual(tag.type, 'Comment') |
341.1.11
by Olivier Tilloy
Unit test writing comments. |
108 |
tag.value = u'DÉJÀ VU' |
341.1.15
by Olivier Tilloy
Starting from 0.20, exiv2 converts unicode comments to UTF-8. |
109 |
self.assertEqual(tag.raw_value, 'charset="Unicode" %s' % self._expected_raw_value('mm', 'DÉJÀ VU')) |
341.1.11
by Olivier Tilloy
Unit test writing comments. |
110 |
self.assertEqual(tag.value, u'DÉJÀ VU') |
111 |
||
341.1.14
by Olivier Tilloy
Unit test adding comments to an empty image an reading them back. |
112 |
|
113 |
class TestUserCommentAdd(unittest.TestCase): |
|
114 |
||
115 |
def setUp(self): |
|
116 |
# Create an empty image file
|
|
117 |
fd, self.pathname = tempfile.mkstemp(suffix='.jpg') |
|
118 |
os.write(fd, EMPTY_JPG_DATA) |
|
119 |
os.close(fd) |
|
120 |
||
121 |
def tearDown(self): |
|
122 |
os.remove(self.pathname) |
|
123 |
||
124 |
def _test_add_comment(self, value): |
|
125 |
metadata = ImageMetadata(self.pathname) |
|
126 |
metadata.read() |
|
127 |
key = 'Exif.Photo.UserComment' |
|
128 |
metadata[key] = value |
|
129 |
metadata.write() |
|
130 |
||
131 |
metadata = ImageMetadata(self.pathname) |
|
132 |
metadata.read() |
|
133 |
self.assert_(key in metadata.exif_keys) |
|
134 |
tag = metadata[key] |
|
363.1.4
by Olivier Tilloy
Add some sanity checks on the types of the UserComment tags. |
135 |
self.assertEqual(tag.type, 'Comment') |
341.1.14
by Olivier Tilloy
Unit test adding comments to an empty image an reading them back. |
136 |
self.assertEqual(tag.value, value) |
137 |
||
138 |
def test_add_comment_ascii(self): |
|
139 |
self._test_add_comment('deja vu') |
|
140 |
||
141 |
def test_add_comment_unicode(self): |
|
142 |
self._test_add_comment(u'déjà vu') |
|
143 |