280.1.6
by Olivier Tilloy
Unit tests for the exposition of image buffers. |
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 |
import unittest |
|
28 |
import os.path |
|
29 |
import hashlib |
|
30 |
from datetime import datetime |
|
31 |
||
32 |
from pyexiv2.metadata import ImageMetadata |
|
33 |
||
34 |
import testutils |
|
35 |
||
36 |
||
37 |
class TestBuffer(unittest.TestCase): |
|
38 |
||
39 |
def setUp(self): |
|
301.1.1
by Olivier Tilloy
Allow running the unit tests from anywhere. |
40 |
filename = os.path.join('data', 'smiley1.jpg') |
41 |
self.filepath = testutils.get_absolute_file_path(filename) |
|
280.1.6
by Olivier Tilloy
Unit tests for the exposition of image buffers. |
42 |
self.md5sum = 'c066958457c685853293058f9bf129c1' |
301.1.1
by Olivier Tilloy
Allow running the unit tests from anywhere. |
43 |
self.assert_(testutils.CheckFileSum(self.filepath, self.md5sum)) |
280.1.6
by Olivier Tilloy
Unit tests for the exposition of image buffers. |
44 |
|
45 |
def _metadata_from_buffer(self): |
|
301.1.1
by Olivier Tilloy
Allow running the unit tests from anywhere. |
46 |
fd = open(self.filepath, 'rb') |
280.1.6
by Olivier Tilloy
Unit tests for the exposition of image buffers. |
47 |
data = fd.read() |
48 |
fd.close() |
|
49 |
return ImageMetadata.from_buffer(data) |
|
50 |
||
51 |
def test_from_file_and_from_buffer(self): |
|
52 |
# from file
|
|
301.1.1
by Olivier Tilloy
Allow running the unit tests from anywhere. |
53 |
m1 = ImageMetadata(self.filepath) |
280.1.6
by Olivier Tilloy
Unit tests for the exposition of image buffers. |
54 |
m1.read() |
55 |
self.assertEqual(hashlib.md5(m1.buffer).hexdigest(), self.md5sum) |
|
56 |
||
57 |
# from buffer
|
|
58 |
m2 = self._metadata_from_buffer() |
|
59 |
self.assertEqual(hashlib.md5(m2.buffer).hexdigest(), self.md5sum) |
|
60 |
||
61 |
def test_buffer_not_updated_until_write_called(self): |
|
62 |
m = self._metadata_from_buffer() |
|
63 |
m.read() |
|
64 |
self.assertEqual(hashlib.md5(m.buffer).hexdigest(), self.md5sum) |
|
65 |
||
66 |
# Modify the value of an EXIF tag
|
|
67 |
m['Exif.Image.DateTime'].value = datetime.today() |
|
68 |
# Check that the buffer is unchanged until write() is called
|
|
69 |
self.assertEqual(hashlib.md5(m.buffer).hexdigest(), self.md5sum) |
|
70 |
# Write back the changes
|
|
71 |
m.write() |
|
72 |
# Check that the buffer has changed
|
|
73 |
self.failIfEqual(hashlib.md5(m.buffer).hexdigest(), self.md5sum) |
|
74 |
||
75 |
def test_from_original_buffer(self): |
|
76 |
m1 = self._metadata_from_buffer() |
|
77 |
m2 = ImageMetadata.from_buffer(m1.buffer) |
|
78 |
self.assertEqual(hashlib.md5(m2.buffer).hexdigest(), self.md5sum) |
|
79 |
||
80 |
def test_from_modified_buffer(self): |
|
81 |
m1 = self._metadata_from_buffer() |
|
82 |
m1.read() |
|
83 |
key = 'Exif.Image.ImageDescription' |
|
84 |
value = 'my kingdom for a semiquaver' |
|
85 |
m1[key] = value |
|
86 |
m1.write() |
|
87 |
||
88 |
m2 = ImageMetadata.from_buffer(m1.buffer) |
|
89 |
m2.read() |
|
90 |
self.assertEqual(m2[key].value, value) |
|
91 |