~osomon/pyexiv2/pyexiv2-0.3

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']
62
        self.assertEqual(tag.raw_value, 'charset="Ascii" deja vu')
63
        self.assertEqual(tag.value, u'deja vu')
64
341.1.9 by Olivier Tilloy
Renamed some unit tests.
65
    def test_read_unicode_little_endian(self):
341.1.12 by Olivier Tilloy
Factorize the test data checksums in a dictionary.
66
        m = self._read_image('usercomment-unicode-ii.jpg')
341.1.8 by Olivier Tilloy
Unit test reading comments off existing files.
67
        tag = m['Exif.Photo.UserComment']
341.1.15 by Olivier Tilloy
Starting from 0.20, exiv2 converts unicode comments to UTF-8.
68
        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.
69
        self.assertEqual(tag.value, u'déjà vu')
70
341.1.9 by Olivier Tilloy
Renamed some unit tests.
71
    def test_read_unicode_big_endian(self):
341.1.12 by Olivier Tilloy
Factorize the test data checksums in a dictionary.
72
        m = self._read_image('usercomment-unicode-mm.jpg')
341.1.8 by Olivier Tilloy
Unit test reading comments off existing files.
73
        tag = m['Exif.Photo.UserComment']
341.1.15 by Olivier Tilloy
Starting from 0.20, exiv2 converts unicode comments to UTF-8.
74
        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.
75
        self.assertEqual(tag.value, u'déjà vu')
76
341.1.11 by Olivier Tilloy
Unit test writing comments.
77
    def test_write_ascii(self):
341.1.12 by Olivier Tilloy
Factorize the test data checksums in a dictionary.
78
        m = self._read_image('usercomment-ascii.jpg')
341.1.11 by Olivier Tilloy
Unit test writing comments.
79
        tag = m['Exif.Photo.UserComment']
80
        tag.value = 'foo bar'
81
        self.assertEqual(tag.raw_value, 'charset="Ascii" foo bar')
82
        self.assertEqual(tag.value, u'foo bar')
83
84
    def test_write_unicode_over_ascii(self):
341.1.12 by Olivier Tilloy
Factorize the test data checksums in a dictionary.
85
        m = self._read_image('usercomment-ascii.jpg')
341.1.11 by Olivier Tilloy
Unit test writing comments.
86
        tag = m['Exif.Photo.UserComment']
87
        tag.value = u'déjà vu'
88
        self.assertEqual(tag.raw_value, 'déjà vu')
89
        self.assertEqual(tag.value, u'déjà vu')
90
91
    def test_write_unicode_little_endian(self):
341.1.12 by Olivier Tilloy
Factorize the test data checksums in a dictionary.
92
        m = self._read_image('usercomment-unicode-ii.jpg')
341.1.11 by Olivier Tilloy
Unit test writing comments.
93
        tag = m['Exif.Photo.UserComment']
94
        tag.value = u'DÉJÀ VU'
341.1.15 by Olivier Tilloy
Starting from 0.20, exiv2 converts unicode comments to UTF-8.
95
        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.
96
        self.assertEqual(tag.value, u'DÉJÀ VU')
97
98
    def test_write_unicode_big_endian(self):
341.1.12 by Olivier Tilloy
Factorize the test data checksums in a dictionary.
99
        m = self._read_image('usercomment-unicode-mm.jpg')
341.1.11 by Olivier Tilloy
Unit test writing comments.
100
        tag = m['Exif.Photo.UserComment']
101
        tag.value = u'DÉJÀ VU'
341.1.15 by Olivier Tilloy
Starting from 0.20, exiv2 converts unicode comments to UTF-8.
102
        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.
103
        self.assertEqual(tag.value, u'DÉJÀ VU')
104
341.1.14 by Olivier Tilloy
Unit test adding comments to an empty image an reading them back.
105
106
class TestUserCommentAdd(unittest.TestCase):
107
108
    def setUp(self):
109
        # Create an empty image file
110
        fd, self.pathname = tempfile.mkstemp(suffix='.jpg')
111
        os.write(fd, EMPTY_JPG_DATA)
112
        os.close(fd)
113
114
    def tearDown(self):
115
        os.remove(self.pathname)
116
117
    def _test_add_comment(self, value):
118
        metadata = ImageMetadata(self.pathname)
119
        metadata.read()
120
        key = 'Exif.Photo.UserComment'
121
        metadata[key] = value
122
        metadata.write()
123
124
        metadata = ImageMetadata(self.pathname)
125
        metadata.read()
126
        self.assert_(key in metadata.exif_keys)
127
        tag = metadata[key]
128
        self.assertEqual(tag.value, value)
129
130
    def test_add_comment_ascii(self):
131
        self._test_add_comment('deja vu')
132
133
    def test_add_comment_unicode(self):
134
        self._test_add_comment(u'déjà vu')
135