From ecd3905b99151855097a68a2e5f5f8825d53f7b2 Mon Sep 17 00:00:00 2001 From: Olivier Tilloy Date: Wed, 30 Jan 2008 22:36:43 +0100 Subject: Added some unit tests for basic functionalities. These tests will help spot regressions in the future. --- unittest/RationalTestCase.py | 62 +++++++++++++++++++++++ unittest/ReadMetadataTestCase.py | 103 +++++++++++++++++++++++++++++++++++++++ unittest/TestsRunner.py | 43 ++++++++++++++++ unittest/data/smiley1.jpg | Bin 0 -> 2969 bytes unittest/testutils.py | 42 ++++++++++++++++ 5 files changed, 250 insertions(+) create mode 100644 unittest/RationalTestCase.py create mode 100644 unittest/ReadMetadataTestCase.py create mode 100755 unittest/TestsRunner.py create mode 100644 unittest/data/smiley1.jpg create mode 100644 unittest/testutils.py diff --git a/unittest/RationalTestCase.py b/unittest/RationalTestCase.py new file mode 100644 index 0000000..7413399 --- /dev/null +++ b/unittest/RationalTestCase.py @@ -0,0 +1,62 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- + +# ****************************************************************************** +# +# Copyright (C) 2006-2007 Olivier Tilloy +# +# 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. +# +# +# File: RationalTestCase.py +# Author(s): Olivier Tilloy +# +# ****************************************************************************** + +import unittest +import pyexiv2 + +class RationalTestCase(unittest.TestCase): + + """ + Test case on the pyexiv2.Rational class. + """ + + def testConstructor(self): + """ + Test that the rational number constructor works as expected. + """ + r = pyexiv2.Rational(2, 1) + self.assertEqual(r.numerator, 2) + self.assertEqual(r.denominator, 1) + + def testException(self): + """ + Test that the constructor throws an exception when denominator is zero. + """ + self.assertRaises(ZeroDivisionError, pyexiv2.Rational, 1, 0) + + def testEquality(self): + """ + Test the (non) equality of two rational numbers. + """ + r1 = pyexiv2.Rational(2, 1) + r2 = pyexiv2.Rational(2, 1) + r3 = pyexiv2.Rational(3, 1) + self.assertEqual(r1, r2) + self.assertNotEqual(r1, r3) + diff --git a/unittest/ReadMetadataTestCase.py b/unittest/ReadMetadataTestCase.py new file mode 100644 index 0000000..4b85959 --- /dev/null +++ b/unittest/ReadMetadataTestCase.py @@ -0,0 +1,103 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- + +# ****************************************************************************** +# +# Copyright (C) 2006-2007 Olivier Tilloy +# +# 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. +# +# +# File: ReadMetadataTestCase.py +# Author(s): Olivier Tilloy +# +# ****************************************************************************** + +import unittest +import testutils +import os.path +import pyexiv2 +import datetime + +class ReadMetadataTestCase(unittest.TestCase): + + """ + Test case on reading the metadata contained in a file. + """ + + def checkTypeAndValue(self, tag, etype, evalue): + """ + Check the type and the value of a metadata tag against expected values. + + Keyword arguments: + tag -- the full name of the tag (eg. 'Exif.Image.DateTime') + etype -- the expected type of the tag value + evalue -- the expected value of the tag + """ + self.assertEqual(tag.__class__, etype) + self.assertEqual(tag, evalue) + + def testReadMetadata(self): + """ + Perform various tests on reading the metadata contained in a file. + """ + # Check that the reference file is not corrupted + filename = os.path.join('data', 'smiley1.jpg') + md5sum = 'c066958457c685853293058f9bf129c1' + self.assert_(testutils.CheckFileSum(filename, md5sum)) + + # Read the image metadata + image = pyexiv2.Image(filename) + image.readMetadata() + + # Exhaustive tests on the values of EXIF metadata + exifTags = [('Exif.Image.ImageDescription', str, 'Well it is a smiley that happens to be green'), + ('Exif.Image.XResolution', pyexiv2.Rational, pyexiv2.Rational(72, 1)), + ('Exif.Image.YResolution', pyexiv2.Rational, pyexiv2.Rational(72, 1)), + ('Exif.Image.ResolutionUnit', int, 2), + ('Exif.Image.Software', str, 'ImageReady'), + ('Exif.Image.DateTime', datetime.datetime, datetime.datetime(2004, 7, 13, 21, 23, 44)), + ('Exif.Image.Artist', str, 'No one'), + ('Exif.Image.Copyright', str, ''), + ('Exif.Image.ExifTag', long, 226L), + ('Exif.Photo.Flash', int, 80), + ('Exif.Photo.PixelXDimension', long, 167L), + ('Exif.Photo.PixelYDimension', long, 140L)] + self.assertEqual(image.exifKeys(), [tag[0] for tag in exifTags]) + for tag in exifTags: + self.checkTypeAndValue(image[tag[0]], tag[1], tag[2]) + + # Exhaustive tests on the values of IPTC metadata + iptcTags = [('Iptc.Application2.Caption', str, 'yelimS green faced dude (iptc caption)'), + ('Iptc.Application2.Writer', str, 'Nobody'), + ('Iptc.Application2.Byline', str, 'Its me'), + ('Iptc.Application2.ObjectName', str, 'GreeenDude'), + ('Iptc.Application2.DateCreated', datetime.date, datetime.date(2004, 7, 13)), + ('Iptc.Application2.City', str, 'Seattle'), + ('Iptc.Application2.ProvinceState', str, 'WA'), + ('Iptc.Application2.CountryName', str, 'USA'), + ('Iptc.Application2.Category', str, 'Things'), + ('Iptc.Application2.Keywords', tuple, ('Green', 'Smiley', 'Dude')), + ('Iptc.Application2.Copyright', str, '\xa9 2004 Nobody')] + self.assertEqual(image.iptcKeys(), [tag[0] for tag in iptcTags]) + for tag in iptcTags: + self.checkTypeAndValue(image[tag[0]], tag[1], tag[2]) + + # Test on the JPEG comment + self.checkTypeAndValue(image.getComment(), + str, 'This is a jpeg comment, about the green smiley.') + diff --git a/unittest/TestsRunner.py b/unittest/TestsRunner.py new file mode 100755 index 0000000..1e1ac6b --- /dev/null +++ b/unittest/TestsRunner.py @@ -0,0 +1,43 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- + +# ****************************************************************************** +# +# Copyright (C) 2006-2007 Olivier Tilloy +# +# 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. +# +# +# File: TestsRunner.py +# Author(s): Olivier Tilloy +# +# ****************************************************************************** + +import unittest + +# Test cases to run +from RationalTestCase import RationalTestCase +from ReadMetadataTestCase import ReadMetadataTestCase + +if __name__ == '__main__': + # Instantiate a test suite containing all the test cases + suite = unittest.TestSuite() + suite.addTest(unittest.defaultTestLoader.loadTestsFromTestCase(RationalTestCase)) + suite.addTest(unittest.defaultTestLoader.loadTestsFromTestCase(ReadMetadataTestCase)) + # Run the test suite + unittest.TextTestRunner(verbosity=2).run(suite) + diff --git a/unittest/data/smiley1.jpg b/unittest/data/smiley1.jpg new file mode 100644 index 0000000..58393b3 Binary files /dev/null and b/unittest/data/smiley1.jpg differ diff --git a/unittest/testutils.py b/unittest/testutils.py new file mode 100644 index 0000000..0bf34e2 --- /dev/null +++ b/unittest/testutils.py @@ -0,0 +1,42 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- + +# ****************************************************************************** +# +# Copyright (C) 2006-2007 Olivier Tilloy +# +# 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. +# +# +# File: testutils.py +# Author(s): Olivier Tilloy +# +# ****************************************************************************** + +import hashlib + +def CheckFileSum(filename, md5sum): + """ + Test the MD5 sum of a given file against the expected value. + + Keyword arguments: + filename -- the name of the file to test + md5sum -- the expected value of the MD5 sum of the file + """ + f = open(filename) + return (hashlib.md5(f.read()).hexdigest() == md5sum) + -- cgit