diff options
author | Olivier Tilloy <olivier@tilloy.net> | 2009-01-22 09:27:00 +0100 |
---|---|---|
committer | Olivier Tilloy <olivier@tilloy.net> | 2009-01-22 09:27:00 +0100 |
commit | 579511a60f05d99827223fd062b072c83a4e9744 (patch) | |
tree | 381c49eabf7979fd1875397867d4edec5aa0447f | |
parent | 67a0fd074b4ec6109e143ee0e9be748316955b26 (diff) | |
download | pyexiv2-579511a60f05d99827223fd062b072c83a4e9744.tar.gz |
Unit tests for the conversion of XMP tags, started fixing the conversion of Dates.
-rw-r--r-- | src/pyexiv2.py | 41 | ||||
-rwxr-xr-x | unittest/TestsRunner.py | 38 | ||||
-rw-r--r-- | unittest/xmp.py | 91 |
3 files changed, 139 insertions, 31 deletions
diff --git a/src/pyexiv2.py b/src/pyexiv2.py index f614b27..8a8f362 100644 --- a/src/pyexiv2.py +++ b/src/pyexiv2.py @@ -144,6 +144,11 @@ class FixedOffset(datetime.tzinfo): r = '%s%02d:%02d' % (self.sign, self.hours, self.minutes) return r + def __equal__(self, other): + return (self.sign == other.sign) and (self.hours == other.hours) and \ + (self.minutes == other.minutes) + + def UndefinedToString(undefined): """ Convert an undefined string into its corresponding sequence of bytes. @@ -484,7 +489,8 @@ class XmpTag(MetadataTag): MetadataTag.__init__(self, key, name, label, description, type, values) # TODO: conversion of values to python types - def _convert_to_python(self, value, xtype): + @staticmethod + def _convert_to_python(value, xtype): """ Convert one single value to its corresponding python type. Do not handle sets and bags. @@ -504,29 +510,40 @@ class XmpTag(MetadataTag): elif xtype == 'Colorant': # TODO return value + elif xtype == 'Date': - match = self._date_re.match(value) + match = XmpTag._date_re.match(value) if match is None: - return + return value gd = match.groupdict() + if gd['month'] is not None: + month = int(gd['month']) + else: + month = 1 + if gd['day'] is not None: + day = int(gd['day']) + else: + day = 1 if gd['time'] is None: - return datetime.date(int(gd['year']), int(gd['month']) or 1, - int(gd['day']) or 1) + return datetime.date(int(gd['year']), month, day) else: + if gd['seconds'] is not None: + seconds = int(gd['seconds']) + else: + seconds = 0 if gd['decimal'] is not None: - microsecond = int(float('0.%s' % gd['decimal']) * 1E6) + microseconds = int(float('0.%s' % gd['decimal']) * 1E6) else: - microsecond = 0 + microseconds = 0 if gd['tzd'] == 'Z': tzinfo = FixedOffset() else: tzinfo = FixedOffset(gd['sign'], int(gd['ohours']), int(gd['ominutes'])) - return datetime.datetime(int(gd['year']), int(gd['month']), - int(gd['day']), int(gd['hours']), - int(gd['minutes']), - int(gd['seconds']) or 0, - microsecond, tzinfo) + return datetime.datetime(int(gd['year']), month, day, + int(gd['hours']), int(gd['minutes']), + seconds, microseconds, tzinfo) + elif xtype == 'Dimensions': # TODO return value diff --git a/unittest/TestsRunner.py b/unittest/TestsRunner.py index 011ce82..5e15d48 100755 --- a/unittest/TestsRunner.py +++ b/unittest/TestsRunner.py @@ -3,7 +3,7 @@ # ****************************************************************************** # -# Copyright (C) 2008 Olivier Tilloy <olivier@tilloy.net> +# Copyright (C) 2008-2009 Olivier Tilloy <olivier@tilloy.net> # # This file is part of the pyexiv2 distribution. # @@ -21,33 +21,33 @@ # 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 <olivier@tilloy.net> +# Author: Olivier Tilloy <olivier@tilloy.net> # # ****************************************************************************** import unittest # Test cases to run -from RationalTestCase import RationalTestCase -from ReadMetadataTestCase import ReadMetadataTestCase -from Bug146313_TestCase import Bug146313_TestCase -from Bug173387_TestCase import Bug173387_TestCase -from Bug175070_TestCase import Bug175070_TestCase -from Bug183332_TestCase import Bug183332_TestCase -from Bug183618_TestCase import Bug183618_TestCase +#from RationalTestCase import RationalTestCase +#from ReadMetadataTestCase import ReadMetadataTestCase +#from Bug146313_TestCase import Bug146313_TestCase +#from Bug173387_TestCase import Bug173387_TestCase +#from Bug175070_TestCase import Bug175070_TestCase +#from Bug183332_TestCase import Bug183332_TestCase +#from Bug183618_TestCase import Bug183618_TestCase +from xmp import TestXmpTag + 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)) - suite.addTest(unittest.defaultTestLoader.loadTestsFromTestCase(Bug146313_TestCase)) - suite.addTest(unittest.defaultTestLoader.loadTestsFromTestCase(Bug173387_TestCase)) - suite.addTest(unittest.defaultTestLoader.loadTestsFromTestCase(Bug175070_TestCase)) - suite.addTest(unittest.defaultTestLoader.loadTestsFromTestCase(Bug183332_TestCase)) - suite.addTest(unittest.defaultTestLoader.loadTestsFromTestCase(Bug183618_TestCase)) + #suite.addTest(unittest.defaultTestLoader.loadTestsFromTestCase(RationalTestCase)) + #suite.addTest(unittest.defaultTestLoader.loadTestsFromTestCase(ReadMetadataTestCase)) + #suite.addTest(unittest.defaultTestLoader.loadTestsFromTestCase(Bug146313_TestCase)) + #suite.addTest(unittest.defaultTestLoader.loadTestsFromTestCase(Bug173387_TestCase)) + #suite.addTest(unittest.defaultTestLoader.loadTestsFromTestCase(Bug175070_TestCase)) + #suite.addTest(unittest.defaultTestLoader.loadTestsFromTestCase(Bug183332_TestCase)) + #suite.addTest(unittest.defaultTestLoader.loadTestsFromTestCase(Bug183618_TestCase)) + suite.addTest(unittest.defaultTestLoader.loadTestsFromTestCase(TestXmpTag)) # Run the test suite unittest.TextTestRunner(verbosity=2).run(suite) - diff --git a/unittest/xmp.py b/unittest/xmp.py new file mode 100644 index 0000000..1ed8725 --- /dev/null +++ b/unittest/xmp.py @@ -0,0 +1,91 @@ +# -*- coding: utf-8 -*- + +# ****************************************************************************** +# +# Copyright (C) 2009 Olivier Tilloy <olivier@tilloy.net> +# +# 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. +# +# Author: Olivier Tilloy <olivier@tilloy.net> +# +# ****************************************************************************** + +import unittest +from pyexiv2 import XmpTag, FixedOffset +import datetime + + +class TestXmpTag(unittest.TestCase): + + def test_convert_to_python_boolean(self): + xtype = 'Boolean' + # Valid values + self.assertEqual(XmpTag._convert_to_python('True', xtype), True) + self.assertEqual(XmpTag._convert_to_python('False', xtype), False) + # Invalid values: not converted + self.assertEqual(XmpTag._convert_to_python('invalid', xtype), 'invalid') + + def test_convert_to_python_choice(self): + pass + + def test_convert_to_python_colorant(self): + pass + + def test_convert_to_python_date(self): + xtype = 'Date' + # Valid values + self.assertEqual(XmpTag._convert_to_python('1999', xtype), + datetime.date(1999, 1, 1)) + self.assertEqual(XmpTag._convert_to_python('1999-10', xtype), + datetime.date(1999, 10, 1)) + self.assertEqual(XmpTag._convert_to_python('1999-10-13', xtype), + datetime.date(1999, 10, 13)) + self.assertEqual(XmpTag._convert_to_python('1999-10-13T05:03Z', xtype) - \ + datetime.datetime(1999, 10, 13, 5, 3, tzinfo=FixedOffset()), + datetime.timedelta(0)) + self.assertEqual(XmpTag._convert_to_python('1999-10-13T05:03+06:00', xtype) - \ + datetime.datetime(1999, 10, 13, 5, 3, tzinfo=FixedOffset('+', 6, 0)), + datetime.timedelta(0)) + self.assertEqual(XmpTag._convert_to_python('1999-10-13T05:03-06:00', xtype) - \ + datetime.datetime(1999, 10, 13, 5, 3, tzinfo=FixedOffset('-', 6, 0)), + datetime.timedelta(0)) + self.assertEqual(XmpTag._convert_to_python('1999-10-13T05:03:54Z', xtype) - \ + datetime.datetime(1999, 10, 13, 5, 3, 54, tzinfo=FixedOffset()), + datetime.timedelta(0)) + self.assertEqual(XmpTag._convert_to_python('1999-10-13T05:03:54+06:00', xtype) - \ + datetime.datetime(1999, 10, 13, 5, 3, 54, tzinfo=FixedOffset('+', 6, 0)), + datetime.timedelta(0)) + self.assertEqual(XmpTag._convert_to_python('1999-10-13T05:03:54-06:00', xtype) - \ + datetime.datetime(1999, 10, 13, 5, 3, 54, tzinfo=FixedOffset('-', 6, 0)), + datetime.timedelta(0)) + self.assertEqual(XmpTag._convert_to_python('1999-10-13T05:03:54.721Z', xtype) - \ + datetime.datetime(1999, 10, 13, 5, 3, 54, 721000, tzinfo=FixedOffset()), + datetime.timedelta(0)) + self.assertEqual(XmpTag._convert_to_python('1999-10-13T05:03:54.721+06:00', xtype) - \ + datetime.datetime(1999, 10, 13, 5, 3, 54, 721000, tzinfo=FixedOffset('+', 6, 0)), + datetime.timedelta(0)) + self.assertEqual(XmpTag._convert_to_python('1999-10-13T05:03:54.721-06:00', xtype) - \ + datetime.datetime(1999, 10, 13, 5, 3, 54, 721000, tzinfo=FixedOffset('-', 6, 0)), + datetime.timedelta(0)) + # Invalid values + self.assertEqual(XmpTag._convert_to_python('invalid', xtype), 'invalid') + self.assertEqual(XmpTag._convert_to_python('11/10/1983', xtype), '11/10/1983') + + # FIXME: the following test should not fail: having hours without minutes is not a valid syntax. + self.assertEqual(XmpTag._convert_to_python('2009-01-22T21', xtype), '2009-01-22T21') + + # TODO: other types |