~osomon/pyexiv2/pyexiv2-0.3

« back to all changes in this revision

Viewing changes to unittest/ReadMetadataTestCase.py

  • Committer: Olivier Tilloy
  • Date: 2008-01-30 21:36:43 UTC
  • Revision ID: olivier@tilloy.net-20080130213643-skrpcsnpjsivu50h
Added some unit tests for basic functionalities. These tests will help spot regressions in the future.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python
 
2
# -*- coding: utf-8 -*-
 
3
 
 
4
# ******************************************************************************
 
5
#
 
6
# Copyright (C) 2006-2007 Olivier Tilloy <olivier@tilloy.net>
 
7
#
 
8
# This file is part of the pyexiv2 distribution.
 
9
#
 
10
# pyexiv2 is free software; you can redistribute it and/or
 
11
# modify it under the terms of the GNU General Public License
 
12
# as published by the Free Software Foundation; either version 2
 
13
# of the License, or (at your option) any later version.
 
14
#
 
15
# pyexiv2 is distributed in the hope that it will be useful,
 
16
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
17
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
18
# GNU General Public License for more details.
 
19
#
 
20
# You should have received a copy of the GNU General Public License
 
21
# along with pyexiv2; if not, write to the Free Software
 
22
# Foundation, Inc., 51 Franklin Street, 5th Floor, Boston, MA 02110-1301 USA.
 
23
#
 
24
#
 
25
# File:      ReadMetadataTestCase.py
 
26
# Author(s): Olivier Tilloy <olivier@tilloy.net>
 
27
#
 
28
# ******************************************************************************
 
29
 
 
30
import unittest
 
31
import testutils
 
32
import os.path
 
33
import pyexiv2
 
34
import datetime
 
35
 
 
36
class ReadMetadataTestCase(unittest.TestCase):
 
37
 
 
38
    """
 
39
    Test case on reading the metadata contained in a file.
 
40
    """
 
41
 
 
42
    def checkTypeAndValue(self, tag, etype, evalue):
 
43
        """
 
44
        Check the type and the value of a metadata tag against expected values.
 
45
 
 
46
        Keyword arguments:
 
47
        tag -- the full name of the tag (eg. 'Exif.Image.DateTime')
 
48
        etype -- the expected type of the tag value
 
49
        evalue -- the expected value of the tag
 
50
        """
 
51
        self.assertEqual(tag.__class__, etype)
 
52
        self.assertEqual(tag, evalue)
 
53
 
 
54
    def testReadMetadata(self):
 
55
        """
 
56
        Perform various tests on reading the metadata contained in a file.
 
57
        """
 
58
        # Check that the reference file is not corrupted
 
59
        filename = os.path.join('data', 'smiley1.jpg')
 
60
        md5sum = 'c066958457c685853293058f9bf129c1'
 
61
        self.assert_(testutils.CheckFileSum(filename, md5sum))
 
62
 
 
63
        # Read the image metadata
 
64
        image = pyexiv2.Image(filename)
 
65
        image.readMetadata()
 
66
 
 
67
        # Exhaustive tests on the values of EXIF metadata
 
68
        exifTags = [('Exif.Image.ImageDescription', str, 'Well it is a smiley that happens to be green'),
 
69
                    ('Exif.Image.XResolution', pyexiv2.Rational, pyexiv2.Rational(72, 1)),
 
70
                    ('Exif.Image.YResolution', pyexiv2.Rational, pyexiv2.Rational(72, 1)),
 
71
                    ('Exif.Image.ResolutionUnit', int, 2),
 
72
                    ('Exif.Image.Software', str, 'ImageReady'),
 
73
                    ('Exif.Image.DateTime', datetime.datetime, datetime.datetime(2004, 7, 13, 21, 23, 44)),
 
74
                    ('Exif.Image.Artist', str, 'No one'),
 
75
                    ('Exif.Image.Copyright', str, ''),
 
76
                    ('Exif.Image.ExifTag', long, 226L),
 
77
                    ('Exif.Photo.Flash', int, 80),
 
78
                    ('Exif.Photo.PixelXDimension', long, 167L),
 
79
                    ('Exif.Photo.PixelYDimension', long, 140L)]
 
80
        self.assertEqual(image.exifKeys(), [tag[0] for tag in exifTags])
 
81
        for tag in exifTags:
 
82
            self.checkTypeAndValue(image[tag[0]], tag[1], tag[2])
 
83
 
 
84
        # Exhaustive tests on the values of IPTC metadata
 
85
        iptcTags = [('Iptc.Application2.Caption', str, 'yelimS green faced dude (iptc caption)'),
 
86
                    ('Iptc.Application2.Writer', str, 'Nobody'),
 
87
                    ('Iptc.Application2.Byline', str, 'Its me'),
 
88
                    ('Iptc.Application2.ObjectName', str, 'GreeenDude'),
 
89
                    ('Iptc.Application2.DateCreated', datetime.date, datetime.date(2004, 7, 13)),
 
90
                    ('Iptc.Application2.City', str, 'Seattle'),
 
91
                    ('Iptc.Application2.ProvinceState', str, 'WA'),
 
92
                    ('Iptc.Application2.CountryName', str, 'USA'),
 
93
                    ('Iptc.Application2.Category', str, 'Things'),
 
94
                    ('Iptc.Application2.Keywords', tuple, ('Green', 'Smiley', 'Dude')),
 
95
                    ('Iptc.Application2.Copyright', str, '\xa9 2004 Nobody')]
 
96
        self.assertEqual(image.iptcKeys(), [tag[0] for tag in iptcTags])
 
97
        for tag in iptcTags:
 
98
            self.checkTypeAndValue(image[tag[0]], tag[1], tag[2])
 
99
 
 
100
        # Test on the JPEG comment
 
101
        self.checkTypeAndValue(image.getComment(),
 
102
            str, 'This is a jpeg comment, about the green smiley.')
 
103